/
Slave.py
56 lines (46 loc) · 1.77 KB
/
Slave.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
# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__
import sys
from geocamPycroraptor.Printable import Printable
from geocamPycroraptor.PycroEncoder import PycroEncoder
from geocamPycroraptor import anyjson as json
class Slave(Printable):
def __init__(self, name, endpoint, dispatcher, logComm=True, statusHandler=None):
self.name = name
self.endpoint = endpoint
self.dispatcher = dispatcher
self.logComm = logComm
self.statusHandler = statusHandler
self.status = None
self.conn = None
def connect(self):
self.conn = self.dispatcher.connect(self.endpoint, connectHandler=self.handleConnect,
lineHandler=self.handleLine)
def isConnected(self):
return self.conn.connected
def write(self, text):
if self.logComm:
print 'Slave: write:', text,
sys.stdout.flush()
self.conn.write(text)
def writeObject(self, obj):
self.write(json.dumps(obj, cls=PycroEncoder) + '\n')
def handleConnect(self, conn):
self.write('sub status -a\n')
self.write('get status -a\n')
def handleLine(self, conn, text):
cmd = json.loads(text)
print 'Slave: handleLine:', cmd
if cmd[0] == 'status':
_, taskName, status = cmd
self.handleStatus(taskName, status)
else:
pass # nothing to do for other message types yet
def handleStatus(self, taskName, status):
if self.statusHandler:
self.statusHandler(self, taskName, status)
else:
pass # no default action