Пример #1
0
 def _make_and_send_request(self, contact_header, route_header, timeout, do_register):
     notification_center = NotificationCenter()
     prev_request = self._current_request or self._last_request
     if prev_request is not None:
         call_id = prev_request.call_id
         cseq = prev_request.cseq + 1
     else:
         call_id = None
         cseq = 1
     extra_headers = []
     extra_headers.append(Header("Expires", str(int(self.duration) if do_register else 0)))
     extra_headers.extend(self.extra_headers)
     request = Request("REGISTER", SIPURI(self.from_header.uri.host), self.from_header, ToHeader.new(self.from_header), route_header,
                       credentials=self.credentials, contact_header=contact_header, call_id=call_id,
                       cseq=cseq, extra_headers=extra_headers)
     notification_center.add_observer(self, sender=request)
     if self._current_request is not None:
         # we are trying to send something already, cancel whatever it is
         self._current_request.end()
         self._current_request = None
     try:
         request.send(timeout=timeout)
     except:
         notification_center.remove_observer(self, sender=request)
         raise
     self._unregistering = not do_register
     self._current_request = request
Пример #2
0
 def _make_and_send_request(self, body, route_header, timeout, do_publish):
     notification_center = NotificationCenter()
     extra_headers = []
     extra_headers.append(Header("Event", self.event))
     extra_headers.append(
         Header("Expires", str(int(self.duration) if do_publish else 0)))
     if self._last_etag is not None:
         extra_headers.append(Header("SIP-If-Match", self._last_etag))
     extra_headers.extend(self.extra_headers)
     content_type = (self.content_type if body is not None else None)
     request = Request("PUBLISH",
                       self.from_header.uri,
                       self.from_header,
                       ToHeader.new(self.from_header),
                       route_header,
                       credentials=self.credentials,
                       cseq=1,
                       extra_headers=extra_headers,
                       content_type=content_type,
                       body=body)
     notification_center.add_observer(self, sender=request)
     if self._current_request is not None:
         # we are trying to send something already, cancel whatever it is
         self._current_request.end()
         self._current_request = None
     try:
         request.send(timeout=timeout)
     except:
         notification_center.remove_observer(self, sender=request)
         raise
     self._unpublishing = not do_publish
     self._current_request = request
Пример #3
0
class Message(object):
    implements(IObserver)

    def __init__(self, from_header, to_header, route_header, content_type, body, credentials=None, extra_headers=[]):
        self._request = Request("MESSAGE", from_header, to_header, to_header.uri, route_header,
                                credentials=credentials, extra_headers=extra_headers, content_type=content_type, body=body)
        self._lock = RLock()

    from_header = property(lambda self: self._request.from_header)
    to_header = property(lambda self: self._request.to_header)
    route_header = property(lambda self: self._request.route_header)
    content_type = property(lambda self: self._request.content_type)
    body = property(lambda self: self._request.body)
    credentials = property(lambda self: self._request.credentials)
    is_sent = property(lambda self: self._request.state != "INIT")
    in_progress = property(lambda self: self._request.state == "IN_PROGRESS")

    def send(self, timeout=None):
        notification_center = NotificationCenter()
        with self._lock:
            if self.is_sent:
                raise RuntimeError("This MESSAGE was already sent")
            notification_center.add_observer(self, sender=self._request)
            try:
                self._request.send(timeout)
            except:
                notification_center.remove_observer(self, sender=self._request)

    def end(self):
        with self._lock:
            self._request.end()

    def handle_notification(self, notification):
        handler = getattr(self, '_NH_%s' % notification.name, Null)
        handler(notification)

    def _NH_SIPRequestDidSucceed(self, notification):
        with self._lock:
            if notification.data.expires:
                # this shouldn't happen really
                notification.sender.end()
            NotificationCenter().post_notification("SIPMessageDidSucceed", sender=self,
                                                   data=TimestampedNotificationData(code=notification.data.code,
                                                                                    reason=notification.data.reason))

    def _NH_SIPRequestDidFail(self, notification):
        with self._lock:
            NotificationCenter().post_notification("SIPMessageDidFail", sender=self,
                                                   data=TimestampedNotificationData(code=notification.data.code,
                                                                                    reason=notification.data.reason))

    def _NH_SIPRequestDidEnd(self, notification):
        with self._lock:
            NotificationCenter().remove_observer(self, sender=notification.sender)
 def __init__(self,
              from_header,
              to_header,
              route_header,
              content_type,
              body,
              credentials=None,
              extra_headers=[]):
     self._request = Request("MESSAGE",
                             from_header,
                             to_header,
                             to_header.uri,
                             route_header,
                             credentials=credentials,
                             extra_headers=extra_headers,
                             content_type=content_type,
                             body=body)
     self._lock = RLock()
