def main(): # COMMAND LINE ARGS parser = argparse.ArgumentParser( description='Parse or follow Cascade Datahub log and publish to NDN.') parser.add_argument('filename', help='datahub log file') parser.add_argument('-f', dest='follow', action='store_true', help='follow (tail -f) the log file') parser.add_argument('--namespace', default='/ndn/edu/ucla/remap/bms', help='root ndn name, no trailing slash') args = parser.parse_args() # NDN global face, keychain loop = asyncio.get_event_loop() face = ThreadsafeFace(loop, "localhost") keychain = KeyChain( IdentityManager( BasicIdentityStorage(), FilePrivateKeyStorage())) # override default even for MacOS cache = MemoryContentCache(face) # READ THE FILE (MAIN LOOP) if args.follow: loop.run_until_complete( followfile(args.filename, args.namespace, cache)) else: loop.run_until_complete(readfile(args.filename, args.namespace, cache)) face.shutdown()
class ThreadsafeFaceWrapper(object): def __init__(self): self._loop = asyncio.get_event_loop() self._face = ThreadsafeFace(self._loop, "") self._keyChain = KeyChain() self._certificateName = self._keyChain.getDefaultCertificateName() self._face.setCommandSigningInfo(self._keyChain, self._certificateName) def startProcessing(self): try: self._loop.run_forever() finally: self.stop() def stopProcessing(self): self._loop.close() self._face.shutdown() self._face = None sys.exit(1) def getFace(self): return self._face def getLoop(self): return self._loop
def main(): loop = asyncio.get_event_loop() face = ThreadsafeFace(loop, "localhost") keyChain = KeyChain() face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName()) # TODO: NFD hack: uncomment once NFD forwarding fixed # discoveree = Discoveree(loop, face, keyChain) # TODO: NFD hack: remove once NFD forwarding fixed discoveree = LocalDiscoveree(loop, face, keyChain) cecTv = CecTv(loop, face, keyChain, discoveree) loop.run_forever() face.shutdown()
def main(): loop = asyncio.get_event_loop() face = ThreadsafeFace(loop, "aleph.ndn.ucla.edu") counter = Counter() face.stopWhen(lambda: counter._callbackCount >= 1) name1 = Name("/") dump("Express name ", name1.toUri()) # This call to exressIinterest is thread safe because face is a ThreadsafeFace. face.expressInterest(name1, counter.onData, counter.onTimeout) # Run until stopWhen stops the loop. loop.run_forever() face.shutdown()
class RegisterSongList(object): def __init__(self, prefix="/ndn/edu/ucla/remap/music/list"): logging.basicConfig() '''#这些log是干嘛的myIP="192.168.1.1",lightIP="192.168.1.50", self.log = logging.getLogger("RegisterSongList") self.log.setLevel(logging.DEBUG) sh = logging.StreamHandler() sh.setLevel(logging.DEBUG) self.log.addHandler(sh) fh = logging.FileHandler("RegisterSongList.log") fh.setLevel(logging.INFO) self.log.addHandler(fh)''' self.device = "PC3" self.deviceComponent = Name.Component(self.device) self.excludeDevice = None #self.songList = originalList #security? self.prefix = Name(prefix) self.changePrefix = Name("/ndn/edu/ucla/remap/music/storage") self.keychain = KeyChain() self.certificateName = self.keychain.getDefaultCertificateName() self.address = "" self._isStopped = True #self.payloadBuffer = [] #self.kinetsender = KinetSender(myIP, playerIP,self.STRAND_SIZE*self.COLORS_PER_LIGHT) def start(self): print "reg start" self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop,self.address) self.face.setCommandSigningInfo(self.keychain,self.certificateName) self.face.registerPrefix(self.prefix,self.onInterest,self.onRegisterFailed) self._isStopped = False self.face.stopWhen(lambda:self._isStopped) try: self.loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self.loop.close() #self.kinetsender.stop = True #self.kinetsender.complete.wait() self.face.shutdown() self.face = None sys.exit(1) def signData(self,data): data.setSignature(Sha256WithRsaSignature()) #self.keychain.sign(data,self.certificateName) def onInterest(self, prefix, interest, transport, registeredPrefixId): print "received interest" initInterest = Name(interest.getName()) print "interest name:",initInterest.toUri() #d = Data(interest.getName().getPrefix(prefix.size()+1)) #self.excludeDevice = interest.getName().get(prefix.size()) #initInterest = interest.getName() d = Data(interest.getName().append(self.deviceComponent)) try: print "start to set data's content" currentString = ','.join(currentList) d.setContent("songList of " +self.device+":"+currentString+ "\n") self.face.registerPrefix(self.changePrefix,self.onInterest,self.onRegisterFailed) except KeyboardInterrupt: print "key interrupt" sys.exit(1) except Exception as e: print e d.setContent("Bad command\n") finally: self.keychain.sign(d,self.certificateName) encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) print d.getName().toUri() print d.getContent() self.stop() '''print"remove register" self.face.removeRegisteredPrefix(registeredPrefixId) time.sleep(30) #sleep 30s which means user cannot update the song list twice within 1 minutes print"register again" self.face.registerPrefix(self.prefix, self.onInterest, self.onRegisterFailed)''' def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class RegisterSongList(object): def __init__(self, prefix="/ndn/edu/ucla/remap/music/list"): logging.basicConfig() '''#这些log是干嘛的myIP="192.168.1.1",lightIP="192.168.1.50", self.log = logging.getLogger("RegisterSongList") self.log.setLevel(logging.DEBUG) sh = logging.StreamHandler() sh.setLevel(logging.DEBUG) self.log.addHandler(sh) fh = logging.FileHandler("RegisterSongList.log") fh.setLevel(logging.INFO) self.log.addHandler(fh)''' self.device = "PC3" self.deviceComponent = Name.Component(self.device) self.excludeDevice = None #self.songList = originalList #security? self.prefix = Name(prefix) self.changePrefix = Name("/ndn/edu/ucla/remap/music/storage") self.keychain = KeyChain() self.certificateName = self.keychain.getDefaultCertificateName() self.address = "" self._isStopped = True #self.payloadBuffer = [] #self.kinetsender = KinetSender(myIP, playerIP,self.STRAND_SIZE*self.COLORS_PER_LIGHT) def start(self): print "reg start" self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, self.address) self.face.setCommandSigningInfo(self.keychain, self.certificateName) self.face.registerPrefix(self.prefix, self.onInterest, self.onRegisterFailed) self._isStopped = False self.face.stopWhen(lambda: self._isStopped) try: self.loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self.loop.close() #self.kinetsender.stop = True #self.kinetsender.complete.wait() self.face.shutdown() self.face = None sys.exit(1) def signData(self, data): data.setSignature(Sha256WithRsaSignature()) #self.keychain.sign(data,self.certificateName) def onInterest(self, prefix, interest, transport, registeredPrefixId): print "received interest" initInterest = Name(interest.getName()) print "interest name:", initInterest.toUri() #d = Data(interest.getName().getPrefix(prefix.size()+1)) #self.excludeDevice = interest.getName().get(prefix.size()) #initInterest = interest.getName() d = Data(interest.getName().append(self.deviceComponent)) try: print "start to set data's content" currentString = ','.join(currentList) d.setContent("songList of " + self.device + ":" + currentString + "\n") self.face.registerPrefix(self.changePrefix, self.onInterest, self.onRegisterFailed) except KeyboardInterrupt: print "key interrupt" sys.exit(1) except Exception as e: print e d.setContent("Bad command\n") finally: self.keychain.sign(d, self.certificateName) encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) print d.getName().toUri() print d.getContent() self.stop() '''print"remove register" self.face.removeRegisteredPrefix(registeredPrefixId) time.sleep(30) #sleep 30s which means user cannot update the song list twice within 1 minutes print"register again" self.face.registerPrefix(self.prefix, self.onInterest, self.onRegisterFailed)''' def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class SongHandler: def __init__(self): self._device = "PC1" self._playPrefix = Name("/ndn/edu/ucla/remap/music/play") self.prefix = self._playPrefix.append(self._device) self._face = None self._loop = None self._keyChain = KeyChain() self._certificateName = self._keyChain.getDefaultCertificateName() self._repoCommandPrefix = "/example/repo/1" self.mp = MusicPlayer() #self._getFiles = [] def start(self): self._loop = asyncio.get_event_loop() self._face = ThreadsafeFace(self._loop,"") self._face.setCommandSigningInfo(self._keyChain, self._certificateName) self._face.registerPrefix(self.prefix, self.onPlayingCommand, self.onRegisterFailed) try: self._loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self._loop.close() self._face.shutdown() sys.exit(1) def getOnset(self, musicName): otxt = musicName + str("-o") print otxt g = GetFile(self._repoCommandPrefix, otxt, self._face, self.getFreq, musicName) g.oStream = open(musicName + str("-os.txt"), 'wb') g.start() #self._getFiles.append(g) def getFreq(self, musicName): ftxt = musicName + str("-f") print musicName, " get Freq called" g = GetFile(self._repoCommandPrefix, ftxt, self._face, self.mp.play_music, musicName, False) g.oStream = open(musicName + str("-freq.txt"),'wb') g.start() #self._getFiles.append(g) def signData(self, data): data.setSignature(Sha256WithRsaSignature()) def onPlayingCommand(self, prefix, interest, transport, prefixId): interestName = Name(interest.getName()) commandComponent = interest.getName().get(self.prefix.size()) if commandComponent == Name.Component("stop"): pass if commandComponent == Name.Component("play"): pass else: songName = commandComponent.toEscapedString() songList = [] songList = songName.split('%2C') for i in songList: fmusic = i + str("-music.mp3") print "started getting music file", i g = GetFile(self._repoCommandPrefix, i, self._face, self.getOnset, i) g.oStream = open(fmusic,'wb') g.start() #self._getFiles.append(g) d = Data(interest.getName()) d.setContent("start to play: " + songName + "\n") encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class LightController(): shouldSign = False COLORS_PER_LIGHT = 3 STRAND_SIZE = 50 #mengchen: let's make the lighting lits only a half every time #HALF_STRAND_SIZE = 25 def __init__(self, nStrands=1, myIP="192.168.1.1", lightIP="192.168.1.50", prefix="/testlight"): self.log = logging.getLogger("LightController") self.log.setLevel(logging.DEBUG) sh = logging.StreamHandler() sh.setLevel(logging.DEBUG) self.log.addHandler(sh) fh = logging.FileHandler("LightController.log") fh.setLevel(logging.INFO) self.log.addHandler(fh) self.payloadBuffer = [[0]*self.STRAND_SIZE*self.COLORS_PER_LIGHT for n in range(nStrands)] self.kinetsender = KinetSender(myIP, lightIP, nStrands, self.STRAND_SIZE*self.COLORS_PER_LIGHT) self.prefix = Name(prefix) self.keychain = KeyChain() self.address = "" self._isStopped = True self.lightState = False #mengchen: let's make the lighting lits only a half every time #self.uphalf = True self.HALF_STRAND_SIZE = 25 self.certificateName = self.keychain.getDefaultCertificateName() self.receiveFile = open('interestReceiveFile', 'w') def unix_time_now(self): epoch = datetime.datetime.utcfromtimestamp(0) delta = datetime.datetime.utcnow() - epoch return delta.total_seconds() * 1000.0 # XXX: we should get a thread for this or something! def start(self): self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, self.address) self.face.setCommandSigningInfo(self.keychain, self.certificateName) self.face.registerPrefix(self.prefix, self.onLightingCommand, self.onRegisterFailed) self._isStopped = False self.face.stopWhen(lambda:self._isStopped) try: self.loop.run_forever() except KeyboardInterrupt: #self.stop() #print "key interrupt in run_forever" sys.exit() finally: #print "executing finally" self.stop() def stop(self): self.loop.close() self.kinetsender.stop = True #print "before wait" #self.kinetsender.complete.set #self.kinetsender.complete.wait() self.face.shutdown() self.face = None #print "sys exit" self.receiveFile.close() sys.exit(1) def signData(self, data): if LightController.shouldSign: self.keychain.sign(data, self.certificateName) else: data.setSignature(Sha256WithRsaSignature()) def setPayloadColor(self, strand, color): # will expand this to allow the repeats, etc # if self.uphalf and not self.lightState: # self.uphalf = False # self.payloadBuffer[strand] = [int(color.r)&0xff, int(color.g)&0xff, int(color.b)&0xff]*self.HALF_STRAND_SIZE+[int(0)&0xff, int(0)&0xff, int(0)&0xff]*self.HALF_STRAND_SIZE #if not self.uphalf and self.lightState : # self.uphalf = True self.payloadBuffer[strand] = [int(color.r)&0xff, int(color.g)&0xff, int(color.b)&0xff]*self.STRAND_SIZE def onLightingCommand(self, prefix, interest, transport, prefixId): #print datetime.datetime.now() self.receiveFile.write('{0:f}'.format(self.unix_time_now()) + '\n') interestName = Name(interest.getName()) #interstname: /ndn/ucla.edu/sculptures/ai_bus/lights/setRGB/%83%0D%84%0B%87%09%89%01%04%8A%01%01%8B%01%01 #d: <pyndn.data.Data object at 0xb64825d0> print "interstname", interestName.toUri() d = Data(interest.getName()) # get the command parameters from the name try: commandComponent = interest.getName().get(prefix.size()) #print commandComponent.toEscapedString():setRGB #print "prefix ",prefix.toUri():/ndn/ucla.edu/sculpture/ai_bus/lights #print "get name",interest.getName().toUri() commandParams = interest.getName().get(prefix.size()+1) #print "commandParams ",commandParams:%83%0D%84%0B%87%09%89%01%04%8A%01%01%8B%01%01 lightingCommand = LightCommandMessage() ProtobufTlv.decode(lightingCommand, commandParams.getValue()) #self.log.info("Command: " + commandComponent.toEscapedString()) requestedColor = lightingCommand.command.pattern.colors[0] colorStr = str((requestedColor.r, requestedColor.g, requestedColor.b)) #self.log.info("Requested color: " + colorStr) self.setPayloadColor(0, requestedColor) self.lightState = not self.lightState if self.lightState: print "Off" else: print "On" #print requestedColor self.sendLightPayload(1) d.setContent("Gotcha: " + colorStr+ "\n") except KeyboardInterrupt: print "key interrupt" sys.exit(1) except Exception as e: print e d.setContent("Bad command\n") finally: d.getMetaInfo().setFinalBlockID(0) self.signData(d) encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop() def sendLightPayload(self, port): # print port # print self.payloadBuffer[port-1] self.kinetsender.setPayload(port, self.payloadBuffer[port-1])
class SongHandler: def __init__(self): self._device = "PC1" self._playPrefix = Name("/ndn/edu/ucla/remap/music/play") self.prefix = self._playPrefix.append(self._device) self._face = None self._loop = None self._keyChain = KeyChain() self._certificateName = self._keyChain.getDefaultCertificateName() self._repoCommandPrefix = "/example/repo/1" self.mp = MusicPlayer() #self._getFiles = [] def start(self): self._loop = asyncio.get_event_loop() self._face = ThreadsafeFace(self._loop, "") self._face.setCommandSigningInfo(self._keyChain, self._certificateName) self._face.registerPrefix(self.prefix, self.onPlayingCommand, self.onRegisterFailed) try: self._loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self._loop.close() self._face.shutdown() sys.exit(1) def getOnset(self, musicName): otxt = musicName + str("-o") print otxt g = GetFile(self._repoCommandPrefix, otxt, self._face, self.getFreq, musicName) g.oStream = open(musicName + str("-os.txt"), 'wb') g.start() #self._getFiles.append(g) def getFreq(self, musicName): ftxt = musicName + str("-f") print musicName, " get Freq called" g = GetFile(self._repoCommandPrefix, ftxt, self._face, self.mp.play_music, musicName, False) g.oStream = open(musicName + str("-freq.txt"), 'wb') g.start() #self._getFiles.append(g) def signData(self, data): data.setSignature(Sha256WithRsaSignature()) def onPlayingCommand(self, prefix, interest, transport, prefixId): interestName = Name(interest.getName()) commandComponent = interest.getName().get(self.prefix.size()) if commandComponent == Name.Component("stop"): pass if commandComponent == Name.Component("play"): pass else: songName = commandComponent.toEscapedString() songList = [] songList = songName.split('%2C') for i in songList: fmusic = i + str("-music.mp3") print "started getting music file", i g = GetFile(self._repoCommandPrefix, i, self._face, self.getOnset, i) g.oStream = open(fmusic, 'wb') g.start() #self._getFiles.append(g) d = Data(interest.getName()) d.setContent("start to play: " + songName + "\n") encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class RepoConsumer: TIMEOUT = 100 def __init__(self, dataPrefix, interestLifetime=100, lastVersion=None, start=True): self.prefix = Name(dataPrefix) #used in the exclude to make sure we get new data only self.lastVersion = lastVersion self.face = None self.dataFormat = re.compile("\((\d+\.?\d*)\)") # data should start with a timestamp in logFormat = '%(asctime)-10s %(message)s' self.logger = logging.getLogger('RepoConsumer') self.logger.setLevel(logging.DEBUG) self.logger.addHandler(logging.StreamHandler()) fh = logging.FileHandler('repo_consumer.log', mode='w') fh.setLevel(logging.DEBUG) fh.setFormatter(logging.Formatter(logFormat)) self.logger.addHandler(fh) self.createTiming = [] self.receiveTiming = [] self.timeouts = 0 self.notReady = 0 self.lastReceivedTime = 0 self.lastCreatedTime = 0 self.backoffCounter = 0 self.interestLifetime = interestLifetime self.nextIssue = None self.loop = None self.isCancelled = False def start(self): self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, "") self.face.stopWhen(lambda:self.isCancelled) self.reissueInterest() self.loop.run_forever() def stop(self): self.loop.close() self.face.shutdown() self.loop = None self.face = None def onData(self, interest, data): now = time.time() # for now, assume there is a version appended to the interest nameLength = interest.getName().size() dataStr = data.getContent().toRawStr() try: lastComponent = data.getName().get(-1).toEscapedString() if str(lastComponent) == 'MISSING': self.notReady += 1 #self.backoffCounter += 1 logger.info('repo not ready') self.reissueInterest() return self.lastVersion = data.getName().get(nameLength) self.logger.debug(interest.getName().toUri() + ": version " + self.lastVersion.toEscapedString()) match = self.dataFormat.match(data.getContent().toRawStr()) ts = float(match.group(1)) if self.lastReceivedTime != 0 and self.lastCreatedTime != 0: self.createTiming.append(now-self.lastReceivedTime) self.receiveTiming.append(now-self.lastCreatedTime) self.lastReceivedTime = now self.lastCreatedTime = ts self.logger.debug("Created: " + str(ts) + ", received: " + str(now)) except Exception as e: self.logger.exception(str(e)) #self.backoffCounter -= 1 self.reissueInterest() def onTimeout(self, interest): self.logger.debug("timeout") self.timeouts += 1 #self.backoffCounter += 1 self.reissueInterest() def reissueInterest(self): BACKOFF_THRESHOLD = 10 if self.backoffCounter > BACKOFF_THRESHOLD: self.TIMEOUT += 50 self.backoffCounter = 0 self.logger.debug('Backing off interval to ' + str(self.TIMEOUT)) if self.backoffCounter < -BACKOFF_THRESHOLD: self.TIMEOUT -= 50 self.backoffCounter = 0 self.logger.debug('Reducing backoff interval to ' + str(self.TIMEOUT)) if self.nextIssue is not None: now = time.clock() if self.nextIssue > now: pass # time.sleep(self.nextIssue-now) interest = Interest(Name(self.prefix)) interest.setInterestLifetimeMilliseconds(self.interestLifetime) interest.setMustBeFresh(False) if self.lastVersion is not None: e = Exclude() e.appendAny() e.appendComponent(self.lastVersion) interest.setExclude(e) interest.setChildSelector(1) #rightmost == freshest self.face.expressInterest(interest, self.onData, self.onTimeout) self.nextIssue = time.clock()+self.TIMEOUT/2000 def printStats(self): # the first value may have been sitting in the repo forever, so ignore the first time timing = self.createTiming self.logger.info('***** Statistics ***** ') if len(timing) > 1: self.logger.info('{1:3.2f}/{2:3.2f}/{3:3.2f} min/mean/max delay(creation)'.format(len(timing), min(timing), mean(timing), max(timing))) timing = self.receiveTiming if len(timing) > 1: self.logger.info('{1:3.2f}/{2:3.2f}/{3:3.2f} min/mean/max delay(receipt)'.format(len(timing), min(timing), mean(timing), max(timing))) self.logger.info('{} data requests satisfied'.format(len(timing))) self.logger.info('{} timeouts'.format(self.timeouts)) self.logger.info('{} not ready responses'.format(self.notReady)) self.logger.info('*'*22)
class NdnRepoClient(object): def __init__(self, repoPrefix=None): super(NdnRepoClient, self).__init__() if repoPrefix is not None: self.repoPrefix = Name(repoPrefix) else: self.repoPrefix = Name('/test/repo') self.loadKey() self.isStopped = False self.dataReady = asyncio.Event() self.resultQueue = asyncio.Queue() self.log = logging.getLogger(str(self.__class__)) h = logging.FileHandler('repo_client.log') s = logging.StreamHandler() logFormatter = logging.Formatter( '%(asctime)-15s %(levelname)-8s %(funcName)s\n\t%(message)s') s.setFormatter(logFormatter) h.setFormatter(logFormatter) self.log.addHandler(h) self.log.addHandler(s) s.setLevel(logging.WARN) self.log.setLevel(logging.DEBUG) self.isStopped = True logging.getLogger('trollius').addHandler(h) self.knownKeys = {} self.pendingKeys = [] def loadKey(self, keyFile='bms_key.pri'): self.keyId = '\xa2\xeb9\xbcGo$\xad\xbf\xe9?k\xb2\xb8|\xa8 E\x96\x13\x1e\xb9\x97\x91Z\xf6\xda\xd1]\xa1lD' with open(keyFile, 'r') as keyFile: binDer = keyFile.read() self.privateKey = RSA.importKey(binDer) @asyncio.coroutine def _decryptAndPrintRecord(self, recordData, keyName, parentDoc): keyUri = keyName.toUri() def cleanString(dirty): return ''.join(filter(string.printable.__contains__, str(dirty))) def onKeyDataReceived(keyInterest, keyData): self.log.debug('Got key for {}'.format(keyInterest.getName())) cipherText = str(keyData.getContent()) symKeyRaw = self.privateKey.decrypt(cipherText) symKey = symKeyRaw[-64:].decode('hex') msg = recordData[8:] iv = msg[:16] cipherText = msg[16:] cipher = AES.new(key=symKey, IV=iv, mode=AES.MODE_CBC) decData = cleanString(cipher.decrypt(cipherText)) fromJson = json.loads(decData) fromJson.update(parentDoc) self.knownKeys[keyUri] = keyData try: self.pendingKeys.remove(keyUri) except ValueError: pass # already removed self.resultQueue.put_nowait(fromJson) def onKeyTimeout(keyInterest): self.log.error('Could not get decryption key for {}'.format(keyInterest.getName())) self.resultQueue.put_nowait({}) i = Interest(keyName) i.setMustBeFresh(False) i.setInterestLifetimeMilliseconds(2000) if keyUri in self.knownKeys: self.log.debug('Using known key') onKeyDataReceived(i, self.knownKeys[keyUri]) elif keyUri in self.pendingKeys: self.log.debug('Waiting on pending key') timeout = 0 while keyUri not in self.knownKeys: yield From(asyncio.sleep(0.5)) timeout += 1 if timeout > 5: self.log.warn('Timed out on key {}'.format(keyUri)) onKeyTimeout(i) break else: onKeyDataReceived(i, self.knownKeys[keyUri]) else: self.log.debug('Adding {} to pending keys'.format(keyUri)) self.pendingKeys.append(keyUri) self.keyFace.expressInterest(i, onKeyDataReceived, onKeyTimeout) def prettifyResults(self, resultsList): # dictionary comparison is by length (# of k:v pairs) allKeys = max(resultsList).keys() columnWidths = {} for k in allKeys: if k == 'ts': columnWidths[k] = len(max([(result[k]).isoformat() for result in resultsList if k in result])) else: columnWidths[k] = len(max([str(result[k]) for result in resultsList if k in result])) columnWidths[k] = max(columnWidths[k], len(k)+2) headerLen = sum(columnWidths.values())+len(k) print '-'*headerLen headers = [] for k in allKeys: headers.append('{0:^{1}}'.format(k, columnWidths[k])) print '|'+'|'.join(headers)+'|' print '-'*headerLen for result in resultsList: line = [] for k in allKeys: if k not in result: val = '' elif k == 'ts': val = result[k].isoformat() else: val = result[k] line.append('{0:^{1}}'.format(val, columnWidths[k])) print '|'+'|'.join(line)+'|' print '-'*headerLen @asyncio.coroutine def collectResults(self, allData): try: for record in allData: parentDoc = {k:v for (k,v) in record.items() if k not in [u'_id', u'value']} aDataVal = str(record[u'value']) keyTs = aDataVal[:8] keyDataName = Name('/ndn/ucla.edu/bms/melnitz/kds').append(keyTs).append(self.keyId) yield From(self._decryptAndPrintRecord(aDataVal, keyDataName, parentDoc)) receivedVals = [] try: for i in asyncio.as_completed([ self.resultQueue.get() for n in range(len(allData))], timeout=5): v = yield From(i) receivedVals.append(v) except asyncio.TimeoutError: pass self.prettifyResults(receivedVals) print finally: self.dataReady.set() def onDataReceived(self, interest, data): # now we have to retrieve the key dataContent = str(data.getContent()) dataContent = BSON(dataContent) dataDict = dataContent.decode() allData = dataDict['results'] totalCount = dataDict['count'] dataCount = len(allData) skipPos = dataDict['skip'] print '---------' print 'Got {}/{} result(s), starting at {}'.format(dataCount, totalCount, skipPos) asyncio.async(self.collectResults(allData)) def onDataTimeout(self, interest): self.log.warn("Timed out on {}".format(interest.toUri())) self.dataReady.set() def sendDataRequestCommand(self, dataName): interest = Interest(dataName) interest.setInterestLifetimeMilliseconds(4000) self.face.expressInterest(interest, self.onDataReceived, self.onDataTimeout) return interest def stop(self): self.isStopped = True def parseSqlSelect(self, statement): statement = statement.strip() queryDict = {} try: selection, parts = re.split(r'\s+WHERE\s+', statement, flags=re.I) except ValueError: print 'WHERE clause is missing' return None selectMatch = re.match(r'^SELECT\s+(?:(\*)|(\w+,(\s*\w+)*))', selection, flags=re.I) if selectMatch is None: print 'Malformed SELECT' return None expressions = re.split(r'\s+AND\s+', parts, flags=re.I) for ex in expressions: if len(ex) == 0: continue try: k,v = ex.split('=') except ValueError: print 'Malformed WHERE clause {}'.format(ex) else: queryDict[k.strip()] = v.strip() return queryDict # get = pairs def assembleDataName(self): schemaStr = ('/ndn/ucla.edu/bms/{building}/data/{room}/electrical/panel/{panel_name}/{quantity}/{data_type}') keyNames = ['building', 'room', 'panel_name', 'quantity', 'data_type'] sqlStatement = raw_input('Query> ').strip() valueDict = self.parseSqlSelect(sqlStatement) if valueDict is None: return None for k in keyNames: valueDict.setdefault(k, '_') dataName = schemaStr.format(**valueDict) return Name(dataName) @asyncio.coroutine def parseDataRequest(self): while True: self.dataReady.clear() dataName = self.assembleDataName() if dataName is not None: self.loop.call_soon(self.sendDataRequestCommand, dataName) yield From(self.dataReady.wait()) def start(self): self.isStopped = False self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, '') self.keyFace = ThreadsafeFace(self.loop, 'borges.metwi.ucla.edu') self.face.stopWhen(lambda:self.isStopped) self.keyFace.stopWhen(lambda:self.isStopped) k = KeyChain() self.face.setCommandSigningInfo(k, k.getDefaultCertificateName()) try: self.loop.run_until_complete(self.parseDataRequest()) except (EOFError, KeyboardInterrupt): pass finally: self.face.shutdown()
class NdnRepoPing(object): """ A repo is supposed to be notified of new data by the publisher. Since the publisher doesn't know about this repo, I will 'manually' poke the repo to insert data points. """ def __init__(self, repoPrefix=None): super(NdnRepoPing, self).__init__() if repoPrefix is not None: self.repoPrefix = repoPrefix else: self.repoPrefix = Name('/test/repo') self.repoWatchNames = [] self.log = logging.getLogger(str(self.__class__)) h = logging.FileHandler('repo_ping.log') h.setFormatter(logging.Formatter( '%(asctime)-15s %(levelname)-8s %(funcName)s\n\t%(message)s')) self.log.addHandler(h) self.log.setLevel(logging.DEBUG) self.isStopped = True logging.getLogger('trollius').addHandler(h) def onDataReceived(self, interest, data): self.log.debug('Response to {}'.format(interest.getName())) responseMessage = RepoCommandResponseMessage() ProtobufTlv.decode(responseMessage, data.getContent()) self.log.debug('Status code: {}'.format( responseMessage.response.status_code)) def onTimeout(self, interest): self.log.info('Timed out on {}'.format(interest.getName())) def sendRepoInsertCommand(self, dataName): self.log.debug('Sending insert command for {}'.format(dataName)) commandMessage = RepoCommandParameterMessage() command = commandMessage.command for component in dataName: command.name.components.append(str(component.getValue())) command.start_block_id = command.end_block_id = 0 commandComponent = ProtobufTlv.encode(commandMessage) interestName = Name(self.repoPrefix).append('insert') interestName.append(commandComponent) interest = Interest(interestName) interest.setInterestLifetimeMilliseconds(4000) self.face.makeCommandInterest(interest) self.face.expressInterest(interest, self.onDataReceived, self.onTimeout) @asyncio.coroutine def sendNextInsertRequest(self): sleepTime = 60*15 while True: for name in self.repoWatchNames: self.sendRepoInsertCommand(name) yield From(asyncio.sleep(sleepTime)) def start(self): self.isStopped = False self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) self.face = ThreadsafeFace(self.loop, '') k = KeyChain() self.face.setCommandSigningInfo(k, k.getDefaultCertificateName()) self.face.stopWhen(lambda:self.isStopped) try: self.loop.run_until_complete(self.sendNextInsertRequest()) finally: self.face.shutdown() def stop(self): self.isStopped = True def addWatchName(self, newName): if newName not in self.repoWatchNames: self.repoWatchNames.append(Name(newName))
class NdnHierarchicalRepo(object): def __init__(self, repoPrefix=None): super(NdnHierarchicalRepo, self).__init__() self.schemaList = [] if repoPrefix is None: self.repoPrefix = Name('/test/repo') else: self.repoPrefix = Name(repoPrefix) self.keyChain = KeyChain() self.currentProcessId = 0 self.pendingProcesses = {} self.log = logging.getLogger(str(self.__class__)) s = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)-15s %(funcName)s\n\t %(message)s') s.setFormatter(formatter) s.setLevel(logging.INFO) self.log.addHandler(s) self.log.setLevel(logging.INFO) def addSchema(self, schema): if not isinstance(schema, NdnSchema): schema = NdnSchema(schema) schema.addField('value', NdnSchema.SCHEMA_BINARY) schema.addField('ts', NdnSchema.SCHEMA_TIMESTAMP) schema.log = self.log self.schemaList.append(schema) self.dataPrefix = schema.dbPrefix def initializeDatabase(self): self.dbClient = mongo.MongoClient() # TODO: different DB name for different schemata? self.db = self.dbClient['bms'] def _registerFailed(self, prefix): self.log.info('Registration failure') if prefix == self.dataPrefix: callback = self.handleDataInterests elif prefix == self.repoPrefix: callback = self.handleCommandInterests self.loop.call_later(5, self._register, prefix, callback) def _register(self, prefix, callback): self.face.registerPrefix(prefix, callback, self._registerFailed) def insertData(self, data): dataName = data.getName() self.log.info('Inserting {}'.format(dataName)) useSchema = (s for s in self.schemaList if s.dbPrefix.match(dataName)).next() #exclude timestamp... dataFields = useSchema.matchNameToSchema(dataName[:-1]) if len(dataFields) == 0: self.log.error('Invalid data name for schema') dataValue = Binary(str(data.getContent())) tsComponent = str(dataName[-1].getValue()) tsConverted = float(struct.unpack("!Q", tsComponent)[0])/1000 dataFields.update({'value':dataValue, 'ts':tsConverted}) dataFields = useSchema.sanitizeData(dataFields) dataCollection = self.db['data'] new_id = dataCollection.update(dataFields, dataFields, upsert=True) self.log.debug('Inserted object {}'.format(new_id)) def start(self): self.initializeDatabase() self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, '') self.face.setCommandSigningInfo(self.keyChain, self.keyChain.getDefaultCertificateName()) self._register(self.dataPrefix, self.handleDataInterests) self._register(self.repoPrefix, self.handleCommandInterests) #TODO: figure out why nfdc doesn't forward to borges self._insertFace = ThreadsafeFace(self.loop, 'borges.metwi.ucla.edu') try: self.loop.run_forever() except Exception as e: self.log.exception(e, exc_info=True) self.face.shutdown() def _segmentResponseData(self, dataQuery, segmentNum=0): limit = 10 startPos = segmentNum*limit results = self.db['data'].find(dataQuery).skip(segmentNum*limit).limit(limit) return(startPos, results) def handleDataInterests(self, prefix, interest, transport, prefixId): # TODO: verification # we match the components to the name, and any '_' components # are discarded. Then we run a MongoDB query and append the # object id to allow for excludes chosenSchema = (s for s in self.schemaList if s.dbPrefix.match(prefix)).next() interestName = interest.getName() responseName = Name(interestName) nameFields = chosenSchema.matchNameToSchema(interestName) self.log.info("Data requested with params:\n\t{}".format(nameFields)) allResults = [] segment = 0 try: segmentComponent = interest.getName()[-4] segment = segmentComponent.toSegment() except RuntimeError: pass (startPos, results) = self._segmentResponseData(nameFields, segment) for result in results: dataId = result[u'_id'] self.log.debug("Found object {}".format(dataId)) allResults.append(result) #responseName.append(str(dataId)) totalCount = results.count(False) responseObject = {'count':totalCount, 'skip':startPos, 'results':allResults} responseData = Data(responseName) resultEncoded = BSON.encode(responseObject) responseData.setContent(resultEncoded) transport.send(responseData.wireEncode().buf()) def _onInsertionDataReceived(self, interest, data): # TODO: update status in processingList self.log.debug("Got {} in response to {}".format(data.getName(), interest.getName())) self.insertData(data) def _onInsertionDataTimeout(self, interest): self.log.warn("Timeout on {}".format(interest.getName())) def handleCommandInterests(self, prefix, interest, transport, prefixId): # TODO: verification interestName = interest.getName() if len(interestName) <= len(prefix)+4: self.log.info("Bad command interest") commandName = str(interestName[len(prefix)].getValue()) responseMessage = RepoCommandResponseMessage() if commandName == 'insert': commandParams = interestName[len(prefix)+1].getValue() commandMessage = RepoCommandParameterMessage() ProtobufTlv.decode(commandMessage, commandParams) dataName = Name() fullSchemaName = Name() for component in commandMessage.command.name.components: fullSchemaName.append(component) if component == '_': continue dataName.append(component) self.log.info("Insert request for {}".format(dataName)) responseMessage.response.status_code = 100 processId = self.currentProcessId self.currentProcessId += 1 responseMessage.response.process_id = processId else: responseMessage.response.status_code = 403 responseData = Data(interestName) responseData.setContent(ProtobufTlv.encode(responseMessage)) transport.send(responseData.wireEncode().buf()) # now send the interest out to the publisher # TODO: pendingProcesses becomes list of all processes as objects i = Interest(dataName) i.setChildSelector(1) i.setInterestLifetimeMilliseconds(4000) try: self.pendingProcesses[processId] = (dataName, 100) except NameError: pass # wasn't valid insert request else: self._insertFace.expressInterest(i, self._onInsertionDataReceived, self._onInsertionDataTimeout)
class RegisterSongList(object): def __init__(self, prefix="/ndn/edu/ucla/remap/music/list"): logging.basicConfig() self.device = "PC1" self.deviceComponent = Name.Component(self.device) self.excludeDevice = None self.prefix = Name(prefix) self.changePrefix = Name("/ndn/edu/ucla/remap/music/storage") self.keychain = KeyChain() self.certificateName = self.keychain.getDefaultCertificateName() self.address = "" self._isStopped = True def start(self): print "reg start" self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop,self.address) self.face.setCommandSigningInfo(self.keychain,self.certificateName) self.face.registerPrefix(self.prefix,self.onInterest,self.onRegisterFailed) self._isStopped = False self.face.stopWhen(lambda:self._isStopped) try: self.loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self.loop.close() self.face.shutdown() self.face = None sys.exit(1) def signData(self,data): data.setSignature(Sha256WithRsaSignature()) def onInterest(self, prefix, interest, transport, registeredPrefixId): print "received interest" initInterest = Name(interest.getName()) print "interest name:",initInterest.toUri() d = Data(interest.getName().append(self.deviceComponent)) try: print "start to set data's content" currentString = ','.join(currentList) d.setContent("songList of " +self.device+":"+currentString+ "\n") self.face.registerPrefix(self.changePrefix,self.onInterest,self.onRegisterFailed) except KeyboardInterrupt: print "key interrupt" sys.exit(1) except Exception as e: print e d.setContent("Bad command\n") finally: self.keychain.sign(d,self.certificateName) encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) print d.getName().toUri() print d.getContent() self.loop.close() self.face.shutdown() self.face = None def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class SongHandler: def __init__(self): self._device = "PC1" self._playPrefix = Name("/ndn/edu/ucla/remap/music/play") self.prefix = self._playPrefix.append(self._device) self._face = None self._loop = None self.thread = None self._keyChain = KeyChain() self._certificateName = self._keyChain.getDefaultCertificateName() self._repoCommandPrefix = "/example/repo/1" self.song = "" self.ftxt = "" self.ffreq = "" self.songList = "" self.mp = MusicPlayer() self.config = RawConfigParser() self.config.read('config.cfg') self.s = LightMessenger(self.config) self.q = Queue.Queue() def start(self): self._loop = asyncio.get_event_loop() self._face = ThreadsafeFace(self._loop,"") self._face.setCommandSigningInfo(self._keyChain, self._certificateName) self._face.registerPrefix(self.prefix, self.onPlayingCommand, self.onRegisterFailed) print "after register prefix" try: self._loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self._loop.close() self._face.shutdown() sys.exit(1) def playFunction(self): txt = open(self.ftxt) print "open txt successfully",self.ftxt #collect the onset duration osDur = [0.0] freq = [] data = [float(line.split()[0])for line in txt] for i in data: osDur.append(i) txt.close() txt = open(self.ffreq) print "open txt successfully",self.ffreq data = [float(line.split()[1])for line in txt] print "dasfdaaaa" for j in data: freq.append(j) avefq = int(sum(freq)/len(freq)) print avefq txt.close() g=(avefq-100)/10 r=avefq/30 b=(100-avefq)/10 startingColors = [int((15+r)/1.5),int((10+g)/1.5), int((10+b)/1.5)] for i in range(0,3): if startingColors[i]<0: startingColors[i]=6 #startingColors = [5,5,5] self.q.put(self.song+str("-music.mp3") ) print "MusicPlayer.isPlaying",MusicPlayer.isPlaying if not MusicPlayer.isPlaying: self.thread.start() #MusicPlayer.isPlaying = True self.s.start(osDur,startingColors) def getOnset(self): print "getonset" otxt = self.song+str("-o") print otxt g = GetFile(self._repoCommandPrefix, otxt, self._face, self.getFreq) g.oStream = open(self.song+str("-os.txt"),'wb') g.start() def getFreq(self): print "getfreq" ftxt = self.song+str("-f") g = GetFile(self._repoCommandPrefix, ftxt, self._face, self.playFunction) g.oStream = open(self.song+str("-freq.txt"),'wb') g.start() def signData(self, data): data.setSignature(Sha256WithRsaSignature()) def onPlayingCommand(self, prefix, interest, transport, prefixId): print "receive interest" interestName = Name(interest.getName()) commandComponent = interest.getName().get(self.prefix.size()) if commandComponent == Name.Component("stop"): pass if commandComponent == Name.Component("play"): pass else: songName = commandComponent.toEscapedString() print songName songList = [] songList = songName.split('%2C') print "songlist and its len",songList,len(songList) for i in songList: self.song = i fmusic = i+str("-music.mp3") self.ftxt = i + str("-os.txt") self.ffreq = i + str("-freq.txt") print "FMUSIC:",fmusic self.thread = threading.Thread(target = self.mp.play_music, args = (fmusic,songList,self.q,)) self.thread.daemon = True g = GetFile(self._repoCommandPrefix, i, self._face, self.getOnset) #g = GetFile(self._repoCommandPrefix, ftxt, self._face, self.lightFunction) g.oStream = open(fmusic,'wb') g.start() d = Data(interest.getName()) d.setContent("start to play: " + songName + "\n") encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()
class RegisterSongList(object): def __init__(self, prefix="/ndn/edu/ucla/remap/music/list"): logging.basicConfig() self.device = "PC1" self.deviceComponent = Name.Component(self.device) self.excludeDevice = None self.prefix = Name(prefix) self.changePrefix = Name("/ndn/edu/ucla/remap/music/storage") self.keychain = KeyChain() self.certificateName = self.keychain.getDefaultCertificateName() self.address = "" self._isStopped = True def start(self): print "reg start" self.loop = asyncio.get_event_loop() self.face = ThreadsafeFace(self.loop, self.address) self.face.setCommandSigningInfo(self.keychain, self.certificateName) self.face.registerPrefix(self.prefix, self.onInterest, self.onRegisterFailed) self._isStopped = False self.face.stopWhen(lambda: self._isStopped) try: self.loop.run_forever() except KeyboardInterrupt: sys.exit() finally: self.stop() def stop(self): self.loop.close() self.face.shutdown() self.face = None sys.exit(1) def signData(self, data): data.setSignature(Sha256WithRsaSignature()) def onInterest(self, prefix, interest, transport, registeredPrefixId): print "received interest" initInterest = Name(interest.getName()) print "interest name:", initInterest.toUri() d = Data(interest.getName().append(self.deviceComponent)) try: print "start to set data's content" currentString = ','.join(currentList) d.setContent("songList of " + self.device + ":" + currentString + "\n") self.face.registerPrefix(self.changePrefix, self.onInterest, self.onRegisterFailed) except KeyboardInterrupt: print "key interrupt" sys.exit(1) except Exception as e: print e d.setContent("Bad command\n") finally: self.keychain.sign(d, self.certificateName) encodedData = d.wireEncode() transport.send(encodedData.toBuffer()) print d.getName().toUri() print d.getContent() self.loop.close() self.face.shutdown() self.face = None def onRegisterFailed(self, prefix): self.log.error("Could not register " + prefix.toUri()) self.stop()