##// END OF EJS Templates
imanay -
r55:56
parent child
Show More
@@ -1,100 +1,100
1 1 #import tftpy
2 2 import socket
3 3
4 4 class UDPComm:
5 5
6 6 __HEADER = "ABS"
7 7
8 8 def __init__(self, ipSource, ipDestino, portDestino, asServer=False):
9 9
10 10 self.ipSource = ipSource
11 11 self.ipDestino = ipDestino
12 12 self.portDestino = portDestino
13 13 self.addr = (ipDestino,portDestino)
14 14 self.answer = ipDestino #test
15 15 self.mode = "none"
16 16
17 17 self.openSocket(asServer)
18 18
19 19 def openSocket(self, asServer):
20 20
21 21 #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM)
22 22 self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0)
23 23 # self.socket_c.connect((self.ipDestino, self.portDestino))
24 24
25 25 if asServer:
26 26 self.configAsServer()
27 27 self.mode = "server"
28 28 else:
29 29 self.configAsClient()
30 30 self.mode = "client"
31 31
32 32 def configAsClient(self):
33 33 #Configurar broadcast
34 34 self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
35 35
36 36 def configAsServer(self):
37 37
38 38 self.socket_c.bind(self.addr)
39 39 print "\nServer initialized"
40 40
41 41 def waitData(self, nbytes = 16384):
42 42
43 43 print "\nWaiting some data"
44 44 trama_rx, self.answer = self.socket_c.recvfrom(nbytes)
45 print "\nThis socket has received some data"
45 print "\nThis socket has received some data from:"
46 46 print self.answer
47 47 ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx)
48 48
49 49 return ipSource, ipDestino, cmd, data
50 50
51 51 def sendData(self, cmd, data):
52 52
53 53 if self.portDestino == 7000:
54 54 trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":"
55 55 else:
56 56 trama_tx = data
57 57
58 58 if self.mode == "client":
59 59 destiny = self.addr
60 60 else:
61 61 destiny = self.answer
62 62 # Send messages
63 63 if(self.socket_c.sendto(trama_tx, destiny)):
64 print "Sending message:[" + trama_tx + "]"
64 print "Sending message:[" + trama_tx + "] to " + str(destiny)
65 65
66 66 def __getTrama(self, trama):
67 67
68 68 FrameList = trama.split(':')
69 69
70 70 header = FrameList[0]
71 71 ipSource = FrameList[1]
72 72 ipDestino = FrameList[2]
73 73 cmd = FrameList[3]
74 74 data = FrameList[4]
75 75 trash = FrameList[5]
76 76
77 77 return ipSource, ipDestino, cmd, data
78 78
79 79 #class FTPComm:
80 80 #
81 81 # ftp_servidor = 'ftp.servidor.com'
82 82 # ftp_usuario = 'miusuario'
83 83 # ftp_clave = 'miclave'
84 84 # ftp_raiz = '/public_html'
85 85 #
86 86 # def __init__(self):
87 87 #
88 88 # self.client = tftpy.TftpClient(self.ftp_servidor, '69')
89 89 #
90 90 #
91 91 # def sendFile(self, filename):
92 92 #
93 93 # self.client.upload(filename)
94 94 #
95 95 #if __name__ == '__main__':
96 96 #
97 97 # obj = FTPComm()
98 98
99 99
100 100 No newline at end of file
@@ -1,282 +1,280
1 1 import os
2 2 import library
3 3 import time
4 4
5 5 class ABSServer:
6 6
7 7 def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", portDestino=7000, ipDestino2="192.168.1.255", portDestino2=5500, ftpPortDestino=None):
8 8
9 9 self.ipSource = ipSource
10 10 self.ipDestino = ipDestino
11 11 self.portDestino = portDestino
12 12
13 13 self.ipDestino2 = ipDestino2
14 14 self.portDestino2 = portDestino2
15 15
16 16 self.ftpPortDestino = ftpPortDestino
17 17 self.experiment_name = "default"
18 18 self.tx_buffer = "default"
19 19
20 20 self.createObjects()
21 21
22 22 def createObjects(self):
23 23
24 24 asServer = True
25 25 self.commServerObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
26 26 self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
27 27 #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
28 28
29 29
30 30 def waitRequest(self):
31 31
32 32 ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData()
33 33
34 34 datarpta = "OK"
35 35
36 36 if cmd == "SNDF":
37 37 self.sendFile2Modules()
38 38
39 39 if cmd == "CHGB":
40 40 self.changeBeam()
41 41
42 42 if cmd == "ANST":
43 43 self.getStatus(mode=3)
44 44 datarpta = self.tx_buffer
45 45
46 46 self.commServerObj.sendData(cmd=cmd, data=datarpta)
47 47
48 48 def checkModule(self, address):
49 49
50 50 cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null"
51 51 status = os.system(cmd)
52 52
53 53 if status == 256:
54 54 return False
55 55
56 56 return True
57 57
58 58 def __writeReport(self, enaModules):
59 59
60 60 status_array = ["Status of modules\n"]
61 61 status_array.append("----------------\n")
62 62
63 63 for address in range(1,65):
64 64 if address in enaModules:
65 65 # status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n")
66 66 status_array.append("192.168.1." + str(address) + " [1 1]\n")
67 67 else:
68 68 status_array.append("192.168.1." + str(address) + " [0 0]\n")
69 69
70 70 f = open("module_status.txt","w")
71 71 f.writelines(status_array)
72 72 f.close()
73 73
74 74 def checkAntenna(self):
75 75
76 76 """
77 77 Direccion de los modulos de las antenas:
78 78
79 79 Norte : 01-16
80 80 Este : 17-32
81 81 Oeste: : 33-48
82 82 Sur : 49-64
83 83
84 84 """
85 85
86 86 enaModules = []
87 87
88 88 for address in range(1,65):
89 89 if self.checkModule(address):
90 90 enaModules.append(address)
91 91
92 92 self.__writeReport(enaModules)
93 93 return enaModules
94 94
95 95 def sendFile2Modules(self):
96 96
97 97 #Needed for the loop
98 98 rx_frame_list = self.datarx.split('\n',2)
99 99
100 experiment_name = rx_frame_list[0]
100 self.experiment_name = rx_frame_list[0]
101 101 experiment_number = rx_frame_list[1]
102 102 str_control_modules = rx_frame_list[2]
103 103
104 104 lst_control_modules = str_control_modules.split("------\n")
105 105
106 106 enaModules = self.checkAntenna()
107 107
108 108 for address in range(1,65):
109 109
110 110 if address not in enaModules:
111 111 continue
112 112
113 self.__writeModuleFile(experiment_name, lst_control_modules[address-1])
114
115 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name
113 self.__writeModuleFile(self.experiment_name, lst_control_modules[address-1])
114
115 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + self.experiment_name
116 116 print cmd
117 117 os.system(cmd)
118
119 self.experiment_name = experiment_name
120 118
121 119 self.__loadFile()
122 120
123 121 def __writeModuleFile(self, filename, str):
124 122
125 123 fobj = open(filename,"w")
126 124 fobj.write(filename + "\n")
127 125 fobj.write("------\n")
128 126 fobj.write(str)
129 127 fobj.write("------\n")
130 128 fobj.close()
131 129
132 130 def __readModuleFile(self, filename):
133 131
134 132 fobj1 = open(filename,"r")
135 133 file_list_1 = fobj1.readlines()
136 134 fobj1.close()
137 135 content_str = ''.join(file_list_1[2:-1])
138 136
139 137 return content_str
140 138
141 139 def __loadFile(self):
142 140
143 141 #Working with the UDP socket
144 142 self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":")
145 143 self.commClientObj.sendData("none", "CAMBIA:0:")
146 144
147 145 def changeBeam(self):
148 146
149 147 #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
150 148 self.commClientObj.sendData("CAMBIA:" + self.datarx + ":")
151 149
152 150 def getStatus(self,mode):
153 151
154 152 if mode == 1:
155 153 self.__getStsMode1()
156 154 elif mode == 2:
157 155 self.__getStsMode2()
158 156 else:
159 157 self.__getStsMode3()
160 158
161 159
162 160 def __getStsMode1(self):
163 161 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
164 162 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
165 163 seconds = int (self.datarx)
166 164 # Give 5 seconds to the control modules
167 165 time.sleep(seconds)
168 166 # Checking the module connection
169 167 module_list = self.connection_status(10)
170 168 #Generating the complete report
171 169 module = 1
172 170 number_of_modules = 16
173 171 filename1 = "Verificacion"
174 172 filename2 = "report.txt"
175 173 fobj2 = open(filename2,"w")
176 174 fobj2.write("Verification_file\n")
177 175 fobj2.write("-----------------\n")
178 176 fobj2.close()
179 177 while module <= number_of_modules:
180 178 if module_list[module -1] == "1":
181 179 #Preparing and doing the tftp command
182 180 cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1
183 181 print cmd
184 182 os.system(cmd)
185 183 # Getting data from the control module file
186 184 fobj1 = open(filename1,"r")
187 185 file_list_1 = fobj1.readlines()
188 186 fobj1.close()
189 187 content = file_list_1[2:-1]
190 188 #
191 189 fobj2 = open(filename2,"a")
192 190 if base == 10:
193 191 fobj2.write("S" + str(module) + "\n")
194 192 else:
195 193 fobj2.write("N" + str(module) + "\n")
196 194 fobj2.writelines(content)
197 195 fobj2.write("------\n")
198 196 fobj2.close()
199 197 module = module + 1
200 198
201 199 def __getStsMode2(self):
202 200
203 201 #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
204 202 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
205 203 seconds = int (self.datarx)
206 204 # Give 5 seconds to the control modules
207 205 time.sleep(seconds)
208 206 # Checking the module connection
209 207 enaModules = self.checkAntenna()
210 208 #Generating the complete report
211 209 filename1 = "Verificacion"
212 210 line1 = "Verification_file\n"
213 211 line2 = "-----------------\n"
214 212 report_list = [line1, line2]
215 213
216 214 for address in range(1,65):
217 215
218 216 if address not in enaModules:
219 217 continue
220 218 #Preparing and doing the tftp command
221 219 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1
222 220 print cmd
223 221 os.system(cmd)
224 222 #Sub_header
225 223 report_list.append("ABS_" + str(address) + "\n")
226 224 # Content
227 225 fobj1 = open(filename1,"r")
228 226 file_list_1 = fobj1.readlines()
229 227 fobj1.close()
230 228 content = ''.join(file_list_1[2:-1])
231 229 report_list.append(content)
232 230 #Ending
233 231 report_list.append("------\n")
234 232 #print "\nFinalizado"
235 233 self.tx_buffer = ''.join(report_list)
236 234
237 235 def __AddingHeader(self,content_list, title):
238 236
239 237 line1 = title + "\n"
240 238 line2 = "-----------------\n"
241 239 header_list = [line1, line2]
242 240 verification_list = header_list + content_list
243 241 # Arming the frame
244 242 self.tx_buffer = ''.join(verification_list)
245 243
246 244 def __getModuleFile(self, filename):
247 245
248 246 enaModules = self.checkAntenna()
249 247 content_list = []
250 248 for address in range(1,65):
251 249
252 250 if address not in enaModules:
253 251 continue
254 252 #Preparing and doing the tftp command
255 253 cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename
256 254 print cmd
257 255 os.system(cmd)
258 256 #Sub_header
259 257 content_list.append("ABS_" + str(address) + "\n")
260 258 # From module file to list
261 259 content_str = self.__readModuleFile(filename)
262 260 content_list.append(content_str)
263 261 content_list.append("------\n")
264 262
265 263 self.__AddingHeader(content_list, title = "Verification_file")
266 264
267 265 def __getStsMode3(self):
268 266
269 267 self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":")
270 268 seconds = int (self.datarx)
271 269 # Give 5 seconds to the control modules
272 270 time.sleep(seconds)
273 271
274 272 self.__getModuleFile(filename = "Verificacion")
275 273
276 274
277 275 if __name__ == '__main__':
278 276
279 277 absObj = ABSServer()
280 278
281 279 while 1:
282 280 absObj.waitRequest() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now