blob: 4754117750800a63bf4d7e1d66b142bebca908b4 [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
20import time
Teng Liang52f43c32015-05-20 17:06:20 -070021import json
philoLbd28e132015-04-16 23:54:21 -070022from pyndn import Name
23from pyndn import Face
Teng Liang52f43c32015-05-20 17:06:20 -070024from pyndn import Interest
25from pyndn import KeyLocator, KeyLocatorType
philo5d4724e2014-11-10 19:34:05 +000026from base_node import BaseNode
Teng Liang52f43c32015-05-20 17:06:20 -070027from pyndn.security.security_exception import SecurityException
28
philoLbd28e132015-04-16 23:54:21 -070029
30def dump(*list):
31 result = ""
32 for element in list:
33 result += (element if type(element) is str else repr(element)) + " "
34 print(result)
35
philo5d4724e2014-11-10 19:34:05 +000036class Device(BaseNode):
Teng Lianga0b49372015-05-15 05:30:27 -070037 def __init__(self,configFileName):
38 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000039
Teng Lianga0b49372015-05-15 05:30:27 -070040 #self.deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070041 self._callbackCount = 0
42
43 def onData(self, interest, data):
44 self._callbackCount += 1
45 dump("Got data packet with name", data.getName().toUri())
46 # Use join to convert each byte to chr.
47 dump(data.getContent().toRawStr())
48
Teng Lianga0b49372015-05-15 05:30:27 -070049 def beforeLoopStart(self):
50 pass
51
philoLbd28e132015-04-16 23:54:21 -070052 def onTimeout(self, interest):
53 self._callbackCount += 1
54 dump("Time out for interest", interest.getName().toUri())
55
philoLfb1b24e2015-05-15 05:33:25 -070056 def _sendCertificateRequest(self, keyIdentity):
57 """
58 We compose a command interest with our public key info so the controller
59 can sign us a certificate that can be used with other nodes in the network.
60 """
61
62 #TODO: GENERATE A NEW PUBLIC/PRIVATE PAIR INSTEAD OF COPYING
philoLfb1b24e2015-05-15 05:33:25 -070063
philo5d4724e2014-11-10 19:34:05 +000064if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -070065 face = Face("")
66
Teng Lianga0b49372015-05-15 05:30:27 -070067 device = Device("default.conf")
68
69 symKey = "symmetricKeyForBootStrapping"
Teng Liang52f43c32015-05-20 17:06:20 -070070 bootStrapName = Name("/home/controller/bootstrap")
philoLbd28e132015-04-16 23:54:21 -070071
Teng Liang52f43c32015-05-20 17:06:20 -070072 deviceParameters = {}
73 deviceParameters["category"] = "sensors"
74 deviceParameters["id"] = "T123456789"
75 bootStrapName.append(json.dumps(deviceParameters))
philoLbd28e132015-04-16 23:54:21 -070076
Teng Liang52f43c32015-05-20 17:06:20 -070077 bootStrapInterest = Interest(bootStrapName)
78
79 bootStrapInterest.setInterestLifetimeMilliseconds(5000)
80
81 bootStrapKeyLocator = KeyLocator()
82 bootStrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
83 bootStrapKeyLocator.setKeyData(symKey)
84 bootStrapInterest.setKeyLocator(bootStrapKeyLocator)
85
86 dump("Express interest ",bootStrapInterest.toUri())
87 face.expressInterest(bootStrapInterest, device.onData, device.onTimeout)
88
89
90 while device._callbackCount < 100:
philoLbd28e132015-04-16 23:54:21 -070091 face.processEvents()
92 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
93 time.sleep(0.01)
94
95 face.shutdown()
96