def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-test.log" logging.basicConfig(filename=strLogFile, level=logging.INFO, format='%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(strLogFile,level=lf.DEBUG) logFormatter.start() logging.info("start the test script of the client") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, 1111) sock.connect(server_address) aMessages = [] logging.info("exit without previous registration") sendMessage(sock,"exit") time.sleep(2) sock.close() logFormatter.stopReadingLogs()
def __init__(self, client_name, interaction_manager, ip="127.0.0.1", port=1111): # store reference to interaction manager self._im = interaction_manager # if log dir doesn't exist ... if not os.path.exists(TCPClient.LOG_DIR): # ... create it os.makedirs(TCPClient.LOG_DIR) # new log path str_log_file = TCPClient.LOG_DIR + "client-interactionmanager.log" # create logger logging.basicConfig(filename=str_log_file, level=logging.DEBUG, format='%(levelname)s %(relativeCreated)6d %(threadName)s %(message)s (%(module)s.%(lineno)d)', filemode='w') self.logFormatter = lf.LogFormatter(str_log_file, level=lf.DEBUG) self.logFormatter.start() logging.info("Log initialized") # create socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) # try to connect in a loop with 3 sec sleep while True: try: logging.info("Try to connect to connectionmanager on %s:%s" % server_address) self.sock.connect(server_address) logging.info("Connection established!") break except socket.error, exc: logging.info("Connection timeout - retry in 3 seconds.") time.sleep(3)
def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-foo2.log" logging.basicConfig(filename=strLogFile, level=logging.DEBUG, format='%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(strLogFile,level=lf.INFO) logFormatter.start() logging.info("start foo2") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip + ":" + str(port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) sock.connect(server_address) sock.settimeout(1) aMessages = [] t = threading.Thread(target=readMessages, args=(sock, aMessages)) t.start() logging.info("register fooTwo") sendMessage(sock,"register:fooTwo") myFoo = Foo() nCpt = 0 try: while True: logging.info("send message") sendMessage(sock,"call:tablet.fooOne.foo|2.0") for strMessage in aMessages: if strMessage != "": fields = strMessage.split("|") logging.info("received message from " + str(fields[0])) method = fields[1] params = fields[2] getattr(myFoo,method)(params) del aMessages[:] time.sleep(1) except KeyboardInterrupt: global runningReading logging.info("Stop the client") sendMessage(sock,"exit") runningReading = False t.join() sock.close() logFormatter.stopReadingLogs()
def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-multiple.log" logging.basicConfig( filename=strLogFile, level=logging.DEBUG, format= '%(levelname)s %(relativeCreated)6d %(threadName)s %(message)s (%(module)s.%(lineno)d)', filemode='w') logFormatter = lf.LogFormatter(strLogFile) logFormatter.start() logging.info("start the test script of the client") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, 1111) sock.connect(server_address) sock.settimeout(1) aMessages = [] t = threading.Thread(target=readMessages, args=(sock, aMessages)) t.start() logging.info("register multiple") sendMessage(sock, "register:multiple") myMultiple = Multiple() try: while True: for strMessage in aMessages: if strMessage != "": fields = strMessage.split("|") logging.info("received message from " + str(fields[0])) method = fields[1] params = fields[2] getattr(myMultiple, method)(params) del aMessages[:] logging.debug("send message") sendMessage(sock, "call:tablet.[stuff,test].paf|0.5") sendMessage(sock, "call:tablet.*.pif|0.5") #sendMessage(sock,"call:nao.ALTextToSpeech.say 'test'") time.sleep(5) except KeyboardInterrupt: global runningReading logging.info("Stope the client") runningReading = False t.join() sock.close() logFormatter.stopReadingLogs()
def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-test.log" logging.basicConfig( filename=strLogFile, level=logging.DEBUG, format= '%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(strLogFile, level=lf.INFO) logFormatter.start() logging.info("start the test script of the client") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, 1111) sock.connect(server_address) aMessages = [] logging.info("register bad") sendMessage(sock, "register:bad") logging.info("unknown device") sendMessage(sock, "call:fake.test.test|param") time.sleep(1) logging.info("Fake module on tablet") sendMessage(sock, "call:tablet.fake.test|param") time.sleep(1) logging.info("Fake module on nao") sendMessage(sock, "call:nao.fake.fake|'test'") time.sleep(1) logging.info("Fake method on nao") sendMessage(sock, "call:nao.ALTextToSpeech.fake|'test'") time.sleep(1) logging.info("No parameters") sendMessage(sock, "call:tablet.test.test") time.sleep(1) logging.info("Exit abruptly") logFormatter.stopReadingLogs() sys.exit()
def main(): global runningAllServer global robotIP # Parse the given arguments parser = argparse.ArgumentParser(description='Broker for communication between modules.') parser.add_argument('--robotIP',nargs=1,dest="robotIP",help="Pass the robot ip. The ip will be autimically stored for further use.") parser.add_argument('--ethernet',dest='networkType',default='wifi',action='store_const',const='ethernet',help='Use to rely on ethernet connection on the tablet. By default the wifi connection is used') parser.add_argument('--computerIP',nargs=1,dest="computerIP",default=None,help="Pass the ip of the computer. If not set, the program will try to deduce the ip automatically") args = parser.parse_args() logsdir="logs" #creates the logs folder if it doesnt exist if not os.path.exists(logsdir): os.makedirs(logsdir) logFile = 'logs/' + time.strftime('%Y%m%d-%H%M%S') + '-server.log' logging.basicConfig(filename=logFile, level=logging.DEBUG, format='%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(filename=logFile,level=lf.WARNING) logFormatter.start() logging.info("start the ConnectionManager") #Find out the outside ip ip = "" if args.computerIP != None: ip = args.computerIP[0] else: logging.info("No ip set. Will try to deduce one") try: if os.name == "nt": guid = get_network_guid(ni.interfaces(),args.networkType) ip = ni.ifaddresses(guid)[2][0]['addr'] else: if args.networkType == "ethernet": ip = ni.ifaddresses('eth0')[2][0]['addr'] else: ip = ni.ifaddresses('wlan0')[2][0]['addr'] except Exception, e: time.sleep(1) logging.error(str(e)) print("Could not deduce automatically ip. Enter remote ip:") ip = raw_input()
def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-nao.log" logging.basicConfig( filename=strLogFile, level=logging.DEBUG, format= '%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(strLogFile, level=lf.INFO) logFormatter.start() logging.info("start the test for comm to nao") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip + ":" + str(port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) sock.connect(server_address) sock.settimeout(1) logging.info("register clientNao") sendMessage(sock, "register:clientNao") try: while True: logging.info("send call") sendMessage( sock, "call:nao.ALTextToSpeech.say|'The connection manager can communicate with NAO'" ) time.sleep(5) except KeyboardInterrupt: global runningReading logging.info("Stop the client") sendMessage(sock, "exit") sock.close() logFormatter.stopReadingLogs()
def main(): #example of client in python #connectin to the server and receving the messages strLogFile = "logs/client-stuff.log" logging.basicConfig( filename=strLogFile, level=logging.DEBUG, format= '%(levelname)s %(relativeCreated)6d %(threadName)s %(message)s (%(module)s.%(lineno)d)', filemode='w') logFormatter = lf.LogFormatter(strLogFile) logFormatter.start() logging.info("start the test script of the client") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip + ":" + str(port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) sock.connect(server_address) sock.settimeout(1) aMessages = [] t = threading.Thread(target=readMessages, args=(sock, aMessages)) t.start() logging.info("register stuff") sendMessage(sock, "register:stuff") myStuff = Stuff() nCpt = 0 try: while True: logging.debug("send message") sendMessage(sock, "call:tablet.test.test 1.0") sendMessage(sock, "call:tablet.test.test 2.0") sendMessage(sock, "call:tablet.test.test 3.0") for strMessage in aMessages: if strMessage != "": fields = strMessage.split("|") logging.info("received message from " + str(fields[0])) method = fields[1] params = fields[2] getattr(myStuff, method)(params) del aMessages[:] #if nCpt % 7 == 0: # sendMessage(sock,"call:nao.ALTextToSpeech.say 'it's time to do a test. So we are going to put a pretty long text, and see what's happening'") # nCpt = 0 #nCpt += 1 time.sleep(1) except KeyboardInterrupt: global runningReading logging.info("Stop the client") sendMessage(sock, "exit") runningReading = False t.join() sock.close() logFormatter.stopReadingLogs()
def main(): #connection to the server and receving the messages strLogFile = "logs/client-content.log" logging.basicConfig( filename=strLogFile, level=logging.DEBUG, format= '%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(strLogFile, level=lf.INFO) logFormatter.start() logging.info("start the client content") ip = "127.0.0.1" if len(sys.argv) == 2: ip = sys.argv[1] port = 1111 logging.info("Attempt to connect to: " + ip + ":" + str(port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) sock.connect(server_address) sock.settimeout(1) aMessages = [] t = threading.Thread(target=readMessages, args=(sock, aMessages)) t.start() logging.info("register content") sendMessage(sock, "register:content") myContent = Content() shortMessage = True try: while True: #send alternatively short and long messages if shortMessage == True: logging.info("send short message") sendMessage(sock, "call:tablet.test.test|short") shortMessage = False else: logging.info("send large message") sendMessage(sock, largeMessage) shortMessage = True for strMessage in aMessages: if strMessage != "": fields = strMessage.split("|") logging.info("received message from " + str(fields[0])) method = fields[1] params = fields[2] getattr(myStuff, method)(params) del aMessages[:] time.sleep(1) except KeyboardInterrupt: global runningReading logging.info("Stop the client") sendMessage(sock, "exit") runningReading = False t.join() sock.close() logFormatter.stopReadingLogs()
except socket.error, e: pass if __name__ == "__main__": # Start the logging # if log dir doesn't exist ... if not os.path.exists('c:/l2tor/logs'): # ... create it os.makedirs('c:/l2tor/logs') logFile = 'c:/l2tor/logs/om_' + time.strftime('%Y%m%d%H%M%S') + '.log' logging.basicConfig(filename=logFile, level=logging.DEBUG, format='%(levelname)s %(asctime)-15s %(threadName)s %(message)s (%(module)s.%(lineno)d)', datefmt='%H.%M.%S', filemode='w') logFormatter = lf.LogFormatter(filename=logFile,level=lf.INFO) logFormatter.start() # Parse command-line options parser = OptionParser() parser.add_option("-s", "--server-ip", dest="server_ip", help="IP address of the ConnectionManager server") parser.add_option("-r", "--robot-ip", dest="robot_ip", help="IP address of the robot", default=None) (options, args) = parser.parse_args() if options.robot_ip == None: # Try to discover the robot's IP based on its name on the network try: logging.info("No robot IP provided -- trying to find it automatically..") ip = socket.gethostbyname('Zora.local') OutputManager(None, ip, options.server_ip) except Exception as e: logging.info("Could not automatically detect the robot's IP -- exiting!") else: