コード例 #1
0
 def _reconnect(self, address, bind_address=None):
     self.address = address
     if bind_address != self.uopts.laddress:
         self.uopts.laddress = bind_address
         self.worker.shutdown()
         self.worker = Udp_server(self.global_config, self.uopts)
         self.delay_flt = recfilter(0.95, 0.25)
コード例 #2
0
ファイル: Rtp_proxy_client_udp.py プロジェクト: sippy/b2bua
 def _reconnect(self, address, bind_address = None):
     self.address = address
     if bind_address != self.uopts.laddress:
         self.uopts.laddress = bind_address
         self.worker.shutdown()
         self.worker = Udp_server(self.global_config, self.uopts)
         self.delay_flt = recfilter(0.95, 0.25)
コード例 #3
0
 def __init__(self,
              global_config,
              address,
              bind_address=None,
              family=AF_INET,
              nworkers=None):
     #print('Rtp_proxy_client_udp(family=%s)' % family)
     self.address = self.getdestbyaddr(address, family)
     self.is_local = False
     self.uopts = Udp_server_opts(bind_address, self.process_reply, family)
     self.uopts.flags = 0
     self.uopts.ploss_out_rate = self.ploss_out_rate
     self.uopts.pdelay_out_max = self.pdelay_out_max
     if nworkers != None:
         self.uopts.nworkers = nworkers
     self.worker = Udp_server(global_config, self.uopts)
     self.pending_requests = {}
     self.global_config = global_config
     self.delay_flt = recfilter(0.95, 0.25)
コード例 #4
0
 def __init__(self, global_config, name, address = '/var/run/rtpproxy.sock'):
     self.active = []
     self.pending = []
     if len(address) == 2:
         self.ccm = Udp_server(address, self.up_command_udp)
     else:
         self.ccm = Cli_server_local(self.up_command, address, (80, 80))
         self.ccm.protocol.expect_lf = False
     self.global_config = global_config
     self.name = name
     self.address = address
コード例 #5
0
ファイル: Rtp_proxy_client_udp.py プロジェクト: sippy/b2bua
 def __init__(self, global_config, address, bind_address = None, family = AF_INET, nworkers = None):
     #print('Rtp_proxy_client_udp(family=%s)' % family)
     self.address = self.getdestbyaddr(address, family)
     self.is_local = False
     self.uopts = Udp_server_opts(bind_address, self.process_reply, family)
     self.uopts.flags = 0
     self.uopts.ploss_out_rate = self.ploss_out_rate
     self.uopts.pdelay_out_max = self.pdelay_out_max
     if nworkers != None:
         self.uopts.nworkers = nworkers
     self.worker = Udp_server(global_config, self.uopts)
     self.pending_requests = {}
     self.global_config = global_config
     self.delay_flt = recfilter(0.95, 0.25)
コード例 #6
0
ファイル: IoTUAC.py プロジェクト: sippy/b2bua
 def sess_started(self):
     print('started')
     self.ua = UA(self.global_config, event_cb = self.recvEvent, \
       nh_address = tuple(self.global_config['nh_addr']), disc_cbs = (self.sess_term,))
     self.ua.username = self.authname
     self.ua.password = self.authpass
     rtp_laddr = local4remote(self.global_config['nh_addr'][0])
     rserv_opts = Udp_server_opts((rtp_laddr, 0), self.rtp_received)
     rserv_opts.nworkers = 1
     self.rserv = Udp_server({}, rserv_opts)
     sect = self.body.content.sections[0]
     sect.c_header.addr = self.rserv.uopts.laddress[0]
     sect.m_header.port = self.rserv.uopts.laddress[1]
     self.body.content.o_header = SdpOrigin()
     event = CCEventTry((SipCallId(), SipCiscoGUID(), self.cli, self.cld, self.body, \
       None, 'PEL 150-2'))
     self.rgen = RTPGen()
     self.ua.recvEvent(event)
     return (self.rgen.enqueue)
コード例 #7
0
ファイル: Rtp_cluster.py プロジェクト: lindows2008/b2bua
 def __init__(self, global_config, name, address = '/var/run/rtpproxy.sock', dry_run = False):
     self.active = []
     self.pending = []
     self.l1rcache = {}
     self.l2rcache = {}
     if len(address) == 2:
         if not dry_run:
             uopts = Udp_server_opts(address, self.up_command_udp)
             self.ccm = Udp_server(global_config, uopts)
     else:
         sown = global_config.get('_rtpc_sockowner', None)
         if not dry_run:
             self.ccm = Cli_server_local(self.up_command, address, sown)
             self.ccm.protocol.expect_lf = False
     self.global_config = global_config
     self.name = name
     self.address = address
     self.commands_inflight = []
     self.cache_purge_el = Timeout(self.rCachePurge, 10, -1)
コード例 #8
0
ファイル: Rtp_cluster.py プロジェクト: radicaldrew/b2bua
 def __init__(self,
              global_config,
              name,
              address='/var/run/rtpproxy.sock',
              dry_run=False):
     self.active = []
     self.pending = []
     if len(address) == 2:
         if not dry_run:
             self.ccm = Udp_server(global_config, address,
                                   self.up_command_udp)
     else:
         sown = global_config.get('_rtpc_sockowner', None)
         if not dry_run:
             self.ccm = Cli_server_local(self.up_command, address, sown)
             self.ccm.protocol.expect_lf = False
     self.global_config = global_config
     self.name = name
     self.address = address
     self.commands_inflight = []
コード例 #9
0
 def recvEvent(self, event, ua):
     if isinstance(event, CCEventTry):
         cId, cGUID, cli, cld, sdp_body, auth, caller_name = event.getData()
         if sdp_body == None:
             return
         sdp_body.parse()
         sect = sdp_body.content.sections[0]
         rtp_target = (sect.c_header.addr, sect.m_header.port)
         rtp_laddr = local4remote(rtp_target[0])
         rserv_opts = Udp_server_opts((rtp_laddr, 0), self.rtp_received)
         rserv_opts.nworkers = 1
         self.rserv = Udp_server({}, rserv_opts)
         self.rgen = RTPGen()
         self.rgen.start(self.rserv, rtp_target)
         sect = self.body.content.sections[0]
         sect.c_header.addr = self.rserv.uopts.laddress[0]
         sect.m_header.port = self.rserv.uopts.laddress[1]
         self.body.content.o_header = SdpOrigin()
         oevent = CCEventConnect((200, 'OK', self.body))
         ua.recvEvent(oevent)
         return
コード例 #10
0
 def __init__(self, global_config, name, address = '/var/run/rtpproxy.sock', \
   dnconfig = None, dry_run = False):
     self.active = []
     self.pending = []
     self.l1rcache = {}
     self.l2rcache = {}
     if len(address) == 2:
         if not dry_run:
             uopts = Udp_server_opts(address, self.up_command_udp)
             self.ccm = Udp_server(global_config, uopts)
     else:
         sown = global_config.get('_rtpc_sockowner', None)
         if not dry_run:
             self.ccm = CLIConnectionManager(self.up_command, address, sown)
     self.global_config = global_config
     self.name = name
     self.address = address
     self.commands_inflight = []
     self.cache_purge_el = TimeoutInact(self.rCachePurge, 10, -1)
     self.cache_purge_el.spread_runs(0.1)
     self.cache_purge_el.go()
     self.update_dnrelay(dnconfig)
