def add_connection(self, node_id1, node_id2): connection_id = util_funcs.get_random_id(10) # TODO: check if already exists conn = ConnectionEntity(id=connection_id, connection_id=connection_id , from_node_key = ndb.Key(NodeEntity, node_id1), to_node_key = ndb.Key(NodeEntity, node_id2)) self.ndb_transactions_queue.append(conn.put_async()) return connection_id
def create_session(self , node_id, session_id=None, session_type=0, session_game_master_node_id=None, notify_only_last_few_users=None, anyone_can_join=None): session_id = session_id or util_funcs.get_random_id(10) if(not notify_only_last_few_users): notify_only_last_few_users = -1 notify_only_last_few_users = max(256 , int(notify_only_last_few_users)) self.sessions.insert_one({"session_id":session_id, "node_id":node_id , "created_at":time.time(), "session_type":session_type, "session_game_master_node_id":session_game_master_node_id, "notify_only_last_few_users" :notify_only_last_few_users, anyone_can_join:anyone_can_join}) return session_id
def create_node(self, client_id , addr , addr_internal, port, is_server=False): node_id = ((client_id+"__") if client_id else "")+util_funcs.get_random_id(10) if(is_server): node_id = "server__"+node_id self.nodes.insert_one({"node_id":node_id, "client_id":client_id ,"addr":addr, "addr_internal":addr_internal, "port":port}) return node_id
def send_message_into_network(src_id, dest_id): message = { "id": util_funcs.get_random_id(10), "src_id": src_id, "dest_id": dest_id, "payload": "same random payload" } node_connections[src_id].send(json_util.dumps(message))
def add_connection(self, node_id1, node_id2): connection_id = util_funcs.get_random_id(10) #TODO: check if already exists conn = self.connections.insert_one({ "connection_id": connection_id, "from_node_id": node_id1, "to_node_id": node_id2 }) return connection_id
def create_session(self, name, description, client_id): session_id = util_funcs.get_random_id(10) session = SessionEntity(id=session_id, session_id=session_id, name=name, description=description, client_id=client_id) self.ndb_transactions_queue.append(session.put_async()) return session_id
def add_connection(self, node_id1, node_id2): connection_id = util_funcs.get_random_id(10) # TODO: check if already exists conn = ConnectionEntity(id=connection_id, connection_id=connection_id, from_node_key=ndb.Key(NodeEntity, node_id1), to_node_key=ndb.Key(NodeEntity, node_id2)) self.ndb_transactions_queue.append(conn.put_async()) return connection_id
def create_node(self, client_id, addr, addr_internal, port): node_id = ((client_id + "__") if client_id else "") + util_funcs.get_random_id(10) node = NodeEntity(id=node_id, node_id=node_id, client_id=client_id, addr=addr, addr_internal=addr_internal, port=port) self.ndb_transactions_queue.append(node.put_async()) return node_id
def join_session(self, session_id, node_id, is_anonymous=False, update_in_db=True, anonymous_node_id=None): node_ids = self.session_node_ids_cache.get(session_id) if (node_ids): node_info = node_ids.get(node_id, None) if (node_info): #already in session return node_info if (update_in_db): doc = {"session_id": session_id, "node_id": node_id} if (is_anonymous): anonymous_node_id = anonymous_node_id or "anonymous_" + util_funcs.get_random_id( 10) doc["anonymous_node_id"] = anonymous_node_id result = self.session_nodes.insert_one(doc) if (not result.inserted_id): return None else: doc = {"session_id": session_id, "node_id": node_id} ret = self.session_nodes.find_one(doc) if (ret): anonymous_node_id = ret["anonymous_node_id"] #below code is to only only notify_only_last_few_users node_ids = self.session_node_ids_cache.get(session_id) session = self.get_session_by_id(session_id) notify_only_last_few_users = session.get("notify_only_last_few_users", -1) if ( notify_only_last_few_users != -1 ): # -1 means every one , possitve number mean , last n users will be notified if (len(node_ids) > notify_only_last_few_users and node_ids and not node_ids.get(node_id, None)): #remove the first node_ids.popitem(last=False) if (node_ids): if (not node_ids.get(node_id, None)): node_ids[node_id] = (node_id, anonymous_node_id) return (node_id, anonymous_node_id)
def create_node(self, client_id, addr, addr_internal, port, is_server=False): node_id = ((client_id + "__") if client_id else "") + util_funcs.get_random_id(10) if (is_server): node_id = "server__" + node_id self.nodes.insert_one({ "node_id": node_id, "client_id": client_id, "addr": addr, "addr_internal": addr_internal, "port": port }) return node_id
def create_session(self, node_id, session_id=None, session_type=0, session_game_master_node_id=None, notify_only_last_few_users=None, anyone_can_join=None): session_id = session_id or util_funcs.get_random_id(10) if (not notify_only_last_few_users): notify_only_last_few_users = -1 notify_only_last_few_users = max(256, int(notify_only_last_few_users)) self.sessions.insert_one({ "session_id": session_id, "node_id": node_id, "created_at": time.time(), "session_type": session_type, "session_game_master_node_id": session_game_master_node_id, "notify_only_last_few_users": notify_only_last_few_users, anyone_can_join: anyone_can_join }) return session_id
def join_session(self, session_id , node_id, is_anonymous=False, update_in_db=True, anonymous_node_id=None): node_ids = self.session_node_ids_cache.get(session_id) if(node_ids): node_info = node_ids.get(node_id, None) if(node_info): #already in session return node_info if(update_in_db): doc = {"session_id":session_id, "node_id":node_id} if(is_anonymous): anonymous_node_id = anonymous_node_id or "anonymous_"+util_funcs.get_random_id(10) doc["anonymous_node_id"] = anonymous_node_id result = self.session_nodes.insert_one(doc) if(not result.inserted_id): return None else: doc = {"session_id":session_id, "node_id":node_id} ret = self.session_nodes.find_one(doc) if(ret): anonymous_node_id = ret["anonymous_node_id"] #below code is to only only notify_only_last_few_users node_ids = self.session_node_ids_cache.get(session_id) session =self.get_session_by_id(session_id) notify_only_last_few_users = session.get("notify_only_last_few_users",-1) if(notify_only_last_few_users!=-1):# -1 means every one , possitve number mean , last n users will be notified if(len(node_ids)>notify_only_last_few_users and node_ids and not node_ids.get(node_id, None)):#remove the first node_ids.popitem(last=False) if(node_ids): if(not node_ids.get(node_id, None)): node_ids[node_id] = (node_id, anonymous_node_id) return (node_id, anonymous_node_id)
def load_test_single_server(n=100, k_procs=1, n_messages=1000): #make n node_ids #register n_node_ids #parallel k_threads each of which send a message from src to dest , with random message ids for one minute #stop sending after one minute and see if all messages recieved import gevent from gevent import monkey monkey.patch_all(thread=False) import cookies import config import time from websocket._core import create_connection from bson import json_util import json from time import sleep import util_funcs import random import threading import urllib import os node_ids = json_util.loads(open("node_ids.txt", "r").read()) if (not node_ids or len(node_ids) < n): if (node_ids == None): node_ids = [] while (len(node_ids) < n): node_ids.append(util_funcs.get_random_id(10)) open("node_ids.txt", "w").write(json_util.dumps(node_ids)) node_connections = {} def send_message_into_network(src_id, dest_id): message = { "id": util_funcs.get_random_id(10), "src_id": src_id, "dest_id": dest_id, "payload": "same random payload" } node_connections[src_id].send(json_util.dumps(message)) messages_recieved = [] last_message_recieved = [-1] def recv_message(ws): try: while (True): message = ws.recv() messages_recieved.append(True) last_message_recieved[0] = time.time() except Exception as ex: print "reading connection exception", ex tasklets = [] #called from threads def open_connections(node_ids, i, j, id): print "Spinning a thread ", id, "node_ids from ", i, j for p in range(i, j): node_id = node_ids[p] try: auth_key = cookies.create_signed_value( config.SERVER_SECRET, config.SERVER_AUTH_KEY_STRING, node_id) # ws = create_connection("ws://104.199.129.250:8081/connectV2?auth_key="+auth_key) ws = create_connection("ws://35.xxxxxxxx/connectV3?auth_key=" + auth_key) #print "created connection", ws node_connections[node_id] = ws tasklets.append(gevent.spawn(recv_message, ws)) except Exception as e: print "exception opening connection" pass def close_connections(node_ids, i, j, id): for p in range(i, j): node_id = node_ids[p] node_connections[node_id].close() process_count = 0 start = time.time() while (process_count < k_procs): c = n / k_procs #each thread spins off new connections which inturn spawn gevent threads pid = 1 #os.fork() if (pid != 0): i = c * process_count j = c * (process_count + 1) open_connections(node_ids, i, j, pid) # start sending messages onto network n = 0 while (n < n_messages): try: n += 1 src_id = None dest_id = None while (src_id == dest_id): src_id = node_ids[random.randrange(i, j)] dest_id = node_ids[random.randrange(i, j)] send_message_into_network(src_id, dest_id) except Exception as ex: print "exception sending messages", ex print "sent ", n, " messages onto network process::", pid, time.time( ) - start, "seconds" p = 0 while (p < 50 and len(messages_recieved) < n_messages): gevent.sleep(1) p += 1 print "messages recieved :: pid:: ", pid, len( messages_recieved ), "in", last_message_recieved[0] - start, " seconds " print " closing connections " close_connections(node_ids, i, j, pid) gevent.joinall(tasklets) break else: process_count += 1 '''
def send_message_into_network(src_id, dest_id): message = {"id":util_funcs.get_random_id(10), "src_id": src_id , "dest_id":dest_id, "payload":"same random payload"} node_connections[src_id].send(json_util.dumps(message))
def load_test_single_server(n=100, k_procs = 1, n_messages=1000): #make n node_ids #register n_node_ids #parallel k_threads each of which send a message from src to dest , with random message ids for one minute #stop sending after one minute and see if all messages recieved import gevent from gevent import monkey monkey.patch_all(thread=False) import cookies import config import time from websocket._core import create_connection from bson import json_util import json from time import sleep import util_funcs import random import threading import urllib import os node_ids = json_util.loads(open("node_ids.txt","r").read()) if(not node_ids or len(node_ids)<n): if(node_ids==None): node_ids = [] while(len(node_ids)<n): node_ids.append(util_funcs.get_random_id(10)) open("node_ids.txt","w").write(json_util.dumps(node_ids)) node_connections = {} def send_message_into_network(src_id, dest_id): message = {"id":util_funcs.get_random_id(10), "src_id": src_id , "dest_id":dest_id, "payload":"same random payload"} node_connections[src_id].send(json_util.dumps(message)) messages_recieved = [] last_message_recieved = [-1] def recv_message(ws): try: while(True): message = ws.recv() messages_recieved.append(True) last_message_recieved[0] = time.time() except Exception as ex: print "reading connection exception" , ex tasklets = [] #called from threads def open_connections(node_ids , i , j, id): print "Spinning a thread ", id, "node_ids from ", i , j for p in range(i , j): node_id = node_ids[p] try: auth_key = cookies.create_signed_value(config.SERVER_SECRET , config.SERVER_AUTH_KEY_STRING ,node_id) # ws = create_connection("ws://104.199.129.250:8081/connectV2?auth_key="+auth_key) ws = create_connection("ws://35.xxxxxxxx/connectV3?auth_key="+auth_key) #print "created connection", ws node_connections[node_id] = ws tasklets.append(gevent.spawn(recv_message , ws)) except Exception as e: print "exception opening connection" pass def close_connections(node_ids , i , j, id): for p in range(i , j): node_id= node_ids[p] node_connections[node_id].close() process_count = 0 start = time.time() while(process_count < k_procs): c = n/k_procs #each thread spins off new connections which inturn spawn gevent threads pid = 1#os.fork() if(pid != 0): i = c*process_count j = c*(process_count+1) open_connections(node_ids, i , j, pid) # start sending messages onto network n= 0 while(n<n_messages): try: n+=1 src_id = None dest_id = None while(src_id==dest_id): src_id = node_ids[random.randrange(i, j)] dest_id = node_ids[random.randrange(i, j)] send_message_into_network(src_id, dest_id) except Exception as ex: print "exception sending messages", ex print "sent ", n , " messages onto network process::",pid, time.time()-start, "seconds" p = 0 while(p<50 and len(messages_recieved)<n_messages): gevent.sleep(1) p+=1 print "messages recieved :: pid:: " , pid , len(messages_recieved), "in", last_message_recieved[0] -start, " seconds " print " closing connections " close_connections(node_ids, i , j , pid) gevent.joinall(tasklets) break else: process_count+=1 '''
def create_node(self, client_id, addr, addr_internal, port): node_id = ((client_id + "__") if client_id else "") + util_funcs.get_random_id(10) node = NodeEntity(id=node_id, node_id=node_id , client_id=client_id, addr = addr, addr_internal=addr_internal, port=port) self.ndb_transactions_queue.append(node.put_async()) return node_id
def add_connection(self, node_id1 , node_id2): connection_id = util_funcs.get_random_id(10) #TODO: check if already exists conn = self.connections.insert_one({"connection_id":connection_id , "from_node_id":node_id1, "to_node_id":node_id2}) return connection_id