-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.py
executable file
·110 lines (90 loc) · 3.67 KB
/
status.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/python
# JoeAgent - A Multi-Agent Distributed Application Framework
# Copyright (C) 2004 Rhett Garber
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import socket, sys, logging
import simple, agent
from event import Event
from job import Job, RunJobEvent
log = logging.getLogger("agent.status")
class StatusJob(Job):
def __init__(self, agent_obj):
Job.__init__(self, agent_obj)
self.conn = None
self.key = None
def run(self):
log.debug("Running Status job")
# Send Shutdown Request
msg = simple.StatusRequest()
self.key = msg.getKey()
assert self.conn != None, "Connection should not be None"
evt = agent.MessageSendEvent(self, msg, self.conn)
self.getAgent().addEvent(evt)
def notify(self, evt):
Job.notify(self, evt)
if isinstance(evt, simple.ConnectCompleteEvent):
self.conn = evt.getConnection()
self.run()
elif isinstance(evt, agent.MessageReceivedEvent):
if isinstance(evt.getMessage(), simple.StatusResponse) and \
self.key == evt.getMessage().getRequestKey():
print "Status Acknowledged"
print str(evt.getMessage())
self.getAgent().setState(agent.STOPPING)
if isinstance(evt.getMessage(), agent.DeniedResponse) and \
self.key == evt.getMessage().getRequestKey():
print "Status Denied"
self.getAgent().setState(agent.STOPPING)
def setup_logger(logname, filename):
if logname != "":
log = logging.getLogger(logname)
else:
log = logging.getLogger()
hdler = logging.FileHandler(filename)
fmt = logging.Formatter(logging.BASIC_FORMAT)
hdler.setFormatter(fmt)
log.addHandler(hdler)
log.setLevel(logging.DEBUG)
return log
if __name__ == "__main__":
log = setup_logger("", "log/status.log")
if len(sys.argv) < 3:
print "Usage: %s <addr> <port>" % sys.argv[0]
sys.exit(1)
bind_addr = sys.argv[1]
port = sys.argv[2]
# Our simple configuration.
# Note we are not setting address or port because we do not want to be a
# server.
config = agent.AgentConfig()
config.setName("Status Command")
# Configuration for the remote agent
remote_info = agent.AgentInfo()
remote_info.setHost(bind_addr)
remote_info.setPort(int(port))
remote_info.setName("Remote Agent")
# Create the agent
command_agent = agent.Agent(config)
# Create the jobs
status_job = StatusJob(command_agent)
connect_job = simple.ConnectJob(command_agent, remote_info)
# Create an event that will start the job we want at run-time
run_evt = RunJobEvent(command_agent, connect_job)
command_agent.addEvent(run_evt)
# Don't forget to add the jobs as listeners
command_agent.addListener(status_job)
command_agent.addListener(connect_job)
command_agent.addListener(simple.HandlePingJob(command_agent))
print "Running status"
command_agent.run()