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 | import time |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 21 | import json |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 22 | from pyndn import Name |
| 23 | from pyndn import Face |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 24 | from pyndn import Interest |
| 25 | from pyndn import KeyLocator, KeyLocatorType |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 26 | from base_node import BaseNode |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 27 | from pyndn.security.security_exception import SecurityException |
| 28 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 29 | |
| 30 | def dump(*list): |
| 31 | result = "" |
| 32 | for element in list: |
| 33 | result += (element if type(element) is str else repr(element)) + " " |
| 34 | print(result) |
| 35 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 36 | class Device(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 37 | def __init__(self,configFileName): |
| 38 | super(Device, self).__init__(configFileName=configFileName) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 39 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 40 | #self.deviceSerial = self.getSerial() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 41 | 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 Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 49 | def beforeLoopStart(self): |
| 50 | pass |
| 51 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 52 | def onTimeout(self, interest): |
| 53 | self._callbackCount += 1 |
| 54 | dump("Time out for interest", interest.getName().toUri()) |
| 55 | |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 56 | 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 |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 63 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 64 | if __name__ == '__main__': |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 65 | face = Face("") |
| 66 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 67 | device = Device("default.conf") |
| 68 | |
| 69 | symKey = "symmetricKeyForBootStrapping" |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 70 | bootStrapName = Name("/home/controller/bootstrap") |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 71 | |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 72 | deviceParameters = {} |
| 73 | deviceParameters["category"] = "sensors" |
| 74 | deviceParameters["id"] = "T123456789" |
| 75 | bootStrapName.append(json.dumps(deviceParameters)) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 76 | |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 77 | 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: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 91 | 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 | |