コード例 #11
0
ファイル: IoTUAC.py プロジェクト: sippy/b2bua
class IoTUAC(object):
    global_config = None
    ua = None
    cli = 'pel150_uac'
    cld = 'pel150_uas'
    authname = None
    authpass = None
    body = None
    rgen = None
    rserv = None

    def __init__(self, global_config):
        self.global_config = global_config
        SipTransactionManager.nworkers_udp = 1
        global_config['_sip_tm'] = SipTransactionManager(global_config)
        self.body = MsgBody(body_txt)
        self.body.parse()

    def sess_started(self):
        print('started')
        self.ua = UA(self.global_config, event_cb = self.recvEvent, \
          nh_address = tuple(self.global_config['nh_addr']), disc_cbs = (self.sess_term,))
        self.ua.username = self.authname
        self.ua.password = self.authpass
        rtp_laddr = local4remote(self.global_config['nh_addr'][0])
        rserv_opts = Udp_server_opts((rtp_laddr, 0), self.rtp_received)
        rserv_opts.nworkers = 1
        self.rserv = Udp_server({}, rserv_opts)
        sect = self.body.content.sections[0]
        sect.c_header.addr = self.rserv.uopts.laddress[0]
        sect.m_header.port = self.rserv.uopts.laddress[1]
        self.body.content.o_header = SdpOrigin()
        event = CCEventTry((SipCallId(), SipCiscoGUID(), self.cli, self.cld, self.body, \
          None, 'PEL 150-2'))
        self.rgen = RTPGen()
        self.ua.recvEvent(event)
        return (self.rgen.enqueue)

    def sess_term(self, ua, rtime, origin, result = 0):
        print('disconnected', origin)
        if origin == 'IoTUAC':
            return
        self.rgen.suspend()
        self.ua = UA(self.global_config, event_cb = self.recvEvent, \
          nh_address = tuple(self.global_config['nh_addr']), disc_cbs = (self.sess_term,))
        self.ua.username = self.authname
        self.ua.password = self.authpass
        self.body.content.o_header = SdpOrigin()
        event = CCEventTry((SipCallId(), SipCiscoGUID(), self.cli, self.cld, self.body, \
          None, 'PEL 150-2'))
        self.ua.recvEvent(event)

    def sess_ended(self):
        print('ended')
        event = CCEventDisconnect(origin = 'IoTUAC')
        self.ua.recvEvent(event)
        self.rgen.stop()
        self.rserv.shutdown()
        self.rgen = None

    def rtp_received(self, data, address, udp_server, rtime):
        pass

    def recvEvent(self, event, ua):
        print('recvEvent', event, ua)
        if isinstance(event, CCEventRing) or isinstance(event, CCEventConnect) or \
          isinstance(event, CCEventPreConnect):
            code, reason, sdp_body = event.getData()
            if sdp_body == None:
                return
            sdp_body.parse()
            sect = sdp_body.content.sections[0]
            rtp_target = (sect.c_header.addr, sect.m_header.port)
            self.rgen.start(self.rserv, rtp_target)
