def send_message_to_peer(self, host, port, message_type, message): counter = 0 total_recvd_message = [] while counter != 1: try: print "Trying to connect to {%s:%d}"%(host, port) comm_peer = comm(host, port) print "Establishing communication with {%s:%d}"%(host, port) comm_peer.out_data(message_type, message) print "Message sent to {%s:%d}"%(host, port) recvd_message = comm_peer.in_data() while(recvd_message != (None, None)): total_recvd_message.append(recvd_message) print "Reply from {%s:%d} ---> %s"%(host, port, total_recvd_message) comm_peer.close_conn() break except KeyboardInterrupt: raise except: counter += 1 print "No response from connected peer {%s:%d} --->%s [%d]"%(host, port, message_type, counter) return total_recvd_message
def __init__(self): self.client = comm() self.commander = commander() #self.sendThread=dataSendingThread(self.client, self.commander) #self.sendThread = m.Process(target=sendData, args=(self.client,self.commander)) #self.sendThread.start() #self.controlThread=controlThread(self.commander) print("starting sensor readings") #self.commander.sensors.start() self.printDelay = 0
def __init__(self): #setup communication with racks self._comm = comm.comm() #create a list of card objects that contain an address data (IP(Static IP!), card number) self._cards = [] #Iterate through IP's or get list directly from DHCP server.... for IP in all ips...: if valid rack IP...: for each valid rack_address (Card) in rack...: temp_card = card(IP, rack_address) self._cards.append(temp_card)
def peer_status(self): not_alive = [] for host, port in self.peer_info.items(): isalive = False try: comm_peer = comm(host, port) comm_peer.out_data('PING','') isalive = True except: not_alive.append(host) if isalive: comm_peer.close_conn() self.p_lock.acquire() try: for offline_peers in not_alive: if self.peer_info[offline_peers]: del self.peer_info[offline_peers] finally: self.p_lock.release()
def handle_incoming_peer_message(self, client_sock): print "Peer {%s:%d} connected to %s"%(self.peer_host, self.peer_port, client_sock.getpeername()) host, port = client_sock.getpeername() #print "Incoming PING from {%s:%d}"%(host, port) try: comm_peer = comm(host, port, client_sock) message_type, message = comm_peer.in_data() if message_type: message_type = message_type.upper() print "Message received:", message_type """ Stores the local file list into dictionary with peer_id as reference to every file """ if message_type == 'FLST': send_message_to_peer(host, port, 'FLRS', self.file_info) elif message_type == 'FLRS': temp = message.copy() temp.update(self.file_info) self.file_info = temp elif message_type == 'PLST': send_message_to_peer(host, port, 'PLST', self.peer_info) elif message_type == 'PLRS': for peer_id in message: if peer_id not in self.peer_info: self.peer_info.append(peer_id) elif message_type == 'FGET': if message in self.file_info.keys(): try: fd = file(message, 'r') f_data = '' start_time = time.time() while True: data = fd.read(2048) if not len(data): break f_data += data fd.close() except: print "Error encountered downloading file" return send_message_to_peer(host, port, 'FRES', f_data) end_time = time.time() download_time = start_time - end_time print download_time elif message_type == 'FRES': path = "/home/tej/Documents/Java Files" fl = [f for f in listdir(path) if isfile(join(path, f))] for files in fl: fd = open(files, 'rb') fd.seek(0,2) size = fd.tell() if size is None: target_file = files with open("/home/tej/Documents/Java Files/" + target_file) as tf: tf.write(message) if message_type not in self.message_handlers: print "Invalid message --> [%s:%s]"%(message_type, message) else: print "Responding to the message --> [%s:%s]"%(message_type, message) self.massage_handlers[message_type] = message except KeyboardInterrupt: raise except: traceback.print_exc() com.close_conn()
import card as card_class import comm test_comm = comm.comm() def test_card_init(): ip = "192.168.1.23" rack_address = 10 test_response = '{"global_id": 12345, "registers":[{"voltage": 53},{"temp": 34}]}' test_comm.set_test_response(test_response) c = card_class.card(test_comm, ip, rack_address) c.print_info() test_card_init()
#import ethernet/sockets communication libraries etc... #import card class import comm as comm main_comm = comm.comm() class main(): def __init__(self): #setup communication with racks self._comm = comm.comm() #create a list of card objects that contain an address data (IP(Static IP!), card number) self._cards = [] #Iterate through IP's or get list directly from DHCP server.... for IP in all ips...: if valid rack IP...: for each valid rack_address (Card) in rack...: temp_card = card(IP, rack_address) self._cards.append(temp_card) #retrieve info about cards in json (iterate through cards and setup object variables based on json returned) for each card in cards: card.update_card() def get(self, global_id): #search through all cards till the one with that global id is found for card in self._cards: if card.global_id = global_id: return card raise Exception("Request for Invalid Card ID, double check the ID and make sure the card is connected") return None