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

Source Code for Module mosp_examples.message_example

 1  #!/bin/env python 
 2   
 3  """ A simple example for the usage of Person.send() 
 4      - random movement 
 5      - output to visual player, which is executed as child process 
 6      - you may try the other commented monitor examples - you can choose a single or multiple monitors 
 7      - print a message send to another person 
 8  """ 
 9   
10  import sys 
11  sys.path.append("..") 
12  import time 
13  import random 
14   
15  from mosp.core import Simulation, Person 
16  from mosp.geo import osm 
17  from mosp.impl import movement 
18  from mosp.monitors import * 
19   
20  __author__ = "P. Tute, B. Henne" 
21  __maintainer__ = "B. Henne" 
22  __contact__ = "henne@dcsec.uni-hannover.de" 
23  __copyright__ = "(c) 2010-2011, DCSec, Leibniz Universitaet Hannover, Germany" 
24  __license__ = "GPLv3" 
25   
26   
27 -class MsgRandomWiggler(Person):
28 """Implements a simple person doing only random movement on the map, sending and receiving some messages. 29 @author: P. Tute""" 30 next_target = movement.person_next_target_random 31
32 - def receive(self, m, sender):
33 """On receiving a message, the message is printed to stdout.""" 34 # this method is called, when a message is available. Do whatever you want with the message here. It will be called for each available message. 35 print 't=%s, sender=%s, receiver=%s' % (self.sim.now(), sender.p_id, self.p_id) 36 print '\t message=%s' % m 37 return True # When True is returned, the message is removed from the message queue. Return False, if you want to keep it for the next time this Person wakes up.
38
39 - def think(self):
40 """Person with id 23 sends hello messages to all people in his vicinity of 50 meters.""" 41 super(MsgRandomWiggler, self).think() 42 if self.p_id == 23: 43 # send a message to a receiver or (here) a group of receivers 44 self.send(self.get_near(50, self_included=False), "Hello Person in my vicinity at t=%s" % self.sim.now())
45 # the message will be queued by the receiver and received, when he wakes up 46 # you may also specify earliest_arrival, which is the earliest tick to deliver this message 47 # note that this is not a guarantee for receiving at that exact time. It is only guaranteed that the message is not delivered earlier. 48 # finally you may also interrupt the receiver(s) by passing interrupt=True when calling send() 49 50
51 -def main():
52 """Defines the simulation, map, monitors, persons.""" 53 t = time.time() 54 s = Simulation(geo=osm.OSMModel('../data/hannover2.osm'), rel_speed=40) 55 print time.time() - t 56 #m = s.add_monitor(EmptyMonitor, 2) 57 #m = s.add_monitor(PipePlayerMonitor, 2) 58 #m = s.add_monitor(RecordFilePlayerMonitor, 2) 59 #m = s.add_monitor(RecordFilePlayerMonitor, 2, filename='exampleoutput_RecordFilePlayerMonitor') 60 #m = s.add_monitor(ChildprocessPlayerChamplainMonitor, 2) 61 m = s.add_monitor(SocketPlayerMonitor, 2) 62 63 s.add_persons(MsgRandomWiggler, 100, monitor=m) 64 s.run(until=1000, real_time=True, monitor=True)
65 66 67 if __name__ == '__main__': 68 main() 69