コード例 #12
0
ファイル: Rtp_proxy_client_udp.py プロジェクト: sippy/b2bua
class Rtp_proxy_client_udp(Rtp_proxy_client_net):
    pending_requests = None
    is_local = False
    worker = None
    uopts = None
    global_config = None
    delay_flt = None
    ploss_out_rate = 0.0
    pdelay_out_max = 0.0
    sock_type = SOCK_DGRAM

    def __init__(self, global_config, address, bind_address = None, family = AF_INET, nworkers = None):
        #print('Rtp_proxy_client_udp(family=%s)' % family)
        self.address = self.getdestbyaddr(address, family)
        self.is_local = False
        self.uopts = Udp_server_opts(bind_address, self.process_reply, family)
        self.uopts.flags = 0
        self.uopts.ploss_out_rate = self.ploss_out_rate
        self.uopts.pdelay_out_max = self.pdelay_out_max
        if nworkers != None:
            self.uopts.nworkers = nworkers
        self.worker = Udp_server(global_config, self.uopts)
        self.pending_requests = {}
        self.global_config = global_config
        self.delay_flt = recfilter(0.95, 0.25)

    def send_command(self, command, result_callback = None, *callback_parameters):
        entropy = str(random()) + str(time())
        cookie = md5(entropy.encode()).hexdigest()
        next_retr = self.delay_flt.lastval * 4.0
        exp_time = 3.0
        if isinstance(command, Rtp_proxy_cmd):
            if command.type == 'I':
                exp_time = 10.0
            if command.type == 'G':
                exp_time = 1.0
            nretr = command.nretr
            command = str(command)
        else:
            if command.startswith('I'):
                exp_time = 10.0
            elif command.startswith('G'):
                exp_time = 1.0
            nretr = None
        if nretr == None:
            nretr = getnretrans(next_retr, exp_time)
        command = '%s %s' % (cookie, command)
        timer = Timeout(self.retransmit, next_retr, 1, cookie)
        preq = Rtp_proxy_pending_req(next_retr, nretr - 1, timer, command, \
          result_callback, callback_parameters)
        self.worker.send_to(command, self.address)
        self.pending_requests[cookie] = preq

    def retransmit(self, cookie):
        preq = self.pending_requests[cookie]
        #print('command to %s timeout %s cookie %s triesleft %d' % (str(self.address), preq.command, cookie, preq.triesleft))
        if preq.triesleft <= 0 or self.worker == None:
            del self.pending_requests[cookie]
            self.go_offline()
            if preq.result_callback != None:
                preq.result_callback(None, *preq.callback_parameters)
            return
        preq.retransmits += 1
        preq.next_retr *= 2
        preq.timer = Timeout(self.retransmit, preq.next_retr, 1, cookie)
        self.worker.send_to(preq.command, self.address)
        preq.triesleft -= 1

    def go_offline(self):
        # To be replaced in the upper level class
        pass

    def process_reply(self, data, address, worker, rtime):
        try:
            cookie, result = data.split(None, 1)
        except:
            print('Rtp_proxy_client_udp.process_reply(): invalid response from %s: "%s"' % \
              (str(address), data))
            return
        cookie = cookie.decode()
        preq = self.pending_requests.pop(cookie, None)
        if preq == None:
            return
        preq.timer.cancel()
        if rtime <= preq.stime:
            # MonoTime as the name suggests is supposed to be monotonic,
            # so if we get response earlier than request went out something
            # is very wrong. Fail immediately.
            rtime_fix = MonoTime()
            raise AssertionError('cookie=%s: MonoTime stale/went' \
              ' backwards (%f <= %f, now=%f)' % (cookie, rtime.monot, \
              preq.stime.monot, rtime_fix.monot))
        if preq.result_callback != None:
            result = result.decode()
            preq.result_callback(result.strip(), *preq.callback_parameters)

        # When we had to do retransmit it is not possible to figure out whether
        # or not this reply is related to the original request or one of the
        # retransmits. Therefore, using it to estimate delay could easily produce
        # bogus value that is too low or even negative if we cook up retransmit
        # while the original response is already in the queue waiting to be
        # processed. This should not be a big issue since UDP command channel does
        # not work very well if the packet loss goes to more than 30-40%.
        if preq.retransmits == 0:
            self.delay_flt.apply(rtime - preq.stime)
            #print('Rtp_proxy_client_udp.process_reply(): delay %f' % (rtime - preq.stime))

    def reconnect(self, address, bind_address = None):
        #print('reconnect', address)
        address = self.getdestbyaddr(address, self.uopts.family)
        self.rtpp_class._reconnect(self, address, bind_address)

    def _reconnect(self, address, bind_address = None):
        self.address = address
        if bind_address != self.uopts.laddress:
            self.uopts.laddress = bind_address
            self.worker.shutdown()
            self.worker = Udp_server(self.global_config, self.uopts)
            self.delay_flt = recfilter(0.95, 0.25)

    def shutdown(self):
        self.worker.shutdown()
        self.worker = None

    def get_rtpc_delay(self):
        return self.delay_flt.lastval
