def __setitem__(self, key, value): self.op_count += 1 try: value = json.dumps(value) while len(value) > 40000: value_to_send = value[:40000] value = value[40000:] msg = { 'method': 'set', 'key': key, 'value': value_to_send, 'addr': [self.r_addr, self.r_port], 'next': 1 } udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) msg = { 'method': 'set', 'key': key, 'value': value, 'addr': [self.r_addr, self.r_port] } udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(65535) # buffer size is 1024 bytes self.delay() except Exception as e: print 'set item', e return None
def __setitem__(self, key, value): msg = { 'method': 'set', 'key': key, 'value': json.dumps(value), 'addr': [self.r_addr, self.r_port] } udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes
def __getitem__(self, key): self.op_count += 1 msg = {'method': 'get', 'key': key, 'addr': [self.r_addr, self.r_port]} try: udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(65535) # buffer size is 1024 bytes ans = json.loads(data) value = '' while 'next' in ans: v += ans['result'] data, addr = self.sock.recvfrom( 65535) # buffer size is 1024 bytes ans = json.loads(data) value += ans['result'] self.delay() return json.loads(value) except Exception as e: print 'get item', e return None
def command_handler(data, addr): if data == 'reload_source': print 'reloading' reload(udp3) print 'sending' request = { 'method': "reload_source", 'addr': "127.0.0.1", 'port': 5000 } for addr, port in pool: send(addr, port, json.dumps(request)) print 'finish' if data == 'exit': print 'closing' event.set() for addr, port in pool: send(addr, port, "exit") print 'finish'
def command_handler(data, addr): if data == 'reload_source': print 'reloading' reload(udp3) print 'sending' request = { 'method' : "reload_source", 'addr' : "127.0.0.1", 'port' : 5000 } for addr, port in pool: send(addr, port, json.dumps(request)) print 'finish' if data == 'exit': print 'closing' event.set() for addr, port in pool: send(addr, port, "exit") print 'finish'
def getCallback(ans): if type({}) == type(ans): #if len(ans[hKey]) < 100: # print ans[hKey] #else : # print 'Big value' res = ans[hKey] while len(res) > 40000: msg = {'result' : res[:40000], 'next' : 1} res = res[40000:] udp_common.send(addr[0], addr[1], msg) #if len(ans[hKey]) > 40000: #udp_common.send(addr[0], addr[1], str(json.dumps('Too Long'))) #else: # udp_common.send(addr[0], addr[1], str(ans[hKey])) msg = {'result' : res[:40000]} udp_common.send(addr[0], addr[1], json.dumps(msg)) else: udp_common.send(addr[0], addr[1], json.dumps({'result' : 'null'}))
def getCallback(ans): if type({}) == type(ans): #if len(ans[hKey]) < 100: # print ans[hKey] #else : # print 'Big value' res = ans[hKey] while len(res) > 40000: msg = {'result': res[:40000], 'next': 1} res = res[40000:] udp_common.send(addr[0], addr[1], msg) #if len(ans[hKey]) > 40000: #udp_common.send(addr[0], addr[1], str(json.dumps('Too Long'))) #else: # udp_common.send(addr[0], addr[1], str(ans[hKey])) msg = {'result': res[:40000]} udp_common.send(addr[0], addr[1], json.dumps(msg)) else: udp_common.send(addr[0], addr[1], json.dumps({'result': 'null'}))
("127.0.0.1", 5003), ("127.0.0.1", 5004), ("127.0.0.1", 5005), ] node_pool = map(lambda x: simple_node.SimpleNode(x[0], x[1], simple_node), addr_pool) e1 = threading.Event() thread_pool = map( lambda x: threading.Thread(target=listen, args=(x.handle, x.addr, x.port, e1.isSet)), node_pool) t1 = threading.Thread(target=listen, args=(commandh.build_command_handler( e1, addr_pool, commandh), "127.0.0.1", 5000, e1.isSet)) t1.start() for t in thread_pool: t.start() for a1, p1 in addr_pool: for a2, p2 in addr_pool: send( a1, p1, '{"method":"connect", "target_addr":"%s", "target_port" : %s}' % (a2, p2)) import sha #node_pool[0].content_table[sha.new('123').hexdigits()] = 'YEAH' for t in thread_pool: t.join()
def setCallback(ans): udp_common.send(addr[0], addr[1], str(ans))
def __setitem__(self, key, value): msg = {'method' : 'set', 'key' : key, 'value' : json.dumps(value), 'addr' : [self.r_addr, self.r_port]} udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes
def __getitem__(self, key): msg = {'method' : 'get', 'key' : key, 'addr' : [self.r_addr, self.r_port]} udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes return json.loads(data)
import sys, udp_common udp_common.send(sys.argv[1], int(sys.argv[2]), sys.argv[3])
import threading from udp_common import send, listen def simple_echo(data, addr): print data, addr e1 = threading.Event() t1 = threading.Thread(target=listen, args=(simple_echo, "127.0.0.1", 5000, e1.isSet)) t1.start() import time send("127.0.0.1", 5000, "Hi") time.sleep(0.5) send("127.0.0.1", 5000, "H") time.sleep(0.5) send("127.0.0.1", 5000, "Hi") send("127.0.0.1", 5000, "Hi") e1.set() t1.join()
def __getitem__(self, key): msg = {'method': 'get', 'key': key, 'addr': [self.r_addr, self.r_port]} udp_common.send(self.boot_addr, self.boot_port, json.dumps(msg)) data, addr = self.sock.recvfrom(1024) # buffer size is 1024 bytes return json.loads(data)
print 'closing' event.set() for addr, port in pool: send(addr, port, "exit") print 'finish' return command_handler if __name__ == '__main__': addr_pool = [ ("127.0.0.1", 5001), ("127.0.0.1", 5002), ("127.0.0.1", 5003), ("127.0.0.1", 5004), ("127.0.0.1", 5005), ] node_pool = map(lambda x: simple_node.SimpleNode(x[0], x[1], simple_node), addr_pool) e1 = threading.Event() thread_pool = map(lambda x: threading.Thread(target=listen, args=(x.handle, x.addr, x.port, e1.isSet)), node_pool) t1 = threading.Thread(target=listen, args=(commandh.build_command_handler(e1, addr_pool, commandh), "127.0.0.1", 5000, e1.isSet)) t1.start() for t in thread_pool: t.start() for a1, p1 in addr_pool: for a2, p2 in addr_pool: send(a1, p1, '{"method":"connect", "target_addr":"%s", "target_port" : %s}' % (a2, p2)) import sha #node_pool[0].content_table[sha.new('123').hexdigits()] = 'YEAH' for t in thread_pool: t.join()
def sendRequest(self, addr, port, request): request.update({'addr' : self.addr, 'port' : self.port, 'nodeID' : self.nodeID}) send(addr, port, json.dumps(request))