from threading import Timer from datetime import datetime handle = _pyccn.create() try: _pyccn.run(handle, 100) except _pyccn.CCNError: pass else: raise AssertionError("ccn_run() should fail when not connected") del handle c = CCN() c.run(0) def change_timeout(): print("Changing timeout!") c.setRunTimeout(1000) t = Timer(0.1, change_timeout) t.start() org_start = datetime.now() while True: if (datetime.now() - org_start).seconds > 3: raise AssertionError("setRunTimeout() failed")
from pyccn import CCN, Name, Interest, Closure from time import sleep worked = False class MyClosure(Closure): def upcall(self, kind, upcallInfo): global worked print("Got response") print(kind) print(upcallInfo) worked = True n = Name("ccnx:/ccnx/ping") i = Interest() closure = MyClosure() c = CCN() res = c.expressInterest(n, closure, i) print(res) #causes crashes c.run(10) print("Ha!") assert(worked)
if __name__ == "__main__": print "prefix", prefix processIncoming = ProcessIncoming() while (True): T = time.time() if T - lastdiscovertime > DISCOVER_INTEREST_PERIOD: interestDiscover.exclude = ExclusionFilter() interestDiscover.exclude.add_names( [Name([key]) for key in tyzxObjects.keys()]) interestDiscover.exclude.add_names( [Name([key]) for key in oldObjects]) ccn.expressInterest(interestDiscover.name, processIncoming, interestDiscover) lastdiscovertime = time.time() for obj in tyzxObjects.values(): if T - obj.lastinteresttime < UPDATE_INTEREST_PERIOD: continue interestUpdate.name = Name(prefix) interestUpdate.name += str(obj.id) interestUpdate.exclude = ExclusionFilter() interestUpdate.exclude.add_any() n = Name() n.components.append(versionFromTime(obj.time)) interestUpdate.exclude.add_name(n) interestUpdate.exclude.add_name(Name([last_version_marker])) interestUpdate.exclude.add_any() ccn.expressInterest(interestUpdate.name, processIncoming, interestUpdate) obj.lastinteresttime = time.time() ccn.run(CCN_WAIT_TIME_MS)
from pyccn import CCN, Name, Interest, Closure from time import sleep worked = False class MyClosure(Closure): def upcall(self, kind, upcallInfo): global worked print("Got response") print(kind) print(upcallInfo) worked = True n = Name("ccnx:/ccnx/ping") i = Interest() closure = MyClosure() c = CCN() res = c.expressInterest(n, closure, i) print(res) #causes crashes c.run(10) print("Ha!") assert (worked)
class TyzxServer(Closure): def __init__(self, prefixstr ): self.handle = CCN() #XXX: temporary, until we allow fetching key from key storage self.key = self.handle.getDefaultKey() self.keylocator = KeyLocator(self.key) self.prefix = Name(prefixstr) # member_name = Name.Name(self.members_uri) # member_name.appendKeyID(fix_digest(self.key.publicKeyID)) # self.member_message = self.publish(member_name, nick) def listen(self): #listen to requests in namespace self.handle.setInterestFilter(self.prefix, self) self.handle.run(-1) def publish(self, name, content): # Name #print name # SignedInfo si = pyccn.SignedInfo() #si.type = pyccn.CCN_CONTENT_DATA si.type = 0x0C04C0 # content type si.finalBlockID = b'\x00' si.publisherPublicKeyDigest = self.key.publicKeyID si.keyLocator = self.keylocator si.freshnessSeconds = FRESHNESS_SECONDS # ContentObject co = ContentObject() co.content = content co.name = name co.signedInfo = si co.sign(self.key) return co def upcall(self, kind, upcallInfo): global lasttime if lasttime is None: # can't answer yet return pyccn.RESULT_OK if len(tyzxObjs.objs)<1: return pyccn.RESULT_OK interest = upcallInfo.Interest #print "Interest", interest.name, time.time() # CALL content matches interest to check exclusion on versions # #print interest.exclude name = interest.name #print name if name==self.prefix: # root #print "Request for root: %s" % str(name) if interest.exclude is None: freshids = tyzxObjs.objs.keys()[0:1] else: if len(interest.exclude.components) > 0: ids = set(tyzxObjs.objs.keys())# new stuff we have suffixes = set([int(str(s)[1:]) for s in interest.exclude.components]) freshids = list(ids.difference(suffixes)) # do we need this conversion? #print "ids", ids #print "suffixes", suffixes #print "freshids", freshids else: freshids = tyzxObjs.objs.keys()[0:1] if len(freshids)>0: child = freshids[0] else: return pyccn.RESULT_OK # no new content else: # should check what we're receiving! take next component child = int(name.components[-1:][0]) # why not be able to do this on name? #print "Request for child: %s" % child, time.time() #print "child - ", child if tyzxObjs.objs.has_key(child): O = tyzxObjs.objs[child] else: # Don't want to respond with a nonexistent. NDN way is to not answer interest. # If we've just started, we may not even know what the content store knows. # But, we do want to answer "exits" that we know about. #print "child", child, "is not present" if tyzxObjs.objExits.has_key(child): #print "child", child, "is exited" O = tyzxObjs.objExits[child] else: return pyccn.RESULT_OK #O = CompositeObject(BaseObject()) #O.time = lasttime.time #O.id = int(child) #O.status = "nonexistent" msgname = Name(self.prefix) msgname += str(child) msgname.components.append(versionFromTime (O.time)) # should have msgname.append #msgname.ccn_data_dirty=True - now handled within PyCCN # need binary add component self.message = self.publish(msgname, O.toJSON()) #print "Publishing", msgname, O.toJSON() #print "Present:", tyzxObjs.objs.keys(), time.time() self.handle.put(self.message) return pyccn.RESULT_INTEREST_CONSUMED return pyccn.RESULT_OK
class TyzxServer(Closure): def __init__(self, prefixstr): self.handle = CCN() #XXX: temporary, until we allow fetching key from key storage self.key = self.handle.getDefaultKey() self.keylocator = KeyLocator(self.key) self.prefix = Name(prefixstr) # member_name = Name.Name(self.members_uri) # member_name.appendKeyID(fix_digest(self.key.publicKeyID)) # self.member_message = self.publish(member_name, nick) def listen(self): #listen to requests in namespace self.handle.setInterestFilter(self.prefix, self) self.handle.run(-1) def publish(self, name, content): # Name #print name # SignedInfo si = pyccn.SignedInfo() #si.type = pyccn.CCN_CONTENT_DATA si.type = 0x0C04C0 # content type si.finalBlockID = b'\x00' si.publisherPublicKeyDigest = self.key.publicKeyID si.keyLocator = self.keylocator si.freshnessSeconds = FRESHNESS_SECONDS # ContentObject co = ContentObject() co.content = content co.name = name co.signedInfo = si co.sign(self.key) return co def upcall(self, kind, upcallInfo): global lasttime if lasttime is None: # can't answer yet return pyccn.RESULT_OK if len(tyzxObjs.objs) < 1: return pyccn.RESULT_OK interest = upcallInfo.Interest #print "Interest", interest.name, time.time() # CALL content matches interest to check exclusion on versions # #print interest.exclude name = interest.name #print name if name == self.prefix: # root #print "Request for root: %s" % str(name) if interest.exclude is None: freshids = tyzxObjs.objs.keys()[0:1] else: if len(interest.exclude.components) > 0: ids = set(tyzxObjs.objs.keys()) # new stuff we have suffixes = set( [int(str(s)[1:]) for s in interest.exclude.components]) freshids = list(ids.difference( suffixes)) # do we need this conversion? #print "ids", ids #print "suffixes", suffixes #print "freshids", freshids else: freshids = tyzxObjs.objs.keys()[0:1] if len(freshids) > 0: child = freshids[0] else: return pyccn.RESULT_OK # no new content else: # should check what we're receiving! take next component child = int( name.components[-1:][0]) # why not be able to do this on name? #print "Request for child: %s" % child, time.time() #print "child - ", child if tyzxObjs.objs.has_key(child): O = tyzxObjs.objs[child] else: # Don't want to respond with a nonexistent. NDN way is to not answer interest. # If we've just started, we may not even know what the content store knows. # But, we do want to answer "exits" that we know about. #print "child", child, "is not present" if tyzxObjs.objExits.has_key(child): #print "child", child, "is exited" O = tyzxObjs.objExits[child] else: return pyccn.RESULT_OK #O = CompositeObject(BaseObject()) #O.time = lasttime.time #O.id = int(child) #O.status = "nonexistent" msgname = Name(self.prefix) msgname += str(child) msgname.components.append(versionFromTime( O.time)) # should have msgname.append #msgname.ccn_data_dirty=True - now handled within PyCCN # need binary add component self.message = self.publish(msgname, O.toJSON()) #print "Publishing", msgname, O.toJSON() #print "Present:", tyzxObjs.objs.keys(), time.time() self.handle.put(self.message) return pyccn.RESULT_INTEREST_CONSUMED return pyccn.RESULT_OK
for obj in tyzxObjects.values(): print obj.id,":", obj.x, obj.y, obj.z, obj.localupdatetime if __name__ == "__main__": print "prefix", prefix processIncoming = ProcessIncoming() while (True): T = time.time() if T-lastdiscovertime > DISCOVER_INTEREST_PERIOD: interestDiscover.exclude = ExclusionFilter() interestDiscover.exclude.add_names([Name([key]) for key in tyzxObjects.keys()]) interestDiscover.exclude.add_names([Name([key]) for key in oldObjects]) ccn.expressInterest(interestDiscover.name, processIncoming, interestDiscover) lastdiscovertime = time.time() for obj in tyzxObjects.values(): if T-obj.lastinteresttime < UPDATE_INTEREST_PERIOD: continue interestUpdate.name = Name(prefix) interestUpdate.name += str(obj.id) interestUpdate.exclude = ExclusionFilter() interestUpdate.exclude.add_any() n = Name() n.components.append(versionFromTime(obj.time)) interestUpdate.exclude.add_name(n) interestUpdate.exclude.add_name(Name([last_version_marker])) interestUpdate.exclude.add_any() ccn.expressInterest(interestUpdate.name, processIncoming, interestUpdate) obj.lastinteresttime = time.time() ccn.run(CCN_WAIT_TIME_MS)