コード例 #13
0
class Rtp_proxy_client_udp(Rtp_proxy_client_net):
    pending_requests = None
    is_local = False
    worker = None
    uopts = None
    global_config = None
    delay_flt = None
    ploss_out_rate = 0.0
    pdelay_out_max = 0.0
    sock_type = SOCK_DGRAM

    def __init__(self,
                 global_config,
                 address,
                 bind_address=None,
                 family=AF_INET,
                 nworkers=None):
        #print('Rtp_proxy_client_udp(family=%s)' % family)
        self.address = self.getdestbyaddr(address, family)
        self.is_local = False
        self.uopts = Udp_server_opts(bind_address, self.process_reply, family)
        self.uopts.flags = 0
        self.uopts.ploss_out_rate = self.ploss_out_rate
        self.uopts.pdelay_out_max = self.pdelay_out_max
        if nworkers != None:
            self.uopts.nworkers = nworkers
        self.worker = Udp_server(global_config, self.uopts)
        self.pending_requests = {}
        self.global_config = global_config
        self.delay_flt = recfilter(0.95, 0.25)

    def send_command(self,
                     command,
                     result_callback=None,
                     *callback_parameters):
        entropy = str(random()) + str(time())
        cookie = md5(entropy.encode()).hexdigest()
        next_retr = self.delay_flt.lastval * 4.0
        exp_time = 3.0
        if isinstance(command, Rtp_proxy_cmd):
            if command.type == 'I':
                exp_time = 10.0
            if command.type == 'G':
                exp_time = 1.0
            nretr = command.nretr
            command = str(command)
        else:
            if command.startswith('I'):
                exp_time = 10.0
            elif command.startswith('G'):
                exp_time = 1.0
            nretr = None
        if nretr == None:
            nretr = getnretrans(next_retr, exp_time)
        command = '%s %s' % (cookie, command)
        timer = Timeout(self.retransmit, next_retr, 1, cookie)
        preq = Rtp_proxy_pending_req(next_retr, nretr - 1, timer, command, \
          result_callback, callback_parameters)
        self.worker.send_to(command, self.address)
        self.pending_requests[cookie] = preq

    def retransmit(self, cookie):
        preq = self.pending_requests[cookie]
        #print('command to %s timeout %s cookie %s triesleft %d' % (str(self.address), preq.command, cookie, preq.triesleft))
        if preq.triesleft <= 0 or self.worker == None:
            del self.pending_requests[cookie]
            self.go_offline()
            if preq.result_callback != None:
                preq.result_callback(None, *preq.callback_parameters)
            return
        preq.retransmits += 1
        preq.next_retr *= 2
        preq.timer = Timeout(self.retransmit, preq.next_retr, 1, cookie)
        self.worker.send_to(preq.command, self.address)
        preq.triesleft -= 1

    def go_offline(self):
        # To be replaced in the upper level class
        pass

    def process_reply(self, data, address, worker, rtime):
        try:
            cookie, result = data.split(None, 1)
        except:
            print('Rtp_proxy_client_udp.process_reply(): invalid response from %s: "%s"' % \
              (str(address), data))
            return
        cookie = cookie.decode()
        preq = self.pending_requests.pop(cookie, None)
        if preq == None:
            return
        preq.timer.cancel()
        if rtime <= preq.stime:
            # MonoTime as the name suggests is supposed to be monotonic,
            # so if we get response earlier than request went out something
            # is very wrong. Fail immediately.
            rtime_fix = MonoTime()
            raise AssertionError('cookie=%s: MonoTime stale/went' \
              ' backwards (%f <= %f, now=%f)' % (cookie, rtime.monot, \
              preq.stime.monot, rtime_fix.monot))
        if preq.result_callback != None:
            result = result.decode()
            preq.result_callback(result.strip(), *preq.callback_parameters)

        # When we had to do retransmit it is not possible to figure out whether
        # or not this reply is related to the original request or one of the
        # retransmits. Therefore, using it to estimate delay could easily produce
        # bogus value that is too low or even negative if we cook up retransmit
        # while the original response is already in the queue waiting to be
        # processed. This should not be a big issue since UDP command channel does
        # not work very well if the packet loss goes to more than 30-40%.
        if preq.retransmits == 0:
            self.delay_flt.apply(rtime - preq.stime)
            #print('Rtp_proxy_client_udp.process_reply(): delay %f' % (rtime - preq.stime))

    def reconnect(self, address, bind_address=None):
        #print('reconnect', address)
        address = self.getdestbyaddr(address, self.uopts.family)
        self.rtpp_class._reconnect(self, address, bind_address)

    def _reconnect(self, address, bind_address=None):
        self.address = address
        if bind_address != self.uopts.laddress:
            self.uopts.laddress = bind_address
            self.worker.shutdown()
            self.worker = Udp_server(self.global_config, self.uopts)
            self.delay_flt = recfilter(0.95, 0.25)

    def shutdown(self):
        self.worker.shutdown()
        self.worker = None

    def get_rtpc_delay(self):
        return self.delay_flt.lastval