philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 20 | import time |
| 21 | from pyndn import Name |
| 22 | from pyndn import Face |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 23 | from base_node import BaseNode |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 24 | |
| 25 | def dump(*list): |
| 26 | result = "" |
| 27 | for element in list: |
| 28 | result += (element if type(element) is str else repr(element)) + " " |
| 29 | print(result) |
| 30 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 31 | class Device(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame^] | 32 | def __init__(self,configFileName): |
| 33 | super(Device, self).__init__(configFileName=configFileName) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 34 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame^] | 35 | #self.deviceSerial = self.getSerial() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 36 | 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 Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame^] | 44 | def beforeLoopStart(self): |
| 45 | pass |
| 46 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 47 | def onTimeout(self, interest): |
| 48 | self._callbackCount += 1 |
| 49 | dump("Time out for interest", interest.getName().toUri()) |
| 50 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 51 | if __name__ == '__main__': |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 52 | face = Face("") |
| 53 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame^] | 54 | 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) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 61 | |
| 62 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame] | 63 | while device._callbackCount < 10: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 64 | 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 | |