small DRY refactor of joiner and joinent transactons

rnamind in stack
This commit is contained in:
Samuel M Smith 2014-02-18 14:30:53 -07:00
parent c9ab0993ec
commit 30e6250d18
3 changed files with 65 additions and 45 deletions

View File

@ -209,7 +209,7 @@ class StackUdp(object):
if (packet.data['tk'] == raeting.trnsKinds.allow and
packet.data['pk'] == raeting.pcktKinds.hello and
packet.data['si'] != 0):
self.replyEndow(packet)
self.replyAllow(packet)
def join(self):
'''
@ -233,24 +233,24 @@ class StackUdp(object):
# need to perform the check for accepted status somewhere
joinent.accept()
def endow(self, rdid=None):
def allow(self, rdid=None):
'''
Initiate endow transaction
Initiate allow transaction
'''
data = odict(hk=self.Hk, bk=raeting.bodyKinds.raw)
endower = transacting.Allower(stack=self, rdid=rdid, txData=data)
endower.hello()
allower = transacting.Allower(stack=self, rdid=rdid, txData=data)
allower.hello()
def replyEndow(self, packet):
def replyAllow(self, packet):
'''
Correspond to new endow transaction
Correspond to new allow transaction
'''
data = odict(hk=self.Hk, bk=raeting.bodyKinds.raw)
endowent = transacting.Allowent(stack=self,
allowent = transacting.Allowent(stack=self,
rdid=packet.data['sd'],
sid=packet.data['si'],
tid=packet.data['ti'],
txData=data,
rxPacket=packet)
endowent.hello()
allowent.hello()

View File

@ -41,6 +41,8 @@ def test():
prikey=masterPriKeyHex,)
stack1 = stacking.StackUdp(device=device)
print "\n********* Join Transaction **********"
stack1.join()
timer = Timer(duration=0.5)
@ -72,7 +74,10 @@ def test():
for device in stack1.devices.values():
print "Remote Device {0} joined= {1}".format(device.did, device.joined)
stack1.endow()
print "\n********* Allow Transaction **********"
stack1.allow()
timer.restart()
while not timer.expired:
stack1.serviceUdp()

View File

@ -164,6 +164,7 @@ class Joiner(Initiator):
self.rdid = self.stack.devices.values()[0].did # zeroth is channel master
self.sid = 0
self.tid = self.stack.devices[self.rdid].nextTid()
self.prep()
self.add(self.index)
def receive(self, packet):
@ -179,14 +180,10 @@ class Joiner(Initiator):
elif packet.data['pk'] == raeting.pcktKinds.response:
self.accept()
def join(self):
def prep(self):
'''
Send join request
Prepare .txData
'''
if self.rdid not in self.stack.devices:
emsg = "Invalid remote destination device id '{0}'".format(self.rdid)
raise raeting.TransactionError(emsg)
self.txData.update( sh=self.stack.device.host,
sp=self.stack.device.port,
dh=self.stack.devices[self.rdid].host,
@ -201,6 +198,14 @@ class Joiner(Initiator):
ck=raeting.coatKinds.nada,
fk=raeting.footKinds.nada)
def join(self):
'''
Send join request
'''
if self.rdid not in self.stack.devices:
emsg = "Invalid remote destination device id '{0}'".format(self.rdid)
raise raeting.TransactionError(emsg)
body = odict([('verhex', self.stack.device.signer.verhex),
('pubhex', self.stack.device.priver.pubhex)])
packet = packeting.TxPacket(stack=self.stack,
@ -274,9 +279,27 @@ class Joinent(Correspondent):
'''
kwa['kind'] = raeting.trnsKinds.join
super(Joinent, self).__init__(**kwa)
self.prep()
# Since corresponding bootstrap transaction use packet.index not self.index
self.add(self.rxPacket.index)
def prep(self):
'''
Prepare .txData
'''
#since bootstrap transaction use the reversed sdid and ddid from packet
self.txData.update( sh=self.stack.device.host,
sp=self.stack.device.port,
sd=self.rxPacket.data['dd'],
dd=self.rxPacket.data['sd'],
tk=self.kind,
cf=self.rmt,
bf=self.bcst,
si=self.sid,
ti=self.tid,
ck=raeting.coatKinds.nada,
fk=raeting.footKinds.nada,)
def join(self):
'''
Process join packet
@ -319,20 +342,9 @@ class Joinent(Correspondent):
emsg = "Invalid remote destination device id '{0}'".format(self.rdid)
raise raeting.TransactionError(emsg)
#since bootstrap transaction use the reversed sdid and ddid from packet
self.txData.update( sh=self.stack.device.host,
sp=self.stack.device.port,
dh=self.stack.devices[self.rdid].host,
dp=self.stack.devices[self.rdid].port,
sd=self.rxPacket.data['dd'],
dd=self.rxPacket.data['sd'],
tk=self.kind,
cf=self.rmt,
bf=self.bcst,
si=self.sid,
ti=self.tid,
ck=raeting.coatKinds.nada,
fk=raeting.footKinds.nada,)
#since bootstrap transaction use updated self.rid
self.txData.update( dh=self.stack.devices[self.rdid].host,
dp=self.stack.devices[self.rdid].port,)
body = odict()
packet = packeting.TxPacket(stack=self.stack,
kind=raeting.pcktKinds.ack,
@ -356,20 +368,23 @@ class Joinent(Correspondent):
raise raeting.TransactionError(emsg)
remote = self.stack.devices[self.rdid]
#self.txData.update( dh=remote.host,
#dp=remote.port,)
#since bootstrap transaction use the reversed sdid and ddid from packet
self.txData.update( sh=self.stack.device.host,
sp=self.stack.device.port,
dh=remote.host,
dp=remote.port,
sd=self.rxPacket.data['dd'],
dd=self.rxPacket.data['sd'],
tk=self.kind,
cf=self.rmt,
bf=self.bcst,
si=self.sid,
ti=self.tid,
ck=raeting.coatKinds.nada,
fk=raeting.footKinds.nada,)
#self.txData.update( sh=self.stack.device.host,
#sp=self.stack.device.port,
#dh=remote.host,
#dp=remote.port,
#sd=self.rxPacket.data['dd'],
#dd=self.rxPacket.data['sd'],
#tk=self.kind,
#cf=self.rmt,
#bf=self.bcst,
#si=self.sid,
#ti=self.tid,
#ck=raeting.coatKinds.nada,
#fk=raeting.footKinds.nada,)
body = odict([ ('ldid', self.rdid),
('rdid', self.stack.device.did),
('verhex', self.stack.device.signer.verhex),
@ -404,7 +419,7 @@ class Allower(Initiator):
self.rdid = self.stack.devices.values()[0].did # zeroth is channel master
remote = self.stack.devices[self.rdid]
if not remote.joined:
emsg = "Must be accepted first"
emsg = "Must be joined first"
raise raeting.TransactionError(emsg)
remote.refresh() # refresh short term keys and .endowed
self.sid = remote.sid
@ -567,7 +582,7 @@ class Allowent(Correspondent):
super(Allowent, self).__init__(**kwa)
remote = self.stack.devices[self.rdid]
if not remote.joined:
emsg = "Must be accepted first"
emsg = "Must be joined first"
raise raeting.TransactionError(emsg)
self.oreo = None #keep locally generated oreo around for redos
remote.refresh() # refresh short term keys and .endowed