def send_response(self, recv_frame, response_code, sdp=None): send_frame = recv_frame.copy() send_frame.set_response(self.frame, response_code, sdp) event.put('send_response', ( send_frame, self.server_address, ))
def retry(self, params): recv_frame = params[0] self.retry_count += 1 if not 1 == self.retry_count: return rd = self.frame.get('remote_domainname') rp = self.frame.get('remote_port') authorization_config = recv_frame.get('authenticate') authorization_config.update({ 'method': 'REGISTER', 'username': self.frame.get('local_username'), 'password': self.frame.get('password'), 'uri': f'sip:{rd}:{rp}', }) authorization = lib.build_authorization(authorization_config) local_cseq_number = self.frame.get('local_cseq_number') self.frame.set('local_cseq_number', local_cseq_number + 1) send_frame = self.frame.copy() send_frame.update({ 'branch': f';branch=z9hG4bK{lib.key(10)}', 'local_tag': f';tag={lib.key(36)}', 'authorization': authorization, 'add_header': {'Authorization', 'Expires'}, 'body': '', }) event.put('send_request', ( send_frame, self.server_address, ))
def send_request(self, method, sdp=None, authorization=None, branch=True): if branch: self.frame.set('branch', f';branch=z9hG4bK{lib.key(10)}'), send_frame = self.frame.copy() send_frame.set_request(self.frame, method, sdp, authorization) event.put('send_request', ( send_frame, self.server_address, ))
def test_event_once(): expected = [('stub1', ('first', ))] event.init() event.regist('a', stub1) event.put('a', ('first', )) event.exec() assert result == expected
def test_event_dual(): result.clear() expected = [('stub1', ('first', )), ('stub1', ('second', ))] event.init() event.regist('a', stub1) event.put('a', ('first', )) event.put('a', ('second', )) event.exec() event.exec() assert result == expected
def idle__regist(self, params): local_cseq_number = self.frame.get('local_cseq_number') self.frame.set('local_cseq_number', local_cseq_number + 1) send_frame = self.frame.copy() send_frame.update({ 'branch': f';branch=z9hG4bK{lib.key(10)}', 'local_tag': f';tag={lib.key(36)}', 'body': '', }) event.put('send_request', ( send_frame, self.server_address, )) self.to_trying()
def trying__recv_response(self, params): recv_frame = params[0] if not 'REGISTER' == recv_frame.get('method'): return response_code = recv_frame.get('response_code') if 200 == response_code: self.retry_count = 0 expires = recv_frame.get('Expires') if expires: self.frame.set('expires', int(expires)) event.put('timer', delay=int(expires) // 2, send_to=self.exec) self.to_registered() if 401 == response_code: self.retry(params)
def cmd(): c = input() if 'a' == c: event.put('answer') elif 'h' == c: event.put('hangup') elif 'm' == c: event.put('new_call') elif '+a' == c: event.put('regist') elif 'q' == c: return False return True
def registered__timer(self, params): event.put('regist') self.to_idle()
def sip_recv(event_id, params): message, address = params frame = SipFrame(message) event.put(f'recv_{frame.get("kind")}', (frame, ))
def sip_send(event_id, params): frame, address = params event.put('send_packet', (frame.to_message(), address))
def recv(): while True: message, address = sock.recvfrom(1024) message = message.decode() event.put('recv_packet', (message, address))
def update(self, event): event.put()