Teng Liang | b09af86 | 2015-06-01 10:28:12 -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 | try: |
| 21 | # Use builtin asyncio on Python 3.4+, or Tulip on Python 3.3 |
| 22 | import asyncio |
| 23 | except ImportError: |
| 24 | # Use Trollius on Python <= 3.2 |
| 25 | import trollius as asyncio |
| 26 | from pyndn import Name |
| 27 | from pyndn import ThreadsafeFace |
| 28 | |
| 29 | def dump(*list): |
| 30 | result = "" |
| 31 | for element in list: |
| 32 | result += (element if type(element) is str else repr(element)) + " " |
| 33 | print(result) |
| 34 | |
| 35 | class Counter(object): |
| 36 | def __init__(self): |
| 37 | self._callbackCount = 0 |
| 38 | |
| 39 | def onData(self, interest, data): |
| 40 | self._callbackCount += 1 |
| 41 | dump("Got data packet with name", data.getName().toUri()) |
| 42 | # Use join to convert each byte to chr. |
| 43 | dump(data.getContent().toRawStr()) |
| 44 | |
| 45 | def onTimeout(self, interest): |
| 46 | self._callbackCount += 1 |
| 47 | dump("Time out for interest", interest.getName().toUri()) |
| 48 | |
| 49 | def onInterest(self,prefix,interest,face,interestFilterId,filter): |
| 50 | self._callbackCount+=1 |
| 51 | dump("Receive interest", interest.toUri()) |
| 52 | |
| 53 | def onRegisterFailed(self,prefix): |
| 54 | dump("Register failed for prefix") |
| 55 | |
| 56 | def main(): |
| 57 | loop = asyncio.get_event_loop() |
| 58 | face = ThreadsafeFace(loop, "aleph.ndn.ucla.edu") |
| 59 | |
| 60 | counter = Counter() |
| 61 | face.stopWhen(lambda: counter._callbackCount >= 10) |
| 62 | |
| 63 | face.registerPrefix("/home",counter.onInterest,counter.onRegisterFailed) |
| 64 | #name1 = Name("/") |
| 65 | #dump("Express name ", name1.toUri()) |
| 66 | # This call to exressIinterest is thread safe because face is a ThreadsafeFace. |
| 67 | #face.expressInterest(name1, counter.onData, counter.onTimeout) |
| 68 | |
| 69 | # Run until stopWhen stops the loop. |
| 70 | loop.run_forever() |
| 71 | face.shutdown() |
| 72 | |
| 73 | main() |