##// END OF EJS Templates
imanay -
r92:93
parent child
Show More
@@ -0,0 +1,165
1 import socket
2
3 class TCPComm:
4
5 __HEADER = "JRO"
6 __TYPE = "ABS"
7
8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9
10 self.ipSource = ipSource
11 self.ipDestino = ipDestino
12 self.portDestino = portDestino
13 self.addr = (ipDestino,portDestino)
14
15 self.sc = "none"
16 self.answer = "none" #test
17 self.asServer = False
18 self.len = 0
19 self.crc = 0
20
21 self.openSocket(asServer)
22
23 def openSocket(self, asServer):
24
25 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
26 # self.socket_c = socket.socket()
27 # self.socket_c.connect((self.ipDestino, self.portDestino))
28
29 if asServer:
30 self.socket_c = socket.socket()
31 # self.configAsServer()
32 self.socket_c.bind(self.addr)
33 self.asServer = True
34 else:
35 # self.configAsClient()
36 self.asServer = False #Socket is opened at the sendData function
37
38 # def configAsClient(self):
39 #Buscar broadcast TCP
40 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
41 # self.socket_c.connect(self.addr)
42 # pass
43
44 # def configAsServer(self):
45 #
46 # self.socket_c.bind(self.addr)
47
48 def waitData(self, nbytes = 1024):
49
50 print "\nWaiting some client."
51
52 if self.asServer == False:
53 # Short data through ethernet
54 trama_rx = self.socket_c.recv(nbytes)
55 else:
56 self.socket_c.listen(1)
57 sc, addr = self.socket_c.accept()
58 self.sc = sc
59 self.answer = addr
60 # Big data through ethernet
61 trama_rx = ""
62 while True:
63 tmp = self.sc.recv(nbytes)
64 trama_rx = trama_rx + tmp
65 if trama_rx[-4:] == "quit":
66 break
67
68 print "\nThis socket has received some data."
69
70 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
71
72 return ipSource, ipDestino, cmd, data
73
74 def waitServer(self, nbytes = 1024):
75
76 print "\nWaiting some client."
77 self.socket_c.listen(1)
78 sc, addr = self.socket_c.accept()
79 self.sc = sc
80 self.answer = addr
81 # Big data through ethernet
82 trama_rx = ""
83 while True:
84 tmp = self.sc.recv(nbytes)
85 trama_rx = trama_rx + tmp
86 if trama_rx[-4:] == "quit":
87 break
88
89 print "\nThis socket has received some data from: " + str(self.answer)
90
91 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
92
93 return ipSource, ipDestino, cmd, data
94
95 def waitClient(self, nbytes = 1024):
96
97 print "\nWaiting the server."
98 # Short data through ethernet
99 trama_rx = self.socket_c.recv(nbytes)
100
101 print "\nThis socket has received this data: " + str(trama_rx)
102
103 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
104
105 return ipSource, ipDestino, cmd, data
106
107 def sendData(self, cmd, data, id):
108
109 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + \
110 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc)
111
112 if self.portDestino == 7000:
113 trama_tx = trama_tx + ":" + "quit"
114 # Send messages
115 if self.asServer == False:
116 host = "192.168.1." + str(id)
117 self.socket_c.connect((host, self.portDestino))
118 self.socket_c.send(trama_tx)
119 else:
120 self.sc.send(trama_tx)
121 print "Sending message:[" + trama_tx + "]"
122
123 def sendData2(self, cmd, data, ipDestino):
124
125 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \
126 ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit"
127
128 if self.asServer == True:
129 self.SendAsServer(trama_tx)
130 else:
131 self.SendAsClient(ipDestino, trama_tx)
132
133 def SendAsServer(self, trama_tx):
134
135 self.sc.send(trama_tx)
136 print "Sending message:[" + trama_tx + "] to: " + str(self.answer)
137
138
139 def SendAsClient(self, ipDestino, trama_tx):
140
141 self.socket_c.connect((ipDestino, self.portDestino))
142 self.socket_c.send(trama_tx)
143 print "Sending message:[" + trama_tx + "] to: " + ipDestino
144
145 def __getTrama(self, trama):
146
147 FrameList = trama.split(':')
148
149 header = FrameList[0]
150 TypeOfInstrument = FrameList[1]
151 ipSource = FrameList[2]
152 ipDestino = FrameList[3]
153 len = FrameList[4]
154 cmd = FrameList[5]
155 data = FrameList[6]
156 crc = FrameList[7]
157 trash = FrameList[8]
158
159 return ipSource, ipDestino, cmd, data
160
161 def close_socket(self):
162 self.socket_c.close()
163
164 def open_socket(self):
165 self.socket_c = socket.socket() No newline at end of file
@@ -4,11 +4,11
4 4 import time
5 5 import numpy as np
6 6
7 import library
7 import library2
8 8
9 9 class ABSClient:
10 10
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000):
11 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000):
12 12
13 13 self.ipSource = ipSource
14 14 self.ipDestino = ipDestino
@@ -18,7 +18,7
18 18
19 19 def createObjects(self):
20 20
21 self.commObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino)
21 self.commObj = library2.TCPComm(self.ipSource, self.ipDestino, self.portDestino)
22 22
23 23 def sendFile(self, filename):
24 24
@@ -30,15 +30,21
30 30 F_Obj.close()
31 31 FileStr = "".join(FileList)
32 32 data = FileStr
33
34 self.commObj.sendData(cmd="SNDF", data=data)
35 self.commObj.waitData()
33
34 self.commObj.open_socket()
35 # self.commObj.sendData(cmd="SNDF", data=data, id = 117)
36 self.commObj.sendData2(cmd="SNDF", data=data, ipDestino = self.ipDestino)
37 # self.commObj.waitData()
38 self.commObj.waitClient()
36 39 self.commObj.close_socket()
37 40
38 41 def changeBeam(self, newBeam):
39
40 self.commObj.sendData(cmd="CHGB", data=newBeam)
41 self.commObj.waitData()
42
43 self.commObj.open_socket()
44 # self.commObj.sendData(cmd="CHGB", data=newBeam, id = 117)
45 self.commObj.sendData2(cmd="CHGB", data=newBeam, ipDestino = self.ipDestino)
46 # self.commObj.waitData()
47 self.commObj.waitClient()
42 48 self.commObj.close_socket()
43 49
44 50 def __writeFile(self, filename, data):
@@ -48,9 +54,12
48 54 fobj.close()
49 55
50 56 def getStatus(self, data):
51
52 self.commObj.sendData(cmd="ANST", data = data)
53 ipSource, ipDestino, cmd, data = self.commObj.waitData()
57
58 self.commObj.open_socket()
59 # self.commObj.sendData(cmd="ANST", data = data, id = 117)
60 self.commObj.sendData2(cmd="ANST", data = data, ipDestino = self.ipDestino)
61 # ipSource, ipDestino, cmd, data = self.commObj.waitData()
62 ipSource, ipDestino, cmd, data = self.commObj.waitClient()
54 63 self.commObj.close_socket()
55 64 self.__writeFile("report.txt", data)
56 65
@@ -479,9 +488,9
479 488 filename = "experimento1.abs"
480 489
481 490 absObj = ABSClient()
482 absObj.sendFile(filename)
491 # absObj.sendFile(filename)
483 492 # absObj.changeBeam("0")
484 # absObj.changeBeam("1")
493 absObj.changeBeam("1")
485 494 # absObj.changeBeam("2")
486 495 # absObj.changeBeam("3")
487 496 # absObj.changeBeam("4")
@@ -13,6 +13,7
13 13 self.addr = (ipDestino,portDestino)
14 14 self.answer = "none" #test
15 15 self.mode = "none"
16
16 17
17 18 self.openSocket(asServer)
18 19
@@ -78,7 +79,8
78 79
79 80 class TCPComm:
80 81
81 __HEADER = "ABS"
82 __HEADER = "JRO"
83 __TYPE = "ABS"
82 84
83 85 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
84 86
@@ -90,30 +92,35
90 92 self.sc = "none"
91 93 self.answer = "none" #test
92 94 self.mode = "none"
95 self.len = 0
96 self.crc = 0
93 97
94 98 self.openSocket(asServer)
95 99
96 100 def openSocket(self, asServer):
97 101
98 102 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
99 self.socket_c = socket.socket()
103 # self.socket_c = socket.socket()
100 104 # self.socket_c.connect((self.ipDestino, self.portDestino))
101 105
102 106 if asServer:
103 self.configAsServer()
107 self.socket_c = socket.socket()
108 # self.configAsServer()
109 self.socket_c.bind(self.addr)
104 110 self.mode = "server"
105 111 else:
106 self.configAsClient()
107 self.mode = "client"
108
109 def configAsClient(self):
112 # self.configAsClient()
113 self.mode = "client" #Socket is opened at the sendData function
114
115 # def configAsClient(self):
110 116 #Buscar broadcast TCP
111 117 # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
112 self.socket_c.connect(self.addr)
113
114 def configAsServer(self):
115
116 self.socket_c.bind(self.addr)
118 # self.socket_c.connect(self.addr)
119 # pass
120
121 # def configAsServer(self):
122 #
123 # self.socket_c.bind(self.addr)
117 124
118 125 def waitData(self, nbytes = 1024):
119 126
@@ -135,21 +142,22
135 142 if trama_rx[-4:] == "quit":
136 143 break
137 144
138 print "\nThis socket has received some data from:"
139 print self.answer
145 print "\nThis socket has received some data."
140 146
141 147 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
142 148
143 149 return ipSource, ipDestino, cmd, data
144 150
145 def sendData(self, cmd, data):
146
151 def sendData(self, cmd, data, id):
152
153 trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc)
154
147 155 if self.portDestino == 7000:
148 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" + "quit"
149 else:
150 trama_tx = data
156 trama_tx = trama_tx + ":" + "quit"
151 157 # Send messages
152 158 if self.mode == "client":
159 host = "192.168.1." + str(id)
160 self.socket_c.connect((host, self.portDestino))
153 161 self.socket_c.send(trama_tx)
154 162 else:
155 163 self.sc.send(trama_tx)
@@ -160,16 +168,22
160 168 FrameList = trama.split(':')
161 169
162 170 header = FrameList[0]
163 ipSource = FrameList[1]
164 ipDestino = FrameList[2]
165 cmd = FrameList[3]
166 data = FrameList[4]
167 trash = FrameList[5]
171 TypeOfInstrument = FrameList[1]
172 ipSource = FrameList[2]
173 ipDestino = FrameList[3]
174 len = FrameList[4]
175 cmd = FrameList[5]
176 data = FrameList[6]
177 crc = FrameList[7]
178 trash = FrameList[8]
168 179
169 180 return ipSource, ipDestino, cmd, data
170 181
171 182 def close_socket(self):
172 183 self.socket_c.close()
184
185 def open_socket(self):
186 self.socket_c = socket.socket()
173 187
174 188 #class FTPComm:
175 189 #
@@ -1,10 +1,10
1 1 import os
2 import library
2 import library2
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000, ipDestino2="192.168.1.225", portDestino2=5500, ftpPortDestino=None):
7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500):
8 8
9 9 self.ipSource = ipSource
10 10 self.ipDestino = ipDestino
@@ -13,37 +13,34
13 13 self.ipDestino2 = ipDestino2
14 14 self.portDestino2 = portDestino2
15 15
16 self.ftpPortDestino = ftpPortDestino
17 self.experiment_name = "default"
18 16 self.tx_buffer = "default"
17 self.rx_buffer = "default"
19 18
20 19 self.createObjects()
21 20
22 21 def createObjects(self):
23 22
24 23 asServer = True
25 self.commServerObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
28
24 self.commServerObj = library2.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer)
25 self.commClientObj = library2.TCPComm("Central_Control", self.ipDestino2, self.portDestino2)
29 26
30 27 def waitRequest(self):
31 28
32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
33
34 datarpta = "OK"
29 # Using rx buffer
30 ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitServer()
35 31
36 32 if cmd == "SNDF":
37 self.sendFile2Modules()
33 datarpta = self.sendFile2Modules(mode=2, cmd = cmd)
38 34
39 35 if cmd == "CHGB":
40 self.changeBeam()
36 datarpta = self.changeBeam(cmd = cmd)
41 37
42 38 if cmd == "ANST":
43 self.getStatus(mode=3)
39 self.getStatus(mode=4, cmd = cmd)
40 # Using tx buffer
44 41 datarpta = self.tx_buffer
45 42
46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
43 self.commServerObj.sendData2(cmd=cmd, data=datarpta, ipDestino = ipSource)
47 44
48 45 def checkModule(self, address):
49 46
@@ -62,7 +59,6
62 59
63 60 for address in range(1,65):
64 61 if address in enaModules:
65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
66 62 status_array.append("192.168.1." + str(address) + " [1 1]\n")
67 63 else:
68 64 status_array.append("192.168.1." + str(address) + " [0 0]\n")
@@ -92,10 +88,17
92 88 self.__writeReport(enaModules)
93 89 return enaModules
94 90
95 def sendFile2Modules(self):
91 def sendFile2Modules(self, mode, cmd):
92
93 if mode == 1:
94 self.__sendFile2Modules1()
95 else:
96 self.__sendFile2Modules2(cmd)
97
98 def __sendFile2Modules1(self):
96 99
97 100 #Needed for the loop
98 rx_frame_list = self.datarx.split('\n',2)
101 rx_frame_list = self.rx_buffer.split('\n',2)
99 102
100 103 self.experiment_name = rx_frame_list[0]
101 104 experiment_number = rx_frame_list[1]
@@ -103,7 +106,8
103 106
104 107 lst_control_modules = str_control_modules.split("------\n")
105 108
106 enaModules = self.checkAntenna()
109 # enaModules = self.checkAntenna()
110 enaModules = [11,12,13,14]
107 111
108 112 for address in range(1,65):
109 113
@@ -117,6 +121,44
117 121 os.system(cmd)
118 122
119 123 self.__loadFile()
124
125 def __sendFile2Modules2(self,cmd):
126
127 #Needed for the loop
128 rx_frame_list = self.rx_buffer.split('\n',2)
129 correct = 0
130 failure = 0
131 header = rx_frame_list[0] + "\n"
132 str_control_modules = rx_frame_list[2]
133
134 lst_control_modules = str_control_modules.split("------\n")
135
136 # enaModules = self.checkAntenna()
137 enaModules = [11,12,13,14]
138
139 for id in range(1,65):
140
141 if id not in enaModules:
142 continue
143 #tcp client needed
144 self.commClientObj.open_socket()
145 ip = "192.168.1." + str(id)
146 self.commClientObj.sendData2(cmd, header + lst_control_modules[id-1], ip)
147 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
148 self.commClientObj.close_socket()
149
150 if tmp == "OK":
151 correct = correct + 1
152 else:
153 failure = failure + 1
154
155 if correct == len(enaModules):
156 rpta = "OK"
157 else:
158 rpta = "Failure"
159
160 return rpta
161
120 162
121 163 def __writeModuleFile(self, filename, str):
122 164
@@ -142,25 +184,52
142 184 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
143 185 self.commClientObj.sendData("none", "CAMBIA:0:")
144 186
145 def changeBeam(self):
146
147 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
148 self.commClientObj.sendData("none", "CAMBIA:" + self.datarx + ":")
149
150 def getStatus(self,mode):
187 def changeBeam(self, cmd):
188
189 correct = 0
190 failure = 0
191 # enaModules = self.checkAntenna()
192 enaModules = [11,12,13,14]
193
194 for id in range(1,65):
195 if id not in enaModules:
196 continue
197
198 self.commClientObj.open_socket()
199 ip = "192.168.1." + str(id)
200 self.commClientObj.sendData2(cmd, self.rx_buffer, ip)
201 # ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData()
202 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
203 self.commClientObj.close_socket()
204
205 if tmp == "OK":
206 correct = correct + 1
207 else:
208 failure = failure + 1
209
210 if correct == len(enaModules):
211 rpta = "OK"
212 else:
213 rpta = "Failure"
214
215 return rpta
216
217 def getStatus(self, mode, cmd):
151 218
152 219 if mode == 1:
153 220 self.__getStsMode1()
154 221 elif mode == 2:
155 222 self.__getStsMode2()
223 elif mode == 3:
224 self.__getStsMode3()
156 225 else:
157 self.__getStsMode3()
226 self.__getStsMode4(cmd)
158 227
159 228
160 229 def __getStsMode1(self):
161 230 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
162 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
163 seconds = int (self.datarx)
231 self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":")
232 seconds = int (self.rx_buffer)
164 233 # Give 5 seconds to the control modules
165 234 time.sleep(seconds)
166 235 # Checking the module connection
@@ -199,8 +268,8
199 268 def __getStsMode2(self):
200 269
201 270 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
202 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
203 seconds = int (self.datarx)
271 self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":")
272 seconds = int (self.rx_buffer)
204 273 # Give 5 seconds to the control modules
205 274 time.sleep(seconds)
206 275 # Checking the module connection
@@ -264,13 +333,33
264 333
265 334 def __getStsMode3(self):
266 335
267 self.commClientObj.sendData("none", "CHEQUEO:" + self.datarx + ":")
268 seconds = int (self.datarx)
336 self.commClientObj.sendData("none", "CHEQUEO:" + self.rx_buffer + ":")
337 seconds = int (self.rx_buffer)
269 338 # Give 5 seconds to the control modules
270 339 time.sleep(seconds)
271 340
272 341 self.__getModuleFile(filename = "Verificacion")
273
342
343 def __getStsMode4(self, cmd):
344
345 content_str = ""
346 # enaModules = self.checkAntenna()
347 enaModules = [11,12,13,14]
348
349 for id in range(1,65):
350 if id not in enaModules:
351 continue
352
353 self.commClientObj.open_socket()
354 ip = "192.168.1." + str(id)
355 self.commClientObj.sendData2(cmd, self.rx_buffer, ip)
356 ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient()
357 self.commClientObj.close_socket()
358
359 content_str = content_str + tmp
360 # self.__AddingHeader(content_list, title = "Verification_file")
361 #Using tx buffer
362 self.tx_buffer = content_str
274 363
275 364 if __name__ == '__main__':
276 365
General Comments 0
You need to be logged in to leave comments. Login now