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 | |
| 21 | import time |
| 22 | from pyndn import Name |
| 23 | from pyndn import Data |
| 24 | from pyndn import Face |
| 25 | from pyndn.security import KeyChain |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 26 | from base_node import BaseNode |
| 27 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 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 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 35 | class Controller(BaseNode): |
| 36 | def __init__(self): |
| 37 | super(Controller, self).__init__() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 38 | self._responseCount = 0 |
| 39 | |
| 40 | def onInterest(self, prefix, interest, transport, registeredPrefixId): |
| 41 | self._responseCount += 1 |
| 42 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame] | 43 | dump("interest ", interest.getName()) |
| 44 | dump("Uri ", interest.getName().toUri()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 45 | # Make and sign a Data packet. |
| 46 | #data = Data(interest.getName()) |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame] | 47 | content = "Echo " + interest.getName().toUri() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 48 | #data.setContent(content) |
| 49 | #self._keyChain.sign(data, self._certificateName) |
| 50 | #encodedData = data.wireEncode() |
| 51 | |
| 52 | #dump("Sent content", content) |
| 53 | #transport.send(encodedData.toBuffer()) |
| 54 | |
| 55 | def onRegisterFailed(self, prefix): |
| 56 | self._responseCount += 1 |
| 57 | dump("Register failed for prefix", prefix.toUri()) |
| 58 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 59 | if __name__ == '__main__': |
| 60 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 61 | # The default Face will connect using a Unix socket, or to "localhost". |
| 62 | face = Face() |
| 63 | |
| 64 | # Use the system default key chain and certificate name to sign commands. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 65 | |
| 66 | controller = Controller() |
| 67 | face.setCommandSigningInfo(controller.getKeyChain(), controller.getDefaultCertificateName()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 68 | |
| 69 | # Also use the default certificate name to sign data packets. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame^] | 70 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 71 | prefix = Name("/home/") |
| 72 | dump("Register prefix", prefix.toUri()) |
| 73 | face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed) |
| 74 | |
| 75 | while controller._responseCount < 100: |
| 76 | face.processEvents() |
| 77 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 78 | time.sleep(0.01) |
| 79 | |
| 80 | face.shutdown() |
| 81 | |