Пример #5
0
 def __init__(self,
              from_header,
              to_header,
              route_header,
              content_type,
              body,
              credentials=None,
              extra_headers=None):
     self._request = Request(
         "MESSAGE",
         to_header.uri,
         from_header,
         to_header,
         route_header,
         credentials=credentials,
         extra_headers=extra_headers,
         content_type=content_type,
         body=body if isinstance(body, bytes) else body.encode())
     self._lock = RLock()
Пример #6
0
 def _make_and_send_request(self, contact_header, route_header, timeout,
                            do_register):
     notification_center = NotificationCenter()
     prev_request = self._current_request or self._last_request
     if prev_request is not None:
         call_id = prev_request.call_id
         cseq = prev_request.cseq + 1
     else:
         call_id = None
         cseq = 1
     extra_headers = []
     extra_headers.append(
         Header("Expires", str(int(self.duration) if do_register else 0)))
     extra_headers.extend(self.extra_headers)
     uri = SIPURI(self.from_header.uri.host)
     to_header = ToHeader.new(self.from_header)
     request = Request("REGISTER",
                       uri,
                       self.from_header,
                       to_header,
                       route_header,
                       credentials=self.credentials,
                       contact_header=contact_header,
                       call_id=call_id,
                       cseq=cseq,
                       extra_headers=extra_headers)
     notification_center.add_observer(self, sender=request)
     if self._current_request is not None:
         # we are trying to send something already, cancel whatever it is
         self._current_request.end()
         self._current_request = None
     try:
         request.send(timeout=timeout)
     except:
         notification_center.remove_observer(self, sender=request)
         raise
     self._unregistering = not do_register
     self._current_request = request
Пример #7
0
 def _make_and_send_request(self, body, route_header, timeout, do_publish):
     notification_center = NotificationCenter()
     extra_headers = []
     extra_headers.append(Header("Event", self.event))
     extra_headers.append(Header("Expires",  str(int(self.duration) if do_publish else 0)))
     if self._last_etag is not None:
         extra_headers.append(Header("SIP-If-Match", self._last_etag))
     extra_headers.extend(self.extra_headers)
     content_type = (self.content_type if body is not None else None)
     request = Request("PUBLISH", self.from_header, ToHeader.new(self.from_header), self.from_header.uri, route_header,
                       credentials=self.credentials, cseq=1, extra_headers=extra_headers,
                       content_type=content_type, body=body)
     notification_center.add_observer(self, sender=request)
     if self._current_request is not None:
         # we are trying to send something already, cancel whatever it is
         self._current_request.end()
         self._current_request = None
     try:
         request.send(timeout=timeout)
     except:
         notification_center.remove_observer(self, sender=request)
         raise
     self._unpublishing = not do_publish
     self._current_request = request
Пример #8
0
 def __init__(self, from_header, to_header, route_header, content_type, body, credentials=None, extra_headers=[]):
     self._request = Request("MESSAGE", from_header, to_header, to_header.uri, route_header,
                             credentials=credentials, extra_headers=extra_headers, content_type=content_type, body=body)
     self._lock = RLock()
