@@ -17,11 +17,6 @@ def __init__(self, **kwds):
1717
1818
1919class Fsm (object ):
20- E_START = "start"
21- E_LOOKUP = "lookup"
22- E_ANNOUNCE = "announce"
23- E_TEST = "test"
24-
2520 RETRY_PERIOD = 30
2621
2722 agent = None
@@ -33,20 +28,23 @@ def __init__(self, agent):
3328
3429 self .agent = agent
3530 self .fsm = f .Fysom ({
36- "initial" : "none " ,
31+ "initial" : "lostandalone " ,
3732 "events" : [
38- {"name" : self .E_START , "src" : [
39- "none" , "unannounced" , "announced" , "ready" ], "dst" : "init" },
40- {"name" : self .E_LOOKUP , "src" : "init" , "dst" : "unannounced" },
41- {"name" : self .E_ANNOUNCE , "src" : "unannounced" , "dst" : "announced" },
42- {"name" : self .E_TEST , "src" : "announced" , "dst" : "ready" }],
33+ ("startup" , "*" , "lostandalone" ),
34+ ("lookup" , "lostandalone" , "found" ),
35+ ("announce" , "found" , "announced" ),
36+ ("ready" , "announced" , "good2go" )],
4337 "callbacks" : {
44- "onstart" : self .lookup_agent_host ,
45- "onenterunannounced" : self .announce_sensor ,
46- "onenterannounced" : self .test_agent }})
38+ "onlookup" : self .lookup_agent_host ,
39+ "onannounce" : self .announce_sensor ,
40+ "onchangestate" : self .printstatechange }})
41+
42+ def printstatechange (self , e ):
43+ l .debug ('========= (%i#%s) FSM event: %s, src: %s, dst: %s ==========' % \
44+ (os .getpid (), t .current_thread ().name , e .event , e .src , e .dst ))
4745
4846 def reset (self ):
49- self .fsm .start ()
47+ self .fsm .lookup ()
5048
5149 def lookup_agent_host (self , e ):
5250 if self .agent .sensor .options .agent_host != "" :
@@ -57,17 +55,19 @@ def lookup_agent_host(self, e):
5755 h = self .check_host (host )
5856 if h == a .AGENT_HEADER :
5957 self .agent .set_host (host )
60- self .fsm .lookup ()
58+ self .fsm .announce ()
6159 else :
6260 host = self .get_default_gateway ()
6361 if host :
6462 self .check_host (host )
6563 if h == a .AGENT_HEADER :
6664 self .agent .set_host (host )
67- self .fsm .lookup ()
65+ self .fsm .announce ()
6866 else :
6967 l .error ("Cannot lookup agent host. Scheduling retry." )
70- self .schedule_retry (self .lookup_agent_host , e )
68+ self .schedule_retry (self .lookup_agent_host , e , "agent_lookup" )
69+ return False
70+ return True
7171
7272 def get_default_gateway (self ):
7373 l .debug ("checking default gateway" )
@@ -101,22 +101,27 @@ def announce_sensor(self, e):
101101 self .agent .make_url (a .AGENT_DISCOVERY_URL ), "PUT" , d )
102102 if not b :
103103 l .error ("Cannot announce sensor. Scheduling retry." )
104- self .schedule_retry (self .announce_sensor , e )
104+ self .schedule_retry (self .announce_sensor , e , "announce" )
105+ return False
105106 else :
106107 self .agent .set_from (b )
107- self .fsm .announce ()
108+ self .fsm .ready ()
109+ return True
108110
109- def schedule_retry (self , fun , e ):
111+ def schedule_retry (self , fun , e , name ):
112+ l .error ("Scheduling: " + name )
110113 self .timer = t .Timer (self .RETRY_PERIOD , fun , [e ])
111- self .timer .setDaemon (True )
114+ self .timer .daemon = True
115+ self .timer .name = name
112116 self .timer .start ()
117+ l .debug ('Threadlist: %s' , str (t .enumerate ()))
113118
114119 def test_agent (self , e ):
115120 l .debug ("testing communication with the agent" )
116121
117122 (b , _ ) = self .agent .head (self .agent .make_url (a .AGENT_DATA_URL ))
118123
119124 if not b :
120- self .schedule_retry (self .test_agent , e )
125+ self .schedule_retry (self .test_agent , e , "agent test" )
121126 else :
122127 self .fsm .test ()
0 commit comments