1 | # WARPnet Client<->Server Architecture |
---|
2 | # WARPnet Parameter Definitions |
---|
3 | # |
---|
4 | # Author: Siddharth Gupta |
---|
5 | |
---|
6 | from warpnet_common_params import * |
---|
7 | from twisted.internet import reactor |
---|
8 | import struct, time |
---|
9 | |
---|
10 | class WaitForResponse(): |
---|
11 | # the receiver gives the received packet to this checker. if the response matches then it must cancel itself from the queue and cancel the |
---|
12 | # resender. |
---|
13 | def responseCheck(self, data): |
---|
14 | pass |
---|
15 | |
---|
16 | # recreate the timeouttimer, increment the resend counter, resend just the data |
---|
17 | def timeoutExpired(self): |
---|
18 | pass |
---|
19 | |
---|
20 | class InstructionClass: |
---|
21 | |
---|
22 | sendType = -1 # packet type of sent command |
---|
23 | returnType = SC_STAT |
---|
24 | updateComplete = False |
---|
25 | returnStatus = False |
---|
26 | |
---|
27 | def waitForUpdate(self): |
---|
28 | while reactor.running and not self.updateComplete: |
---|
29 | pass |
---|
30 | return self.returnStatus |
---|
31 | |
---|
32 | def updateFromServer(self, data, status): |
---|
33 | updateComplete = True |
---|
34 | |
---|
35 | |
---|
36 | def checkError(statusCode): |
---|
37 | if statusCode == SC_STAT_GROUP_UNMATCHED: |
---|
38 | print "Server does not know of group" |
---|
39 | elif statusCode == SC_STAT_NOT_CONN_NODE: |
---|
40 | print "Not connected to specified nodes" |
---|
41 | elif statusCode == SC_NOT_REGISTERED: |
---|
42 | print "Not registered for struct, group pair" |
---|
43 | elif statusCode == C_TIMEOUT: |
---|
44 | print "Request timed out" |
---|
45 | else: |
---|
46 | print "Error not known. Status Code = %d" % statusCode |
---|
47 | |
---|
48 | # create blank ClientStruct that users can extend for structs that are understood at the WARP board |
---|
49 | class ClientStruct: |
---|
50 | structID = -1 |
---|
51 | expectedReturnStructID = -1 |
---|
52 | updateDone = False |
---|
53 | returnStatus = False |
---|
54 | lockStatus = False |
---|
55 | |
---|
56 | def __init__(self, logger): |
---|
57 | if logger is None: |
---|
58 | self.loggers = [] |
---|
59 | else: |
---|
60 | self.loggers = [logger] |
---|
61 | |
---|
62 | # The prepToSend function is called by a Node instance when it is about to send this struct to a particular node. The destination |
---|
63 | # nodeID will be provided by the instance and the struct must embed this in the right order. The returned data is the byte packed |
---|
64 | # sequence that is understood by the node. |
---|
65 | def prepToSend(self, nodeID): |
---|
66 | self.updateDone = False |
---|
67 | return struct.pack('BB', self.structID, nodeID) #pack converts the variables to binary |
---|
68 | |
---|
69 | def callbackFromResponse(self, data, status): |
---|
70 | if status: |
---|
71 | self.updateFromNode(data['raw'], data['pcapts']) |
---|
72 | else: |
---|
73 | if data['stat'] == SC_DATA_LOCKED: |
---|
74 | print "Data struct is locked, cannot update" |
---|
75 | else: |
---|
76 | checkError(data['stat']) |
---|
77 | self.returnStatus = status |
---|
78 | self.updateDone = True |
---|
79 | |
---|
80 | # The updateFromNode function is called when the node either receives a response to a sent struct or receives an unrequested struct. |
---|
81 | def updateFromNode(self, data, pcapts): |
---|
82 | pass |
---|
83 | |
---|
84 | def updateComplete(self): |
---|
85 | #print "waiting in update done" |
---|
86 | while reactor.running and not self.updateDone: |
---|
87 | pass |
---|
88 | #print "%s" % self.updateDone |
---|
89 | return self.returnStatus |
---|
90 | |
---|
91 | def addLogger(self, loggerInst): |
---|
92 | print "in add logger" |
---|
93 | self.loggers.append(loggerInst) |
---|
94 | print "new logger list %s" % self.loggers |
---|
95 | |
---|
96 | |
---|
97 | def logData(self, data): |
---|
98 | for logger in self.loggers: |
---|
99 | logger.log(data) |
---|
100 | |
---|
101 | class DataCollector(): |
---|
102 | |
---|
103 | def log(self, dataToLog): |
---|
104 | pass |
---|