Пример #9
0
class Message(object):
    def __init__(self,
                 from_header,
                 to_header,
                 route_header,
                 content_type,
                 body,
                 credentials=None,
                 extra_headers=None):
        self._request = Request(
            "MESSAGE",
            to_header.uri,
            from_header,
            to_header,
            route_header,
            credentials=credentials,
            extra_headers=extra_headers,
            content_type=content_type,
            body=body if isinstance(body, bytes) else body.encode())
        self._lock = RLock()

    from_header = property(lambda self: self._request.from_header)
    to_header = property(lambda self: self._request.to_header)
    route_header = property(lambda self: self._request.route_header)
    content_type = property(lambda self: self._request.content_type)
    body = property(lambda self: self._request.body)
    credentials = property(lambda self: self._request.credentials)
    is_sent = property(lambda self: self._request.state != "INIT")
    in_progress = property(lambda self: self._request.state == "IN_PROGRESS")
    peer_address = property(lambda self: self._request.peer_address)

    def send(self, timeout=None):
        notification_center = NotificationCenter()
        with self._lock:
            if self.is_sent:
                raise RuntimeError("This MESSAGE was already sent")
            notification_center.add_observer(self, sender=self._request)
            try:
                self._request.send(timeout)
            except:
                notification_center.remove_observer(self, sender=self._request)
                raise

    def handle_notification(self, notification):
        handler = getattr(self, '_NH_%s' % notification.name, Null)
        handler(notification)

    def _NH_SIPRequestDidSucceed(self, notification):
        if notification.data.expires:
            # this shouldn't happen really
            notification.sender.end()
        notification.center.post_notification('SIPMessageDidSucceed',
                                              sender=self,
                                              data=notification.data)

    def _NH_SIPRequestDidFail(self, notification):
        notification.center.post_notification('SIPMessageDidFail',
                                              sender=self,
                                              data=notification.data)

    def _NH_SIPRequestDidEnd(self, notification):
        notification.center.remove_observer(self, sender=notification.sender)
class Message(object):
    implements(IObserver)

    def __init__(self,
                 from_header,
                 to_header,
                 route_header,
                 content_type,
                 body,
                 credentials=None,
                 extra_headers=[]):
        self._request = Request("MESSAGE",
                                from_header,
                                to_header,
                                to_header.uri,
                                route_header,
                                credentials=credentials,
                                extra_headers=extra_headers,
                                content_type=content_type,
                                body=body)
        self._lock = RLock()

    from_header = property(lambda self: self._request.from_header)
    to_header = property(lambda self: self._request.to_header)
    route_header = property(lambda self: self._request.route_header)
    content_type = property(lambda self: self._request.content_type)
    body = property(lambda self: self._request.body)
    credentials = property(lambda self: self._request.credentials)
    is_sent = property(lambda self: self._request.state != "INIT")
    in_progress = property(lambda self: self._request.state == "IN_PROGRESS")

    def send(self, timeout=None):
        notification_center = NotificationCenter()
        with self._lock:
            if self.is_sent:
                raise RuntimeError("This MESSAGE was already sent")
            notification_center.add_observer(self, sender=self._request)
            try:
                self._request.send(timeout)
            except:
                notification_center.remove_observer(self, sender=self._request)

    def end(self):
        with self._lock:
            self._request.end()

    def handle_notification(self, notification):
        handler = getattr(self, '_NH_%s' % notification.name, Null)
        handler(notification)

    def _NH_SIPRequestDidSucceed(self, notification):
        with self._lock:
            if notification.data.expires:
                # this shouldn't happen really
                notification.sender.end()
            NotificationCenter().post_notification(
                "SIPMessageDidSucceed",
                sender=self,
                data=TimestampedNotificationData(
                    code=notification.data.code,
                    reason=notification.data.reason))

    def _NH_SIPRequestDidFail(self, notification):
        with self._lock:
            NotificationCenter().post_notification(
                "SIPMessageDidFail",
                sender=self,
                data=TimestampedNotificationData(
                    code=notification.data.code,
                    reason=notification.data.reason))

    def _NH_SIPRequestDidEnd(self, notification):
        with self._lock:
            NotificationCenter().remove_observer(self,
                                                 sender=notification.sender)