1
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"
31 """Implements a zombie person doing random movement infecting others.
32 @author: P. Tute
33 @author: F. Ludwig"""
34
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)
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
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)
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
102 """Defines the simulation, map, monitors, persons."""
103
104 map_path = '../data/hannover0.osm'
105 s = Simulation(geo=osm.OSMModel(map_path), rel_speed=1)
106
107
108
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
113
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()
120
121
122 if __name__ == '__main__':
123 main()
124