def find_nodes(): network.send_multicast_message( '{"tag":"whois","address":"'+self_ip+'"}',ukey,self_ip) mutex.acquire() try: del nodes[:] if data: for d in data: d = json.loads(d) if d['tag'] == "info" and d['message'] == "whois": if nodes: check = False for n in nodes: n = json.loads(n) if str(d['address']) == str(n['address']): check = True break if not check: nodes.append(json.dumps(d)) else: nodes.append(json.dumps(d)) except: pass finally: del data[:] del console_data[:] mutex.release()
def place_bid(my_bid): network.send_multicast_message( '{"tag":"bid","bid":"' + str(my_bid) + '","address":"' + self_ip + '"}', ukey, self_ip) t = threading.Thread(target=network.send_multicast_message, args=(my_bid, ukey, self_ip)) t.start()
def thread_complex(services, ukey, sender_address): for s in services: s += "}" if s[0] == ",": s = s[1:] network.send_multicast_message(s, ukey, sender_address) time.sleep(3)
def service_path(msg, sock): if check_service_exists(msg): del bids[:] my_bid = random.randint(1, 1000) timer(my_bid, msg) network.send_multicast_message( '{"tag":"bidding","address":"' + self_ip + '"}', ukey, self_ip) place_bid(my_bid) return
def thread_timer(my_bid, msg): TTL = 1 timeout = time.time() + TTL global stop while True: if (time.time() > timeout): stop = True network.send_multicast_message( '{"tag":"timer"}', ukey, self_ip) #dont delete, cycles bid loop finish_bidding(my_bid, msg) break
def finish_bidding(my_bid, msg): try: print bids print "my bid: " + str(my_bid) print "max: " + max(bids) if bids and (str(my_bid) >= max(bids)): print "\tWON" network.send_multicast_message( '{"tag":"winner","address":"' + self_ip + '","service":"' + msg['service'] + '"}', ukey, self_ip) services.run_service(msg, ukey, self_ip) else: print "\tLOST" except: pass # print "finish_bidding failed" finally: del bids[:] network.send_multicast_message( '{"tag":"waiting","address":"' + self_ip + '"}', ukey, self_ip)
def main(): # try: mcast_sock = network.start_multicast_receiver(self_ip) network.send_multicast_message( '{"tag":"info","message":"starting mcast reciever","address":"' + self_ip + '"}', ukey, self_ip) network.send_multicast_message( '{"tag":"info","message":"starting tcp server","address":"' + self_ip + '"}', ukey, self_ip) tcp_sock = network.start_tcp_server(self_ip) network.send_multicast_message( '{"tag":"info","message":"waiting for message","address":"' + self_ip + '"}', ukey, self_ip) wait_for_message(mcast_sock)
def whois_path(): servs = json.dumps(services.all_services()) network.send_multicast_message( '{"tag":"info","message":"whois","address":"' + self_ip + '","services":' + servs + '}', ukey, self_ip)
def complex_send(msg_lst): message = build_complex(msg_lst) network.send_multicast_message(message,ukey,self_ip)
def send_message(message): print message network.send_multicast_message(message,ukey,self_ip)
def handle_ctrl_c(signal, frame): print "\nSIGNAL: ctrl c" network.send_multicast_message('{"tag":"stop"}',ukey,self_ip) network.send_tcp_message(self_ip,"stop") sys.exit(0)