Package mosp_examples :: Module external_device_wiggler
[hide private]
[frames] | no frames]

Source Code for Module mosp_examples.external_device_wiggler

  1  #!/bin/env python 
  2   
  3  """ Example for a simulation with external, real-world devices. 
  4      - works like any simulation, mostly 
  5      - persons are not routed, but receive coordinates from real-world devices 
  6      - basic logic (mainly in Person.think() can still be implemented 
  7      - an external data manager is used to receive and relay received data 
  8  """ 
  9   
 10  import sys 
 11  sys.path.append("..") 
 12  import time 
 13  import random 
 14   
 15  from mosp.core import Simulation, Person, action, start_action 
 16  from mosp.geo import osm 
 17  from mosp.impl import movement 
 18  from mosp.monitors import * 
 19   
 20  from mosp.external_persons import external_person, external_data_manager 
 21  from mosp.external_persons.external_person import ExternalPerson 
 22  from mosp.external_persons.external_data_manager import ExternalDataManager 
 23   
 24  __author__ = "P. Tute" 
 25  __maintainer__ = "B. Henne" 
 26  __contact__ = "henne@dcsec.uni-hannover.de" 
 27  __copyright__ = "(c) 2010-2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 28  __license__ = "GPLv3" 
29 30 -class ZombieWiggler(Person):
31 """Implements a zombie person doing random movement infecting others. 32 @author: P. Tute 33 @author: F. Ludwig""" 34
35 - def __init__(self, *args, **kwargs):
36 """Init the zombie.""" 37 super(ZombieWiggler, self).__init__(*args, **kwargs) 38 self.p_infected = False 39 if kwargs.get('infected'): 40 self.infect(True)
41 42 next_target = movement.person_next_target_random 43
44 - def infect(self, for_sure=False):
45 """the infection routine itself. 46 47 if not infected, person gets infected and speed is set to its half.""" 48 if for_sure or self._random.random() < 0.5: 49 if self.p_infected == False: 50 self.p_infected = True 51 self.p_color = 1 52 self.p_color_rgba = (1.0, 0.1, 0.1, 1.0) 53 self.p_speed = self.p_speed / 2 54 start_action(self.infect_other)
55 56 @action(2, start=False)
57 - def infect_other(self):
58 """the zombie infect action. 59 60 every 2 ticks this action is called (using the mosp action decorator). 61 this action looks for people in range of 1 meters, and calls their 62 infect() routine. action is not active at beginning. it is activated by 63 self.infect_other.start() and then is called every 2 ticks.""" 64 if self.p_infected == True: 65 self.get_near(1).call(delay=1).infect(True)
66
67 68 -class ExternalZombieWiggler(ExternalPerson, ZombieWiggler):
69 - def __init__(self, *args, **kwargs):
70 super(ExternalZombieWiggler, self).__init__(*args, **kwargs) 71 self.p_infected = False 72 if kwargs.get('infected'): 73 self.infect(True)
74 75 next_target = ExternalPerson.next_target 76
77 - def infect(self, for_sure=False):
78 """the infection routine itself. 79 80 if not infected, person gets infected and speed is set to its half.""" 81 if for_sure or self._random.random() < 0.5: 82 if self.p_infected == False: 83 self.p_infected = True 84 self.p_color = 1 85 self.p_color_rgba = (1.0, 0.1, 0.1, 1.0) 86 self.p_speed = self.p_speed / 2 87 start_action(self.infect_other)
88 89 @action(2, start=False)
90 - def infect_other(self):
91 """the zombie infect action. 92 93 every 2 ticks this action is called (using the mosp action decorator). 94 this action looks for people in range of 1 meters, and calls their 95 infect() routine. action is not active at beginning. it is activated by 96 self.infect_other.start() and then is called every 2 ticks.""" 97 if self.p_infected == True: 98 self.get_near(1).call(delay=1).infect(True)
99
100 101 -def main():
102 """Defines the simulation, map, monitors, persons.""" 103 # initialize simulation as usual 104 map_path = '../data/hannover0.osm' 105 s = Simulation(geo=osm.OSMModel(map_path), rel_speed=1) 106 107 # create an instance of ExternalDataManager and add it to the simulation as a SimPy process (do NOT use add_persons). 108 #ext_manager = ExternalDataManager(sim=s, address='0.0.0.0', port=8080, map_path='../data/home.osm', free_move_only=True) 109 ext_manager = ExternalDataManager(sim=s, address='0.0.0.0', port=8080, map_path=map_path, free_move_only=False) 110 s.activate(ext_manager, ext_manager.run(), 0) 111 112 # continue initialization as usual 113 #m = s.add_monitor(EmptyMonitor, 2) 114 m = s.add_monitor(SocketPlayerMonitor, 1) 115 s.add_persons(ExternalZombieWiggler, 1, monitor=m) 116 s.add_persons(ZombieWiggler, 48, monitor=m) 117 s.add_persons(ZombieWiggler, 1, monitor=m, args={"infected":True, "speed":0.7}) 118 s.run(until=1000000, real_time=True, monitor=True) 119 ext_manager.shutdown() # call this to reliably shut down the webserver
120 121 122 if __name__ == '__main__': 123 main() 124