| 1 |
""" |
| 2 |
pynorm - Python wrapper for NRL's libnorm |
| 3 |
By: Tom Wambold <[email protected]> |
| 4 |
""" |
| 5 |
|
| 6 |
# For Python 2.5 |
| 7 |
from __future__ import with_statement |
| 8 |
|
| 9 |
from threading import Thread, Lock |
| 10 |
from random import randint |
| 11 |
|
| 12 |
#from .models import get_session, SessionProfile, SendProfile, ReceiveProfile |
| 13 |
from .. import constants as c |
| 14 |
from ..instance import Instance |
| 15 |
|
| 16 |
class StopManager(Exception): pass |
| 17 |
|
| 18 |
class Manager(Thread): |
| 19 |
"""Provides a callback driven method of handling NORM events.""" |
| 20 |
|
| 21 |
def __init__(self, instance=None, *args, **kwargs): |
| 22 |
super(Manager, self).__init__(*args, **kwargs) |
| 23 |
self.setDaemon(True) |
| 24 |
|
| 25 |
self.instance = instance if instance is not None else Instance() |
| 26 |
|
| 27 |
self.callbacks = {} |
| 28 |
self.sessions = {} |
| 29 |
self.lock = Lock() |
| 30 |
self.doQuit = False |
| 31 |
|
| 32 |
def run(self): |
| 33 |
while not self.doQuit: |
| 34 |
event = self.instance.getNextEvent(2) |
| 35 |
if event is None: |
| 36 |
continue |
| 37 |
|
| 38 |
with self.lock: |
| 39 |
try: |
| 40 |
cbs = self.callbacks[event.type] |
| 41 |
except KeyError: |
| 42 |
continue |
| 43 |
|
| 44 |
try: |
| 45 |
for func, args, kwargs in cbs: |
| 46 |
func(event, *args, **kwargs) |
| 47 |
except StopManager: |
| 48 |
self.doQuit = True |
| 49 |
|
| 50 |
def register(self, event, func, *args, **kwargs): |
| 51 |
with self.lock: |
| 52 |
self.callbacks.setdefault(event, list()).append( |
| 53 |
(func, args, kwargs)) |
| 54 |
|
| 55 |
def unregister(self, event, func, *args, **kwargs): |
| 56 |
with self.lock: |
| 57 |
self.callbacks[event].remove((func, args, kwargs)) |
| 58 |
|
| 59 |
# def start_session(self, name): |
| 60 |
# sqlsession = get_session() |
| 61 |
# profile = sqlsession.query(SessionProfile).filter_by(name=name).first() |
| 62 |
# self.instance.setCacheDirectory(profile.cache_directory) |
| 63 |
# session = self.instance.createSession(profile.address, profile.port, profile.node_id) |
| 64 |
# session.userData = name |
| 65 |
# |
| 66 |
# if profile.interface is not None: |
| 67 |
# session.setMulticastInterface(profile.interface) |
| 68 |
# |
| 69 |
# if profile.ttl is not None: |
| 70 |
# session.setTTL(profile.ttl) |
| 71 |
# |
| 72 |
# if profile.tos is not None: |
| 73 |
# session.setTOS(profile.tos) |
| 74 |
# |
| 75 |
# if profile.loopback is not None: |
| 76 |
# session.setLoopback(profile.loopback) |
| 77 |
# self.sessions[name] = session |
| 78 |
# sqlsession.close() |
| 79 |
# |
| 80 |
# def start_sender(self, session_name, sender_name): |
| 81 |
# session = self.sessions[session_name] |
| 82 |
# sqlsession = get_session() |
| 83 |
# profile = sqlsession.query(SendProfile).filter_by( |
| 84 |
# name=sender_name).first() |
| 85 |
# session.startSender(randint(0, 10000), profile.buffer_space, |
| 86 |
# profile.segment_size, profile.block_size, profile.num_parity) |
| 87 |
# |
| 88 |
# if profile.graceful_stop is not None: |
| 89 |
# session.sendGracefulStop = profile.graceful_stop |
| 90 |
# |
| 91 |
# if profile.rate is not None: |
| 92 |
# session.setTxRate(profile.rate) |
| 93 |
# |
| 94 |
# if profile.socket_buffer is not None: |
| 95 |
# session.setTxSocketBuffer(profile.socket_buffer) |
| 96 |
# |
| 97 |
# if profile.congestion_control is not None: |
| 98 |
# session.setCongestionControl(profile.congestion_control) |
| 99 |
# |
| 100 |
# if None not in (profile.rate_min, profile.rate_max): |
| 101 |
# session.setTxRateBounds(profile.rate_min, profile.rate_max) |
| 102 |
# |
| 103 |
# if None not in (profile.size_max, profile.cache_min, |
| 104 |
# profile.cache_max): |
| 105 |
# session.setTxCacheBounds(profile.size_max, profile.cache_min, |
| 106 |
# profile.cache_max) |
| 107 |
# |
| 108 |
# if profile.auto_parity is not None: |
| 109 |
# session.setAutoParity(profile.auto_parity) |
| 110 |
# |
| 111 |
# if profile.grtt is not None: |
| 112 |
# session.setGrttEstimate(profile.grtt) |
| 113 |
# |
| 114 |
# if profile.grtt_max is not None: |
| 115 |
# session.setGrttMax(profile.grtt_max) |
| 116 |
# |
| 117 |
# if profile.grtt_probing_mode is not None: |
| 118 |
# session.setGrttProbingMode(getattr(c, profile.grtt_probing_mode)) |
| 119 |
# |
| 120 |
# if None not in (profile.grtt_probing_interval_min, |
| 121 |
# profile.grtt_probing_interval_max): |
| 122 |
# session.setGrttProbingInterval(profile.grtt_probing_interval_min, |
| 123 |
# profile.grtt_probing_interval_max) |
| 124 |
# |
| 125 |
# if profile.backoff_factor is not None: |
| 126 |
# session.setBackoffFactor(profile.backoff_factor) |
| 127 |
# |
| 128 |
# if profile.group_size is not None: |
| 129 |
# session.setGroupSize(profile.group_size) |
| 130 |
# sqlsession.close() |
| 131 |
# return session |
| 132 |
# |
| 133 |
# def start_receiver(self, session_name, receiver_name): |
| 134 |
# sqlsession = get_session() |
| 135 |
# session = self.sessions[session_name] |
| 136 |
# profile = sqlsession.query(ReceiveProfile).filter_by( |
| 137 |
# name=receiver_name).first() |
| 138 |
# session.startReceiver(profile.buffer_space) |
| 139 |
# |
| 140 |
# if profile.grace_period is not None: |
| 141 |
# session.gracePeriod = profile.grace_period |
| 142 |
# |
| 143 |
# if profile.socket_buffer is not None: |
| 144 |
# session.setRxSocketBuffer(profile.socket_buffer) |
| 145 |
# |
| 146 |
# if profile.is_silent is not None: |
| 147 |
# session.setSilentReceiver(profile.is_silent, profile.silent_max_delay) |
| 148 |
# |
| 149 |
# if profile.unicast_nack is not None: |
| 150 |
# session.setDefaultUnicastNack(profile.unicast_nack) |
| 151 |
# |
| 152 |
# if profile.nacking_mode is not None: |
| 153 |
# session.setDefaultNackingMode(getattr(c, profile.nacking_mode)) |
| 154 |
# |
| 155 |
# if profile.repair_boundary is not None: |
| 156 |
# session.setDefaultRepairBoundary(getattr(c, profile.repair_boundary)) |
| 157 |
# sqlsession.close() |
| 158 |
# return session |