blob: 484751bf852113f9daff0704b6c795638809291a [file] [log] [blame]
philoLbd28e132015-04-16 23:54:21 -07001# -*- 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
21import time
22from pyndn import Name
23from pyndn import Data
24from pyndn import Face
25from pyndn.security import KeyChain
philo5d4724e2014-11-10 19:34:05 +000026from base_node import BaseNode
27
philoLbd28e132015-04-16 23:54:21 -070028
29def dump(*list):
30 result = ""
31 for element in list:
32 result += (element if type(element) is str else repr(element)) + " "
33 print(result)
34
philo5d4724e2014-11-10 19:34:05 +000035class Controller(BaseNode):
36 def __init__(self):
37 super(Controller, self).__init__()
philoLbd28e132015-04-16 23:54:21 -070038 self._responseCount = 0
39
40 def onInterest(self, prefix, interest, transport, registeredPrefixId):
41 self._responseCount += 1
42
philo5eec5e32014-11-08 08:57:52 +000043 dump("interest ", interest.getName())
44 dump("Uri ", interest.getName().toUri())
philoLbd28e132015-04-16 23:54:21 -070045 # Make and sign a Data packet.
46 #data = Data(interest.getName())
philo5eec5e32014-11-08 08:57:52 +000047 content = "Echo " + interest.getName().toUri()
philoLbd28e132015-04-16 23:54:21 -070048 #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
philo5d4724e2014-11-10 19:34:05 +000059if __name__ == '__main__':
60
philoLbd28e132015-04-16 23:54:21 -070061 # 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.
philo5d4724e2014-11-10 19:34:05 +000065
66 controller = Controller()
67 face.setCommandSigningInfo(controller.getKeyChain(), controller.getDefaultCertificateName())
philoLbd28e132015-04-16 23:54:21 -070068
69 # Also use the default certificate name to sign data packets.
philo5d4724e2014-11-10 19:34:05 +000070
philoLbd28e132015-04-16 23:54:21 -070071 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