def thread_bidding_timer(): 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() break
def service_path(msg, sock): if check_service_exists(msg): service_queue.append(msg) bidding_timer() network.send_multicast_message( '{"tag":"bidding","uid":"' + msg['uid'] + '","address":"' + self_ip + '"}', ukey, self_ip) place_bid(msg['uid']) return else: network.send_multicast_message( '{"tag":"info","message":"service does not exist","address":"' + self_ip + '"}', ukey, self_ip)
def finish_bidding(): try: tmp = [] for s in service_queue: del tmp[:] for b in bids: if s['uid'] == b['uid']: tmp.append(b['bid']) for m in my_bids: if s['uid'] == m[1]: if (str(m[0]) >= max(tmp)): print "\tWON" network.send_multicast_message( '{"tag":"winner","address":"' + self_ip + '","service":"' + s['service'] + '"}', ukey, self_ip) services.run_service(s, ukey, self_ip) service_queue.remove(s) my_bids.remove(m) except: pass finally: 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) except: network.send_multicast_message( "error, ERROR - main failed: " + self_ip, ukey, self_ip)
def place_bid(uid): bid = random.randint(1, 100000) my_bids.append((bid, uid)) network.send_multicast_message( '{"tag":"bid","bid":"' + str(bid) + '","uid":"' + uid + '","address":"' + self_ip + '"}', ukey, self_ip)
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)