class Node : def __init__(self, model) : self._model = Model.from_string(model) self._name = self._model.get('name') self._api = API(self) self._api.get_event_channel().subscribe('MOVEACPT', self._on_move) self._position = self._model.get('position') def _on_move(self, event) : if event.get_model().get('name') == self._name : self._position = event.get_model().get('pos') #print 'NODE %s HEARD ACCEPT %s' % (self._name, self._position) def get_position(self) : return self._position def get_name(self) : return self._name def get_model(self) : return self._model def start(self) : for agent_model in self._model.get('agents') : agent_model.set_owner(self) Thread(target=agent_model.run()).start()
def __init__(self) : self._channel = API('foobar').get_event_channel() self._channel.subscribe('LINK_UP', self.link_up) self._channel.subscribe('LINK_DOWN', self.link_down) self._channel.subscribe('MOVEACPT', self.moveacpt) self._sock = SDTSocket('127.0.0.1', 50730) self._sock.setLossThresholds(100,101) self.lat_bot_left = 39.9534 self.lon_bot_left = -75.1912 self.delta = 0.0045 #deg lat/long self.max_altitude = 500 #meters self.random_scale = 100
def __init__(self, scen_def, net_def) : self._scen_def = scen_def self._net_def = net_def self._api = API('simulation') self._api.get_event_channel().subscribe('*', self._event_callback) self._scen_inst = None self._net_inst = None self._network_sim = NetworkSim(self) def quit(signal, frame) : os.system('pkill python') sys.exit(0) signal.signal(signal.SIGINT, quit)
class Simulation : def __init__(self, scen_def, net_def) : self._scen_def = scen_def self._net_def = net_def self._api = API('simulation') self._api.get_event_channel().subscribe('*', self._event_callback) self._scen_inst = None self._net_inst = None self._network_sim = NetworkSim(self) def quit(signal, frame) : os.system('pkill python') sys.exit(0) signal.signal(signal.SIGINT, quit) def _event_callback(self, event) : if event.get_type() == 'SENT' : self._network_sim.on_sent_event(event) elif event.get_type() == 'MOVEREQ' : #print 'SIMULATION GOT MOVEREQ %s %s' % (event.get_model().get('name'), event.get_model().get('pos')) self._scen_inst.get_nodes()[int(event.get_model().get('name')[1:])].set('position', event.get_model().get('pos')) self._api.get_event_channel().publish(Event('MOVEACPT', Model(name=event.get_model().get('name'), pos=event.get_model().get('pos')))) for other_node in self._scen_inst.get_nodes() : n1 = self.get_scen().get_nodes()[int(event.get_model().get('name')[1:])] n2 = self.get_scen().get_nodes()[int(other_node.get('name')[1:])] if other_node.get('name') != event.get_model().get('name') : self._network_sim.can_transmit(n1, n2) def get_scen(self) : return self._scen_inst def get_api(self) : return self._api def stop(self) : self._running = False def start(self) : scenmod = __import__(scen_def.split('.')[0]) self._scen_inst = getattr(scenmod, scen_def.split('.')[1])() netmod = __import__(net_def.split('.')[0]) self._net_inst = getattr(netmod, net_def.split('.')[1])(self._scen_inst) for node_model in self._scen_inst.get_nodes() : subprocess.Popen(('python node.py ' + node_model.__str__()).split(' ')) time.sleep(1) self._api.get_event_channel().publish(Event('start', Model())) while True : pass
class sdt_foobar : def __init__(self) : self._channel = API('foobar').get_event_channel() self._channel.subscribe('LINK_UP', self.link_up) self._channel.subscribe('LINK_DOWN', self.link_down) self._channel.subscribe('MOVEACPT', self.moveacpt) self._sock = SDTSocket('127.0.0.1', 50730) self._sock.setLossThresholds(100,101) self.lat_bot_left = 39.9534 self.lon_bot_left = -75.1912 self.delta = 0.0045 #deg lat/long self.max_altitude = 500 #meters self.random_scale = 100 def link_up(self, event) : node1 = event.get_model().get('n1').get('name') node2 = event.get_model().get('n2').get('name') self._sock.handlePathlossEvent(node1, node2, 200) self.debug("link_up: %s, %s" % (node1, node2)) def link_down(self, event) : node1 = event.get_model().get('n1').get('name') node2 = event.get_model().get('n2').get('name') self._sock.handlePathlossEvent(node1, node2, -200) #self.debug("link_down: %s, %s" % (node1, node2)) def moveacpt(self, event) : node = event.get_model().get('name') x,y,z = event.get_model().get('pos') lat = self.lat_bot_left + (self.delta/self.random_scale)*y lon = self.lon_bot_left + (self.delta/self.random_scale)*x alt = (self.max_altitude/self.random_scale)*z self._sock.handleLocationEvent(node, lat, lon, alt) #self.debug("moveacpt: %s (%s, %s)" % (node, x, y)) def debug(self, string) : print string
def __init__(self, model) : self._model = Model.from_string(model) self._name = self._model.get('name') self._api = API(self) self._api.get_event_channel().subscribe('MOVEACPT', self._on_move) self._position = self._model.get('position')