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): |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 32 | def __init__(self): |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 33 | super(Device, self).__init__() |
| 34 | |
| 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 | |
| 44 | def onTimeout(self, interest): |
| 45 | self._callbackCount += 1 |
| 46 | dump("Time out for interest", interest.getName().toUri()) |
| 47 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 48 | if __name__ == '__main__': |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 49 | 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 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame] | 58 | while device._callbackCount < 10: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 59 | 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 | |