blob: 9eee1aef5c95c2ac9644af7d8c5d10d63f35b3ba [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
26
27def dump(*list):
28 result = ""
29 for element in list:
30 result += (element if type(element) is str else repr(element)) + " "
31 print(result)
32
33class Controller(object):
34 def __init__(self, keyChain, certificateName):
35 self._keyChain = keyChain
36 self._certificateName = certificateName
37 self._responseCount = 0
38
39 def onInterest(self, prefix, interest, transport, registeredPrefixId):
40 self._responseCount += 1
41
42 dump("interest ": interest)
43 # Make and sign a Data packet.
44 #data = Data(interest.getName())
45 #content = "Echo " + interest.getName().toUri()
46 #data.setContent(content)
47 #self._keyChain.sign(data, self._certificateName)
48 #encodedData = data.wireEncode()
49
50 #dump("Sent content", content)
51 #transport.send(encodedData.toBuffer())
52
53 def onRegisterFailed(self, prefix):
54 self._responseCount += 1
55 dump("Register failed for prefix", prefix.toUri())
56
57def main():
58 # The default Face will connect using a Unix socket, or to "localhost".
59 face = Face()
60
61 # Use the system default key chain and certificate name to sign commands.
62 keyChain = KeyChain()
63 face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
64
65 # Also use the default certificate name to sign data packets.
66 controller = Controller(keyChain, keyChain.getDefaultCertificateName())
67 prefix = Name("/home/")
68 dump("Register prefix", prefix.toUri())
69 face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed)
70
71 while controller._responseCount < 100:
72 face.processEvents()
73 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
74 time.sleep(0.01)
75
76 face.shutdown()
77
78main()