def connectZMQ(self): context = zmq.Context() socket = context.socket(zmq.SUB) print("Tapas Player connection with zmq-Entity") socket.connect("tcp://192.168.1.2:4444") curr_localPort=getLocalport() listenTo="192.168.1.10:"+str(curr_localPort) if isinstance(listenTo,bytes): listenTo = listenTo.decode('ascii') socket.setsockopt_string(zmq.SUBSCRIBE, listenTo) print('connLost is '+ str(self.connLost)) while self.connLost==False: try: print('connLost is '+ str(self.connLost)) string_received = socket.recv_string() print("=======================================") print('I RECEIVED A MESSAGE') print('IT IS MEANT TO BE FOR '+str(listenTo)) print('MY CURRENT LOCALPORT I HAVE TO LISTEN TO IS '+str(curr_localPort)) print("=======================================") listenTo, priostat = string_received.split() self.setPrio(int(float(priostat))) if not self.connLost: print colored("[_zmq got Answer()] I call fetchNextSegment() from if [1]",'red'); self.fetchNextSegment(curr_localPort) print colored("[_zmq got Answer ()] I called fetchNextSegment() from if [2]",'red'); if self.connLost: print colored('----------------------->>>>> Do not request because connection is lost','green') except KeyboardInterrupt: break
def listen_to_alive_messages(address, port): global context # context = zmq.Context() socket = context.socket(zmq.SUB) socket.bind("tcp://*:%s" % port) socket.setsockopt_string(zmq.SUBSCRIBE, '') print("Listening to ALIVE messages on %s:%s.." % (address, port)) db = mysql.connect(host="localhost", user="******", passwd="hydragang", database="data_nodes") cursor = db.cursor() while True: try: received_message = socket.recv_string(flags=zmq.NOBLOCK) node_id, message = received_message.split() node_id = int(node_id) # print("Tracker: received %s on %s:%s" % (received_message, address, port)) cursor.execute( "UPDATE node_table SET is_node_alive = TRUE WHERE node_number=%d" % node_id) cursor.execute( "UPDATE file_table SET is_node_alive = TRUE WHERE node_number=%d" % node_id) db.commit() # print(cursor.rowcount) except zmq.Again as e: cursor.execute( "UPDATE node_table SET is_node_alive = FALSE WHERE last_modified < NOW() - INTERVAL 1 MINUTE" ) cursor.execute( "UPDATE file_table SET is_node_alive = FALSE WHERE last_modified < NOW() - INTERVAL 1 MINUTE" ) db.commit()
def connectZMQ(self): context = zmq.Context() socket = context.socket(zmq.SUB) print("Tapas Player connection with zmq-Entity") socket.connect("tcp://192.168.1.2:4444") listenTo = "192.168.1.10:" + str(os.getpid()) print colored('im listening to ' + str(listenTo), 'green') #listenTo="192.168.1.2:"+str(getLocalport()) if isinstance(listenTo, bytes): listenTo = listenTo.decode('ascii') socket.setsockopt_string(zmq.SUBSCRIBE, listenTo) while True: try: string_received = socket.recv_string() listenTo, delegated_level = string_received.split() print colored("received prio status", 'red') print colored("[3] execute fetchNExtSegment() from here", 'blue') print colored("[!!!!!!] level delegate i received: " + str(delegated_level)) self.setDelegatedLevel(int((delegated_level))) self.fetchNextSegment() except KeyboardInterrupt: break
def setup_pub_sub_socket(isServer, server_addr='', server_port=''): context = zmq.Context() socket = None if isServer: socket = context.socket(zmq.PUB) socket.bind("tcp://*:{server_port}") else: socket = context.socket(zmq.SUB) socket.connect("tcp://{server_addr}:{server_port}") logger.info("PUB/SUB set up") socket.setsockopt_string(zmq.SUBSCRIBE, "")
def connectZMQ(self): context = zmq.Context() socket = context.socket(zmq.SUB) print("[Network Control] Network Control connected to Decision Entity") socket.connect("tcp://132.187.12.97:6666") listenTo="6" if isinstance(listenTo, bytes): listenTo = listenTo.decode('ascii') socket.setsockopt_string(zmq.SUBSCRIBE, listenTo) #SPM performs prioritization of certain flows if mechanism=="spm": while True: string_received = socket.recv_string() listenTo, clientInfo, prio = string_received.split() prio=int(prio) client_info_splitted=clientInfo.split(":") client_ip=client_info_splitted[0] client_port=client_info_splitted[1] #Control whether it should be downloaded via priority queue or best-effort queue (default!) if prio==1: self.socket_trash.send("CMD addfilter GW1 "+str(client_ip)+" "+str(client_port)+" GW1_PRIORITY_1 1\n") else: print "deprio clients" self.socket_trash.send("CMD deletefilter GW1 "+str(client_ip)+" "+str(client_port)+" GW1_PRIORITY_1 1\n") #NADE performs bandwidth reservation for clients if mechanism=="nade": while True: string_received = socket.recv_string() listenTo, messageType,clientInfo, share, flow_id = string_received.split() print clientInfo client_info_splitted=clientInfo.split(":") client_ip=client_info_splitted[0] client_port=client_info_splitted[1] #if messagetype is register, then the flow needs to be added if messageType=="update": print ("UPDATE BANDWIDTH SHARE") comm = "CMD changebw "+str(flow_id)+" "+str(share)+"kbit 1\n" self.socket_trash.send("CMD changebw "+str(flow_id)+" "+str(float(share)*float(self.curr_bw))+"kbit 1\n") print clientInfo elif messageType == "register": print "REGISTER NEW INSTANCE" comm = "CMD addapp "+str(flow_id)+" "+str(client_ip)+" "+str(client_port)+" GW1\n" self.socket_trash.send("CMD addapp "+str(flow_id)+" "+str(client_ip)+" "+str(client_port)+" GW1\n") elif messageType == "delete": print "DELETE OLD INSTANCE" comm = "CMD delapp "+str(flow_id)+" "+str(client_ip)+" "+str(client_port)+" GW1\n" self.socket_trash.send(comm)
def configure_port(ipPort, portType, connectionType, openTimeOut=False): context = zmq.Context() socket = context.socket(portType) if (portType == zmq.SUB): socket.setsockopt_string(zmq.SUBSCRIBE, "") if (openTimeOut): socket.setsockopt(zmq.LINGER, 0) socket.setsockopt(zmq.AFFINITY, 1) socket.setsockopt(zmq.RCVTIMEO, 800) if (connectionType == "connect"): socket.connect("tcp://" + ipPort) else: socket.bind("tcp://" + ipPort) return socket, context
def configure_multiple_ports(IPs, ports, portType, openTimeOut=False): context = zmq.Context() socket = context.socket(portType) if (portType == zmq.SUB): socket.setsockopt_string(zmq.SUBSCRIBE, "") if (openTimeOut): socket.setsockopt(zmq.LINGER, 0) socket.setsockopt(zmq.AFFINITY, 1) socket.setsockopt(zmq.RCVTIMEO, 700) if (isinstance(IPs, list)): for ip in IPs: socket.connect("tcp://" + ip + ":" + ports) else: tempPorts = ports.copy() random.shuffle(tempPorts) for port in tempPorts: socket.connect("tcp://" + IPs + ":" + port) return socket, context
def configure_multiple_ports(IPs, port, portType, openTimeOut=False, Time=0, subTopic=False, Topics=[]): context = zmq.Context() socket = context.socket(portType) if (portType == zmq.SUB and subTopic == True): for topic in Topics: socket.setsockopt_string(zmq.SUBSCRIBE, topic) elif (portType == zmq.SUB and subTopic == False): socket.setsockopt_string(zmq.SUBSCRIBE, "") if (openTimeOut): socket.setsockopt(zmq.RCVTIMEO, Time) socket.setsockopt(zmq.LINGER, 0) socket.setsockopt(zmq.AFFINITY, 1) for IP in IPs: socket.connect("tcp://" + IP + ":" + port) time.sleep(1) return socket, context
def configure_port(ipPort, portType, connectionType, openTimeOut=False, Time=0, subTopic=False, Topics=[]): context = zmq.Context() socket = context.socket(portType) if (portType == zmq.SUB and subTopic == True): for topic in Topics: socket.setsockopt_string(zmq.SUBSCRIBE, topic) elif (portType == zmq.SUB and subTopic == False): socket.setsockopt_string(zmq.SUBSCRIBE, "") if (openTimeOut): socket.setsockopt(zmq.RCVTIMEO, Time) socket.setsockopt(zmq.LINGER, 0) socket.setsockopt(zmq.AFFINITY, 1) if (connectionType == "connect"): socket.connect("tcp://" + ipPort) else: socket.bind("tcp://" + ipPort) return socket, context
def add_to_DB(port): global context socket = context.socket(zmq.SUB) socket.bind("tcp://*:%s" % port) socket.setsockopt_string(zmq.SUBSCRIBE, '') print("Listening to ADD_To_DB messages on port: %s.." % (port)) while True: # try: received_message = socket.recv_string(flags=zmq.NOBLOCK) data_node_id, client_id, file_name = socket.recv(2048).decode( 'utf-8').split('#') print("adding file from master tracker " + data_node_id) db = mysql.connect(host="localhost", user="******", passwd="hydragang", database="data_nodes") cursor = db.cursor() cursor.execute( "INSERT INTO file_table (user_id,node_number,file_name,file_path) VALUES (" + client_id + "," + data_node_id + ",'" + file_name + "','" + file_name + "');") db.commit() cursor.close() print("file added into DB!")
print("\x1B]0;%s\x07" % msg) context = zmq.Context() # starting publisher for data packets print("Starting ZMQ publisher…") socket2 = context.socket(zmq.PUB) socket2.bind("tcp://*:5564") printzmq("Started ZMQ publisher.") # Socket to talk to Sylvain's C code printzmq("Connecting to PicTalk") socket = context.socket(zmq.SUB) socket.connect("tcp://localhost:5563") socket.setsockopt_string(zmq.SUBSCRIBE, "IQ") printzmq("Connected to PicTalk.") # configuring decode function decode = Decode() # configuring main thread mainThread = Thread(target=mainFunction, args=(socket, decode)) # starting main thread mainThread.start() #resetThread = Thread(target = resetFreq, args=(socket2,decode)) #resetThread.start() resetFreq(socket2, decode)
port = "5556" if len(sys.argv) > 1: port = sys.argv[1] int(port) if len(sys.argv) > 2: port1 = sys.argv[2] int(port1) # Socket to talk to server context = zmq.Context() socket = context.socket(zmq.SUB) print("Collecting updates from weather server...") socket.connect("tcp://" + myip + ":" + port) if len(sys.argv) > 2: socket.connect("tcp://" + myip + ":" + port1) # Subscribe to zipcode, default is NYC, 10001 topicfilter = "" #topicfilter = "" socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter) # Process 5 updates total_value = 0 while True: string = socket.recv() print("Received :", string)
topic = input("Enter topic id:") socket.send("%s %s %i %i %s" % ("sub", myIPaddress, topic, -1, "blah")) message = socket.recv() print(message) socket.close() #sub_socket = context.socket(zmq.SUB) socket = context.socket(zmq.SUB) event_serviceIP = "tcp://10.0.0.1:5557" socket.connect(event_serviceIP) #IPfilter = "" IPfilter = myIPaddress # Python 2 - ascii bytes to unicode str if isinstance(IPfilter, bytes): IPfilter = IPfilter.decode('ascii') # any subscriber must use the SUBSCRIBE to set a subscription, i.e., tell the # system what it is interested in socket.setsockopt_string(zmq.SUBSCRIBE, IPfilter) while True: #print("Waiting for notifications from publishers.....") string = socket.recv_string() #print("***Received***:",string) incomingIP, message = string.split() #print("***Received***:",incomingIP,message) #if incomingIP==myIPaddress: print("Kohli hits century number:", message.encode('ascii', 'ignore'))