@@ -127,7 +127,16 @@ async def run(self):
127127 # if not hasattr(self, 'on_event'):
128128 # raise NotImplementedError("You must implement on_event.")
129129 self .loop = asyncio .get_event_loop ()
130- await self .create_party_registration (ven_id = self .ven_id )
130+
131+ request_id = None
132+ response_type , response_payload = await self .query_registration ()
133+ if 'registration_id' in response_payload :
134+ self .registration_id = response_payload ['registration_id' ]
135+ if response_payload and 'response' in response_payload and 'request_id' in response_payload ['response' ]:
136+ request_id = response_payload ['response' ]['request_id' ]
137+
138+ await self .create_party_registration (ven_id = self .ven_id , request_id = request_id )
139+
131140
132141 if not self .registration_id :
133142 logger .error ("No RegistrationID received from the VTN, aborting." )
@@ -393,7 +402,7 @@ async def query_registration(self):
393402 async def create_party_registration (self , http_pull_model = True , xml_signature = False ,
394403 report_only = False , profile_name = '2.0b' ,
395404 transport_name = 'simpleHttp' , transport_address = None ,
396- ven_id = None ):
405+ ven_id = None , request_id = None , registration_id = None ):
397406 """
398407 Take the neccessary steps to register this client with the server.
399408
@@ -406,7 +415,8 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
406415 :param str transport_address: Which public-facing address the server should use
407416 to communicate.
408417 """
409- request_id = utils .generate_id ()
418+ if request_id is None :
419+ request_id = utils .generate_id ()
410420 service = 'EiRegisterParty'
411421 payload = {'ven_name' : self .ven_name ,
412422 'ven_id' : self .ven_id ,
@@ -415,7 +425,8 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
415425 'report_only' : report_only ,
416426 'profile_name' : profile_name ,
417427 'transport_name' : transport_name ,
418- 'transport_address' : transport_address }
428+ 'transport_address' : transport_address ,
429+ 'registration_id' : registration_id }
419430
420431 message = self ._create_message ('oadrCreatePartyRegistration' ,
421432 request_id = request_id ,
@@ -455,8 +466,53 @@ async def create_party_registration(self, http_pull_model=True, xml_signature=Fa
455466 logger .info (f"The polling frequency is { self .poll_frequency } " )
456467 return response_type , response_payload
457468
469+ async def create_party_reregistration (self , registration_id = None ):
470+ """
471+ Take the neccessary steps to re-register this client with the server.
472+ """
473+
474+ await self .create_party_registration (ven_id = self .ven_id , registration_id = registration_id )
475+
476+ if not self .registration_id :
477+ logger .error ("No RegistrationID received from the VTN, aborting." )
478+ await self .stop ()
479+ return
480+
481+ await self .register_reports (self .reports )
482+ if self .reports :
483+ self .report_queue_task = self .loop .create_task (self ._report_queue_worker ())
484+
485+ # Perform initial event sync
486+ await self .sync_events ()
487+
458488 async def cancel_party_registration (self ):
459- raise NotImplementedError ("Cancel Registration is not yet implemented" )
489+ if self .registration_id is None :
490+ logger .info ("VEN is not registered" )
491+ return
492+
493+ logger .info (f"VEN is registered with registration ID { self .registration_id } and venID { self .ven_id } , trying to un-register" )
494+ request_id = utils .generate_id ()
495+ payload = {'request_id' : request_id ,
496+ 'registration_id' : self .registration_id ,
497+ 'ven_id' : self .ven_id }
498+
499+ service = 'EiRegisterParty'
500+ message = self ._create_message ('oadrCancelPartyRegistration' , ** payload )
501+ response_type , response_payload = await self ._perform_request (service , message )
502+
503+ if response_type == 'oadrCanceledPartyRegistration' and response_payload ['response' ]['response_code' ] == 200 :
504+ logger .info ("VEN successfully un-registered" )
505+ # Update/Delete all the registration and reports information
506+ self .registration_id = None
507+ self .report_requests = None
508+ self .reports = None
509+ self .report_callbacks = None
510+ self .report_requests = None
511+ self .incomplete_reports = None
512+ self .pending_reports = None
513+ self .scheduler .remove_all_jobs ()
514+ else :
515+ logger .warning ("The VEN couldn't cancel the registration" )
460516
461517 ###########################################################################
462518 # #
@@ -513,6 +569,11 @@ async def register_reports(self, reports):
513569 Tell the VTN about our reports. The VTN miht respond with an
514570 oadrCreateReport message that tells us which reports are to be sent.
515571 """
572+
573+ # When registering reports, they need to have the current time as the creation time
574+ for report in reports :
575+ report .created_date_time = datetime .now ()
576+
516577 request_id = utils .generate_id ()
517578 payload = {'request_id' : request_id ,
518579 'ven_id' : self .ven_id ,
@@ -522,6 +583,7 @@ async def register_reports(self, reports):
522583 for report in payload ['reports' ]:
523584 utils .setmember (report , 'report_request_id' , 0 )
524585
586+
525587 service = 'EiReport'
526588 message = self ._create_message ('oadrRegisterReport' , ** payload )
527589 response_type , response_payload = await self ._perform_request (service , message )
@@ -755,8 +817,31 @@ async def on_update_event(self, event):
755817 return self .responded_events .get (event ['event_descriptor' ]['event_id' ])
756818
757819 async def on_cancel_party_registration (self , message ):
820+ if self .registration_id is None :
821+ logger .info ('VEN is not registered, doing nothing' )
822+ return
823+ if 'registration_id' in message :
824+ if self .registration_id != message ['registration_id' ]:
825+ logger .info (
826+ f"Cancel request is not for us: VEN registrationID is { self .registration_id } , requested for { message ['registration_id' ]} " )
827+ response = {'response_code' : 452 ,
828+ 'response_description' : 'ERROR' ,
829+ 'request_id' : message ['request_id' ]}
830+
831+ message = self ._create_message ('oadrCanceledPartyRegistration' , response = response , ven_id = self .ven_id ,
832+ registration_id = self .registration_id )
833+ service = 'EiRegisterParty'
834+ response_type , response_payload = await self ._perform_request (service , message )
835+ logger .info (response_type , response_payload )
836+
837+ return
838+ else :
839+ response = {'response_code' : 200 ,
840+ 'response_description' : 'OK' ,
841+ 'request_id' : message ['request_id' ]}
842+ else :
843+ return
758844 # Update/Delete all the registration and reports information
759- self .registration_id = None
760845 self .report_requests = None
761846 self .reports = None
762847 self .report_callbacks = None
@@ -765,12 +850,10 @@ async def on_cancel_party_registration(self, message):
765850 self .pending_reports = None
766851 self .scheduler .remove_all_jobs ()
767852
768- response = {'response_code' : 200 ,
769- 'response_description' : 'OK' ,
770- 'request_id' : message ['request_id' ]}
771- message = self ._create_message ('oadrCanceledPartyRegistration' , response = response )
853+ message = self ._create_message ('oadrCanceledPartyRegistration' , response = response , ven_id = self .ven_id , registration_id = self .registration_id )
772854 service = 'EiRegisterParty'
773855 response_type , response_payload = await self ._perform_request (service , message )
856+ self .registration_id = None
774857 logger .info (response_type , response_payload )
775858
776859 ###########################################################################
@@ -784,6 +867,7 @@ async def send_response(self, service, response_code=200, response_description="
784867 Send an empty oadrResponse, for instance after receiving oadrRequestReregistration.
785868 """
786869 msg = self ._create_message ('oadrResponse' ,
870+ ven_id = self .ven_id ,
787871 response = {'response_code' : response_code ,
788872 'response_description' : response_description ,
789873 'request_id' : request_id })
@@ -962,6 +1046,7 @@ async def _poll(self):
9621046 "does not support reports in this direction." )
9631047 message = self ._create_message ('oadrRegisteredReport' ,
9641048 report_requests = [],
1049+ ven_id = self .ven_id ,
9651050 response = {'response_code' : 200 ,
9661051 'response_description' : 'OK' ,
9671052 'request_id' : response_payload ['request_id' ]})
0 commit comments