| # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| # |
| # Copyright (C) 2014-2015 Regents of the University of California. |
| # Author: Jeff Thompson <jefft0@remap.ucla.edu> |
| # |
| # This program is free software: you can redistribute it and/or modify |
| # it under the terms of the GNU Lesser General Public License as published by |
| # the Free Software Foundation, either version 3 of the License, or |
| # (at your option) any later version. |
| # |
| # This program is distributed in the hope that it will be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU Lesser General Public License for more details. |
| # |
| # You should have received a copy of the GNU Lesser General Public License |
| # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| # A copy of the GNU Lesser General Public License is in the file COPYING. |
| |
| try: |
| # Use builtin asyncio on Python 3.4+, or Tulip on Python 3.3 |
| import asyncio |
| except ImportError: |
| # Use Trollius on Python <= 3.2 |
| import trollius as asyncio |
| from pyndn import Name |
| from pyndn import ThreadsafeFace |
| |
| def dump(*list): |
| result = "" |
| for element in list: |
| result += (element if type(element) is str else repr(element)) + " " |
| print(result) |
| |
| class Counter(object): |
| def __init__(self): |
| self._callbackCount = 0 |
| |
| def onData(self, interest, data): |
| self._callbackCount += 1 |
| dump("Got data packet with name", data.getName().toUri()) |
| # Use join to convert each byte to chr. |
| dump(data.getContent().toRawStr()) |
| |
| def onTimeout(self, interest): |
| self._callbackCount += 1 |
| dump("Time out for interest", interest.getName().toUri()) |
| |
| def onInterest(self,prefix,interest,face,interestFilterId,filter): |
| self._callbackCount+=1 |
| dump("Receive interest", interest.toUri()) |
| |
| def onRegisterFailed(self,prefix): |
| dump("Register failed for prefix") |
| |
| def main(): |
| loop = asyncio.get_event_loop() |
| face = ThreadsafeFace(loop, "aleph.ndn.ucla.edu") |
| |
| counter = Counter() |
| face.stopWhen(lambda: counter._callbackCount >= 10) |
| |
| face.registerPrefix("/home",counter.onInterest,counter.onRegisterFailed) |
| #name1 = Name("/") |
| #dump("Express name ", name1.toUri()) |
| # This call to exressIinterest is thread safe because face is a ThreadsafeFace. |
| #face.expressInterest(name1, counter.onData, counter.onTimeout) |
| |
| # Run until stopWhen stops the loop. |
| loop.run_forever() |
| face.shutdown() |
| |
| main() |