Skip to content

merge master on drop_py27 #69

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

Merged
merged 3 commits into from
May 28, 2021
Merged
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
67 changes: 31 additions & 36 deletions pysipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# Authors : Tyler Goodlet
'''
"""
pysipp - a python wrapper for launching SIPp
'''
"""
import sys
from os.path import dirname
from . import plugin, netplug, agent
from .load import iter_scen_dirs
from .agent import client, server


__package__ = 'pysipp'
__author__ = 'Tyler Goodlet ([email protected])'
__package__ = "pysipp"
__author__ = "Tyler Goodlet ([email protected])"

__all__ = ['walk', 'client', 'server', 'plugin']
__all__ = ["walk", "client", "server", "plugin"]


def walk(rootpath, delay_conf_scen=False, autolocalsocks=True,
**scenkwargs):
def walk(rootpath, delay_conf_scen=False, autolocalsocks=True, **scenkwargs):
"""SIPp scenario generator.

Build and return scenario objects for each scenario directory.
Expand All @@ -48,23 +47,21 @@ def walk(rootpath, delay_conf_scen=False, autolocalsocks=True,
assert dirname(confpy.__file__) == path

# predicate hook based filtering
res = hooks.pysipp_load_scendir(
path=path, xmls=xmls, confpy=confpy)
res = hooks.pysipp_load_scendir(path=path, xmls=xmls, confpy=confpy)
if res and not all(res):
continue

agents = []
for xml in xmls:
if 'uac' in xml.lower():
if "uac" in xml.lower():
ua = agent.client(scen_file=xml)
agents.append(ua)
elif 'uas' in xml.lower():
elif "uas" in xml.lower():
ua = agent.server(scen_file=xml)
agents.insert(0, ua) # servers are always launched first
else:
raise ValueError(
"xml script must contain one of 'uac' or 'uas':\n{}"
.format(xml)
"xml script must contain one of 'uac' or 'uas':\n{}".format(xml)
)

if delay_conf_scen:
Expand All @@ -81,17 +78,13 @@ def walk(rootpath, delay_conf_scen=False, autolocalsocks=True,
yield path, scen


def scenario(dirpath=None, proxyaddr=None, autolocalsocks=True,
**scenkwargs):
def scenario(dirpath=None, proxyaddr=None, autolocalsocks=True, **scenkwargs):
"""Return a single Scenario loaded from `dirpath` if provided else the
basic default call flow.
"""
if dirpath:
# deliver single scenario from dir
path, scen = next(
walk(dirpath, autolocalsocks=autolocalsocks,
**scenkwargs)
)
path, scen = next(walk(dirpath, autolocalsocks=autolocalsocks, **scenkwargs))
else:
with plugin.register([netplug] if autolocalsocks else []):
# deliver the default scenario bound to loopback sockets
Expand All @@ -100,13 +93,13 @@ def scenario(dirpath=None, proxyaddr=None, autolocalsocks=True,

# same as above
scen = plugin.mng.hook.pysipp_conf_scen_protocol(
agents=[uas, uac], confpy=None,
agents=[uas, uac],
confpy=None,
scenkwargs=scenkwargs,
)

if proxyaddr is not None:
assert isinstance(proxyaddr, tuple), (
'proxyaddr must be a (addr, port) tuple')
assert isinstance(proxyaddr, tuple), "proxyaddr must be a (addr, port) tuple"
scen.clientdefaults.proxyaddr = proxyaddr

return scen
Expand All @@ -123,8 +116,7 @@ def pysipp_load_scendir(path, xmls, confpy):

@plugin.hookimpl
def pysipp_conf_scen_protocol(agents, confpy, scenkwargs):
"""Perform default configuration rule set
"""
"""Perform default configuration rule set"""
# more sanity
if confpy:
ua = agents[0]
Expand All @@ -138,13 +130,19 @@ def pysipp_conf_scen_protocol(agents, confpy, scenkwargs):
scen = agent.Scenario(agents, confpy=confpy)

# order the agents for launch
agents = list(hooks.pysipp_order_agents(
agents=scen.agents, clients=scen.clients,
servers=scen.servers)) or agents
agents = (
list(
hooks.pysipp_order_agents(
agents=scen.agents, clients=scen.clients, servers=scen.servers
)
)
or agents
)

# create scenario wrapper
scen = hooks.pysipp_new_scen(
agents=agents, confpy=confpy, scenkwargs=scenkwargs)
agents=agents, confpy=confpy, scenkwargs=scenkwargs
)

# configure scenario
hooks.pysipp_conf_scen(agents=scen.agents, scen=scen)
Expand All @@ -158,10 +156,8 @@ def pysipp_conf_scen_protocol(agents, confpy, scenkwargs):

@plugin.hookimpl
def pysipp_order_agents(agents, clients, servers):
"""Lexicographically sort agents by name and always start servers first
"""
return (agents[name] for name in
sorted(servers) + sorted(clients))
"""Lexicographically sort agents by name and always start servers first"""
return (agents[name] for name in sorted(servers) + sorted(clients))


@plugin.hookimpl
Expand All @@ -171,14 +167,13 @@ def pysipp_new_scen(agents, confpy, scenkwargs):

@plugin.hookimpl(trylast=True)
def pysipp_conf_scen(agents, scen):
"""Default validation logic and routing with media
"""
"""Default validation logic and routing with media"""
if scen.servers:
# point all clients to send requests to 'primary' server agent
# if they aren't already
servers_addr = scen.serverdefaults.get('srcaddr', ('127.0.0.1', 5060))
servers_addr = scen.serverdefaults.get("srcaddr", ("127.0.0.1", 5060))
uas = scen.prepare_agent(list(scen.servers.values())[0])
scen.clientdefaults.setdefault('destaddr', uas.srcaddr or servers_addr)
scen.clientdefaults.setdefault("destaddr", uas.srcaddr or servers_addr)

elif not scen.clientdefaults.proxyaddr:
# no servers in scenario so point proxy addr to remote socket addr
Expand Down
Loading