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

Source Code for Module mosp_examples.BTvirus_wiggler

  1  #!/bin/env python 
  2   
  3  """Infect action example with infection duration and action delay: BT-Virus example 
  4      - random movement 
  5      - BT infection 
  6          - infection range <= 10 up to 20m 
  7          - infection duration == after being 15s in infection range  
  8          - 1 initial infected and 89 healthy devices 
  9      - output to visual player, which is executed as child process 
 10  """ 
 11   
 12  import sys 
 13  sys.path.append("..")  
 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 ChildprocessPlayerChamplainMonitor, SocketPlayerMonitor, EmptyMonitor 
 19   
 20  __author__ = "B. Henne" 
 21  __contact__ = "henne@dcsec.uni-hannover.de" 
 22  __copyright__ = "(c) 2011, DCSec, Leibniz Universitaet Hannover, Germany" 
 23  __license__ = "GPLv3" 
24 25 26 -class BTVirusWiggler(Person):
27 """Models a random movement BT-virus infection 28 29 Mobile device gets infected if compatible device is in range of 10 to 20m for 15 seconds. 30 @author: B. Henne""" 31
32 - def __init__(self, *args, **kwargs):
33 """Init the BT-Infect-Wiggler.""" 34 super(BTVirusWiggler, self).__init__(*args, **kwargs) 35 self.p_infected = False #: infected? 36 self.pplInRange = {} #: stores infecting people in range 37 self.p_infecttime = None #: time if infection 38 if 'infected' in kwargs: 39 self.p_color = 1 #: color for playerChamplain 40 self.p_color_rgba = (0.9,0.1,0.1,1.0) #: color for sim_viewer.py 41 self.p_infected = True 42 self.p_infectionTime = self.sim.now() 43 self.p_infectionPlace = self.current_coords() #: coordinates of infection 44 print self.p_infectionTime, self.p_infectionPlace[0], self.p_infectionPlace[1], self.name, self.name 45 start_action(self.infect_other) 46 if 'infectionTime' in kwargs: 47 self.p_infectionTime = kwargs['infectionTime']
48 49 next_target = movement.person_next_target_random 50
51 - def tryinfect(self, infecting_one):
52 """The infection routine itself. 53 54 If not infected, person gets infected if bad one is in range of 10 to 20m for 15 seconds.""" 55 now = self.sim.now() 56 if infecting_one not in self.pplInRange: # was not in range till now? 57 new = { infecting_one : [1, now]} # remember duration, last time 58 self.pplInRange.update(new) # and store 59 else: # was in range before? 60 old = self.pplInRange[infecting_one] # get old 61 if old[1] < self.sim.now()-1: # if last time was not last tick 62 new = { infecting_one : [1, now] } # reset: remember duration, last time 63 self.pplInRange.update(new) # and store 64 else: # last time was last tick or newer 65 new = { infecting_one : [old[0]+1, now] } # increase duration, update last time 66 self.pplInRange.update(new) # and store 67 if self.pplInRange[infecting_one][0] >= 15: # if infection time is reached 68 self.p_color = 3 69 self.p_color_rgba = (0.5,0.0,0.5,1.0) 70 self.p_infected = True # I'm infected, infectious in 300s 71 self.p_infectionTime = now # I was infected when 72 self.p_infectionPlace = self.current_coords() # I was infected where 73 print self.p_infectionTime, self.p_infectionPlace[0], self.p_infectionPlace[1], self.name, infecting_one.name 74 start_action(self.infect_other, delay=300) # start being infectious after 300s
75 76 @action(1, start=False)
77 - def infect_other(self):
78 """The BT infect action. 79 80 Every tick this action is called (using the mosp action decorator). 81 This action looks for people in range of random(10,20) meters, and calls 82 their tryinfect() routine (300s start delay is done by start_action(this, delay=300). 83 Action is not active at beginning. It is activated by self.infect_other.start() 84 and then is called every tick.""" 85 if self.p_infected == True: 86 if self.p_color == 3: 87 self.p_color = 1 # used to viz difference of being only infected and also infecting after 300s 88 self.p_color_rgba = (0.9,0.1,0.1,1.0) 89 self.get_near(self._random.randint(10,20), self_included=False).filter(p_infected=False).call(delay=0).tryinfect(self)
90
91 92 -def main():
93 """Defines the BT infection simulation with random movement. 94 95 map: hannover2.osm, output to socketPlayer or champlain child process player, 96 89 healthy people, 1 infected, infection using action decorator.""" 97 s = Simulation(geo=osm.OSMModel('../data/hannover2.osm'), rel_speed=120) 98 #m = s.add_monitor(ChildprocessPlayerChamplainMonitor, 10) 99 m = s.add_monitor(SocketPlayerMonitor, 2) 100 s.add_persons(BTVirusWiggler, 1, monitor=m, args={"infected":True, "infectionTime":-301}) 101 s.add_persons(BTVirusWiggler, 89, monitor=m) 102 s.run(until=28800, real_time=True, monitor=True)
103 104 105 if __name__ == '__main__': 106 main() 107