blob: 26c1612b81190a98c298176f586f00c7fe89eb49 [file] [log] [blame]
philoLbd28e132015-04-16 23:54:21 -07001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2014-2015 Regents of the University of California.
4# Author: Jeff Thompson <jefft0@remap.ucla.edu>
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18# A copy of the GNU Lesser General Public License is in the file COPYING.
19
20import time
21from pyndn import Name
22from pyndn import Face
philo5d4724e2014-11-10 19:34:05 +000023from base_node import BaseNode
philoLbd28e132015-04-16 23:54:21 -070024
25def dump(*list):
26 result = ""
27 for element in list:
28 result += (element if type(element) is str else repr(element)) + " "
29 print(result)
30
philo5d4724e2014-11-10 19:34:05 +000031class Device(BaseNode):
philoLbd28e132015-04-16 23:54:21 -070032 def __init__(self):
philo5d4724e2014-11-10 19:34:05 +000033 super(Device, self).__init__()
34
35 self.deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070036 self._callbackCount = 0
37
38 def onData(self, interest, data):
39 self._callbackCount += 1
40 dump("Got data packet with name", data.getName().toUri())
41 # Use join to convert each byte to chr.
42 dump(data.getContent().toRawStr())
43
44 def onTimeout(self, interest):
45 self._callbackCount += 1
46 dump("Time out for interest", interest.getName().toUri())
47
philo5d4724e2014-11-10 19:34:05 +000048if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -070049 face = Face("")
50
51 device = Device()
52
53 name1 = Name("/home/controller/bootstrap/publickey/category1/id")
54 dump("Express name ", name1.toUri())
55 face.expressInterest(name1, device.onData, device.onTimeout)
56
57
philo5eec5e32014-11-08 08:57:52 +000058 while device._callbackCount < 10:
philoLbd28e132015-04-16 23:54:21 -070059 face.processEvents()
60 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
61 time.sleep(0.01)
62
63 face.shutdown()
64