1   
  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" 
 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       
 33          """Init the BT-Infect-Wiggler.""" 
 34          super(BTVirusWiggler, self).__init__(*args, **kwargs) 
 35          self.p_infected = False                                
 36          self.pplInRange = {}                                   
 37          self.p_infecttime = None                               
 38          if 'infected' in kwargs: 
 39              self.p_color = 1                                   
 40              self.p_color_rgba = (0.9,0.1,0.1,1.0)              
 41              self.p_infected = True 
 42              self.p_infectionTime = self.sim.now() 
 43              self.p_infectionPlace = self.current_coords()      
 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       
 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:                         
 57              new = { infecting_one : [1, now]}                            
 58              self.pplInRange.update(new)                                  
 59          else:                                                            
 60              old = self.pplInRange[infecting_one]                         
 61              if old[1] < self.sim.now()-1:                                
 62                  new = { infecting_one : [1, now] }                       
 63                  self.pplInRange.update(new)                              
 64              else:                                                        
 65                  new = { infecting_one : [old[0]+1, now] }                
 66                  self.pplInRange.update(new)                              
 67              if self.pplInRange[infecting_one][0] >= 15:                  
 68                  self.p_color = 3 
 69                  self.p_color_rgba = (0.5,0.0,0.5,1.0) 
 70                  self.p_infected = True                                     
 71                  self.p_infectionTime = now                                 
 72                  self.p_infectionPlace = self.current_coords()              
 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)               
  75   
 76      @action(1, start=False) 
 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   
 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   
 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       
 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