Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port aiosip to use the new ursine library for SIP #116

Open
wants to merge 1 commit into
base: ursine
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pyquery = "*"
aiodns = "*"
websockets = "*"
async-timeout = "*"
ursine = ">=0.2.4"

[dev-packages]
twine = "*"
Expand Down
18 changes: 17 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions aiosip/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import aiodns
from contextlib import suppress
import traceback
from ursine import URI

__all__ = ['Application']

Expand All @@ -18,8 +19,8 @@
from .protocol import UDP, TCP, WS
from .peers import UDPConnector, TCPConnector, WSConnector
from .message import Response
from .contact import Contact
from .via import Via
from .utils import get_details


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -111,8 +112,8 @@ def _create_dialog(self):
if not self.dialog:
self.dialog = peer._create_dialog(
method=msg.method,
from_details=Contact.from_header(msg.headers['To']),
to_details=Contact.from_header(msg.headers['From']),
from_details=URI(msg.headers['To']),
to_details=URI(msg.headers['From']),
call_id=call_id,
inbound=True
)
Expand All @@ -133,12 +134,15 @@ async def prepare(self, status_code, *args, **kwargs):

async def _dispatch(self, protocol, msg, addr):
call_id = msg.headers['Call-ID']
dialog = self._dialogs.get(frozenset((msg.to_details.details,
msg.from_details.details,
dialog = self._dialogs.get(frozenset((get_details(msg.to_details),
get_details(msg.from_details),
call_id)))

if dialog:
await dialog.receive_message(msg)
try:
await dialog.receive_message(msg)
except Exception:
LOG.exception("Error dispatching message in dialog")
return

# If we got an ACK, but nowhere to deliver it, drop it. If we
Expand All @@ -165,8 +169,8 @@ async def _run_dialplan(self, protocol, msg):
async def reply(*args, **kwargs):
dialog = peer._create_dialog(
method=msg.method,
from_details=Contact.from_header(msg.headers['To']),
to_details=Contact.from_header(msg.headers['From']),
from_details=URI(msg.headers['To']),
to_details=URI(msg.headers['From']),
call_id=call_id,
inbound=True
)
Expand Down
122 changes: 0 additions & 122 deletions aiosip/contact.py

This file was deleted.

19 changes: 8 additions & 11 deletions aiosip/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from async_timeout import timeout as Timeout

from . import utils
from .auth import Auth
from .message import Request, Response
from .transaction import UnreliableTransaction, ProxyTransaction

from .auth import Auth
from .utils import get_details


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -58,8 +58,8 @@ def __init__(self,

@property
def dialog_id(self):
return frozenset((self.original_msg.to_details.details,
self.original_msg.from_details.details,
return frozenset((get_details(self.original_msg.to_details),
get_details(self.original_msg.from_details),
self.call_id))

def _receive_response(self, msg):
Expand All @@ -74,7 +74,7 @@ def _receive_response(self, msg):
LOG.debug('Response without Request. The Transaction may already be closed. \n%s', msg)

def _prepare_request(self, method, contact_details=None, headers=None, payload=None, cseq=None, to_details=None):
self.from_details.add_tag()
self.from_details = self.from_details.with_tag()
if not cseq:
self.cseq += 1

Expand Down Expand Up @@ -211,7 +211,7 @@ async def reply(self, request, status_code, status_message=None, payload=None, h

def _prepare_response(self, request, status_code, status_message=None, payload=None, headers=None,
contact_details=None):
self.from_details.add_tag()
self.from_details = self.from_details.with_tag()

if contact_details:
self.contact_details = contact_details
Expand Down Expand Up @@ -273,9 +273,7 @@ async def receive_message(self, msg):
return await self._receive_request(msg)

async def _receive_request(self, msg):
if 'tag' in msg.from_details['params']:
self.to_details['params']['tag'] = msg.from_details['params']['tag']

self.to_details = self.to_details.with_tag(msg.from_details.tag)
await self._incoming.put(msg)
self._maybe_close(msg)

Expand Down Expand Up @@ -385,8 +383,7 @@ async def handle_completed_state(msg):
return await self._receive_request(msg)

async def _receive_request(self, msg):
if 'tag' in msg.from_details['params']:
self.to_details['params']['tag'] = msg.from_details['params']['tag']
self.to_details = self.to_details.with_tag(msg.from_details.tag)

if msg.method == 'BYE':
self._closed = True
Expand Down
20 changes: 10 additions & 10 deletions aiosip/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import re
import uuid

from pyquery import PyQuery
from multidict import CIMultiDict
from pyquery import PyQuery
from ursine import URI

from . import utils
from .contact import Contact
from .auth import Auth

FIRST_LINE_PATTERN = {
Expand Down Expand Up @@ -54,8 +54,8 @@ def __init__(self,

if 'Via' not in self.headers:
self.headers['Via'] = 'SIP/2.0/%(protocol)s ' + \
utils.format_host_and_port(self.contact_details['uri']['host'],
self.contact_details['uri']['port']) + \
utils.format_host_and_port(self.contact_details.host,
self.contact_details.port) + \
';branch=%s' % utils.gen_branch(10)

@property
Expand All @@ -75,7 +75,7 @@ def payload(self, payload):
@property
def from_details(self):
if not hasattr(self, '_from_details'):
self._from_details = Contact.from_header(self.headers['From'])
self._from_details = URI(self.headers['From'])
return self._from_details

@from_details.setter
Expand All @@ -85,7 +85,7 @@ def from_details(self, from_details):
@property
def to_details(self):
if not hasattr(self, '_to_details'):
self._to_details = Contact.from_header(self.headers['To'])
self._to_details = URI(self.headers['To'])
return self._to_details

@to_details.setter
Expand All @@ -96,7 +96,7 @@ def to_details(self, to_details):
def contact_details(self):
if not hasattr(self, '_contact_details'):
if 'Contact' in self.headers:
self._contact_details = Contact.from_header(self.headers['Contact'])
self._contact_details = URI(self.headers['Contact'])
else:
self._contact_details = None
return self._contact_details
Expand Down Expand Up @@ -259,7 +259,7 @@ def __init__(self,
if not first_line:
self._first_line = FIRST_LINE_PATTERN['request']['str'].format(
method=self.method,
to_uri=str(self.to_details['uri'].short_uri())
to_uri=self.to_details.short_uri()
)
else:
self._first_line = first_line
Expand All @@ -276,14 +276,14 @@ def auth(self):
@property
def to_details(self):
if not hasattr(self, '_to_details'):
self._to_details = Contact.from_header(self.headers['To'])
self._to_details = URI(self.headers['To'])
return self._to_details

@to_details.setter
def to_details(self, to_details):
self._to_details = to_details
self._first_line = FIRST_LINE_PATTERN['request']['str'].format(method=self.method,
to_uri=str(self._to_details['uri'].short_uri()))
to_uri=str(self._to_details.short_uri()))

def __str__(self):
return '%s%s%s' % (self._first_line, utils.EOL, super().__str__())
Expand Down
Loading