blob: 67fc1506e411b7b5ed6c54fb66d964b828dc36a2 [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):
Teng Lianga0b49372015-05-15 05:30:27 -070032 def __init__(self,configFileName):
33 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000034
Teng Lianga0b49372015-05-15 05:30:27 -070035 #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
Teng Lianga0b49372015-05-15 05:30:27 -070044 def beforeLoopStart(self):
45 pass
46
philoLbd28e132015-04-16 23:54:21 -070047 def onTimeout(self, interest):
48 self._callbackCount += 1
49 dump("Time out for interest", interest.getName().toUri())
50
philo5d4724e2014-11-10 19:34:05 +000051if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -070052 face = Face("")
53
Teng Lianga0b49372015-05-15 05:30:27 -070054 device = Device("default.conf")
55
56 symKey = "symmetricKeyForBootStrapping"
57 bootStrapName = Name("/home/controller/bootstrap/light/id1/"+symKey)
58 dump("Express name ",bootStrapName.toUri())
59
60 face.expressInterest(bootStrapName, device.onData, device.onTimeout)
philoLbd28e132015-04-16 23:54:21 -070061
62
philo5eec5e32014-11-08 08:57:52 +000063 while device._callbackCount < 10:
philoLbd28e132015-04-16 23:54:21 -070064 face.processEvents()
65 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
66 time.sleep(0.01)
67
68 face.shutdown()
69