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 |
| 23 | |
| 24 | def dump(*list): |
| 25 | result = "" |
| 26 | for element in list: |
| 27 | result += (element if type(element) is str else repr(element)) + " " |
| 28 | print(result) |
| 29 | |
| 30 | class Device(object): |
| 31 | def __init__(self): |
| 32 | self._callbackCount = 0 |
| 33 | |
| 34 | def onData(self, interest, data): |
| 35 | self._callbackCount += 1 |
| 36 | dump("Got data packet with name", data.getName().toUri()) |
| 37 | # Use join to convert each byte to chr. |
| 38 | dump(data.getContent().toRawStr()) |
| 39 | |
| 40 | def onTimeout(self, interest): |
| 41 | self._callbackCount += 1 |
| 42 | dump("Time out for interest", interest.getName().toUri()) |
| 43 | |
| 44 | def main(): |
| 45 | face = Face("") |
| 46 | |
| 47 | device = Device() |
| 48 | |
| 49 | name1 = Name("/home/controller/bootstrap/publickey/category1/id") |
| 50 | dump("Express name ", name1.toUri()) |
| 51 | face.expressInterest(name1, device.onData, device.onTimeout) |
| 52 | |
| 53 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame^] | 54 | while device._callbackCount < 10: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 55 | face.processEvents() |
| 56 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 57 | time.sleep(0.01) |
| 58 | |
| 59 | face.shutdown() |
| 60 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame^] | 61 | main() |