Esempio n. 1
0
    def initUI(self):
        self.mazeView=MazeView()
        name="robotMaze"
        regtype='_maze._tcp'
        
        self.address=None
        self.browser=Bonjour(name,regtype)
        self.browser.runBrowser()
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.mitSignal.connect(self.updateIp)

        closeAction = QtGui.QAction('Close', self)
        closeAction.setShortcut('Ctrl+Q')
        closeAction.setStatusTip('Close Notepad')
        closeAction.triggered.connect(self.close)
    
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(closeAction)
    
        self.setGeometry(300,300,300,50) 
        self.setWindowTitle('LUL') 
        self.browser.addClientEventHandler(self.mitSignal.emit)  
        
        self.rpiLabel = QtGui.QLabel("Not connected to r-pi",self)
        self.rpiLabel.move(5,5)
        self.rpiLabel.resize(self.rpiLabel.sizeHint())
        self.rpiLabel.show()
        
        self.getMaze = QtGui.QPushButton('getMaze', self)
        self.getMaze.clicked.connect(self.clientSendMaze)
        self.getMaze.resize(self.getMaze.sizeHint())
        self.getMaze.move(0, 20)
        self.getMaze.setEnabled(False)    
        self.show()
Esempio n. 2
0
    def initUI(self):
        self.mazeView = MazeView()
        name = "robotMaze"
        regtype = "_maze._tcp"

        self.address = None
        self.browser = Bonjour(name, regtype)
        self.browser.runBrowser()
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.mitSignal.connect(self.updateIp)

        closeAction = QtGui.QAction("Close", self)
        closeAction.setShortcut("Ctrl+Q")
        closeAction.setStatusTip("Close Notepad")
        closeAction.triggered.connect(self.close)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu("&File")
        fileMenu.addAction(closeAction)

        self.setGeometry(300, 300, 300, 50)
        self.setWindowTitle("LUL")
        self.browser.addClientEventHandler(self.mitSignal.emit)

        self.rpiLabel = QtGui.QLabel("Not connected to r-pi", self)
        self.rpiLabel.move(5, 5)
        self.rpiLabel.resize(self.rpiLabel.sizeHint())
        self.rpiLabel.show()

        self.getMaze = QtGui.QPushButton("getMaze", self)
        self.getMaze.clicked.connect(self.clientSendMaze)
        self.getMaze.resize(self.getMaze.sizeHint())
        self.getMaze.move(0, 20)
        self.getMaze.setEnabled(False)
        self.show()
Esempio n. 3
0
class MainGui(QtGui.QMainWindow):
    mitSignal = pyqtSignal(str, int, name="mitSignal")

    def __init__(self):

        super(MainGui, self).__init__()
        self.initUI()

    def initUI(self):
        self.mazeView = MazeView()
        name = "robotMaze"
        regtype = "_maze._tcp"

        self.address = None
        self.browser = Bonjour(name, regtype)
        self.browser.runBrowser()
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.mitSignal.connect(self.updateIp)

        closeAction = QtGui.QAction("Close", self)
        closeAction.setShortcut("Ctrl+Q")
        closeAction.setStatusTip("Close Notepad")
        closeAction.triggered.connect(self.close)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu("&File")
        fileMenu.addAction(closeAction)

        self.setGeometry(300, 300, 300, 50)
        self.setWindowTitle("LUL")
        self.browser.addClientEventHandler(self.mitSignal.emit)

        self.rpiLabel = QtGui.QLabel("Not connected to r-pi", self)
        self.rpiLabel.move(5, 5)
        self.rpiLabel.resize(self.rpiLabel.sizeHint())
        self.rpiLabel.show()

        self.getMaze = QtGui.QPushButton("getMaze", self)
        self.getMaze.clicked.connect(self.clientSendMaze)
        self.getMaze.resize(self.getMaze.sizeHint())
        self.getMaze.move(0, 20)
        self.getMaze.setEnabled(False)
        self.show()

    def closeEvent(self, event):
        self.browser.stopBrowser()
        self.mazeView.close()
        event.accept()

    def updateIp(self, ip, port):
        if self.address == (ip, port):
            self.closeTcpClient()
            self.address = None
            self.getMaze.setEnabled(False)
            self.rpiLabel.setText("Not connected to r-pi")
            self.rpiLabel.resize(self.rpiLabel.sizeHint())
            print ("r-pi removed and clientSocket closed with ip=" + str(ip) + " port=" + str(port))
        else:
            print ("r-pi catched with address" + str((ip, port)))

            reply = QtGui.QMessageBox.question(
                self,
                "question",
                "rpi detected\nwanna update ip/port?",
                QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
                QtGui.QMessageBox.No,
            )

            if reply == QtGui.QMessageBox.Yes:
                print ("old ip and port=" + str(self.address))
                self.address = (str(ip), port)
                print ("new ip and port=" + str(self.address) + "\n")
                self.getMaze.setEnabled(True)
                self.rpiLabel.setText("Connected to " + str(self.address))
                self.rpiLabel.resize(self.rpiLabel.sizeHint())
            else:
                pass

    def clientSendNumber(self):
        self.clientSend("number")

    def clientSendMaze(self):
        data = {"message": "maze"}
        print "maze called"
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.clientSocket.connect(self.address)
        self.clientSocket.send(json.dumps(data))
        data = self.clientSocket.recv(16384)  # limit reply to 16K
        print data
        received = json.loads(data)
        status = received.get("status")
        if status == "error":
            print "error: " + received.get("cause")
        else:
            print status
            currentPos = received.get("currentpos")
            maze = Maze(received.get("maze"))

            self.mazeView = MazeView(maze, currentPos, self.address)
            self.mazeView.repaint()
            self.mazeView.show()

        self.clientSocket.close()

        print ("closed socket")
        # print maze

    def clientSend(self, string):
        received = "nothing received"
        data = {"message": string, "test": 123.4}
        try:
            self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.clientSocket.connect(self.address)
            self.clientSocket.send(json.dumps(data))
            received = json.loads(self.clientSocket.recv(1024))
        finally:
            tmp = received.get(string)
            if tmp != None:
                print tmp
            else:
                print received

    def closeTcpClient(self):
        try:
            self.clientSocket.close()
            print ("closed client")
        finally:
            pass
Esempio n. 4
0
class MainGui(QtGui.QMainWindow):
    mitSignal = pyqtSignal(str, int, name='mitSignal')

    def __init__(self):
        
        super(MainGui, self).__init__()
        self.initUI()

    def initUI(self):
        self.mazeView=MazeView()
        name="robotMaze"
        regtype='_maze._tcp'
        
        self.address=None
        self.browser=Bonjour(name,regtype)
        self.browser.runBrowser()
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        self.mitSignal.connect(self.updateIp)

        closeAction = QtGui.QAction('Close', self)
        closeAction.setShortcut('Ctrl+Q')
        closeAction.setStatusTip('Close Notepad')
        closeAction.triggered.connect(self.close)
    
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(closeAction)
    
        self.setGeometry(300,300,300,50) 
        self.setWindowTitle('LUL') 
        self.browser.addClientEventHandler(self.mitSignal.emit)  
        
        self.rpiLabel = QtGui.QLabel("Not connected to r-pi",self)
        self.rpiLabel.move(5,5)
        self.rpiLabel.resize(self.rpiLabel.sizeHint())
        self.rpiLabel.show()
        
        self.getMaze = QtGui.QPushButton('getMaze', self)
        self.getMaze.clicked.connect(self.clientSendMaze)
        self.getMaze.resize(self.getMaze.sizeHint())
        self.getMaze.move(0, 20)
        self.getMaze.setEnabled(False)    
        self.show()

    def closeEvent(self,event):
        self.browser.stopBrowser()
        self.mazeView.close()
        event.accept() 
    
    def updateIp(self,ip,port):
        if self.address==(ip,port):
            self.closeTcpClient()
            self.address=None
            self.getMaze.setEnabled(False)    
            self.rpiLabel.setText("Not connected to r-pi")
            self.rpiLabel.resize(self.rpiLabel.sizeHint())
            print("r-pi removed and clientSocket closed with ip="+str(ip)+" port="+str(port))
        else:
            print("r-pi catched with address"+str((ip,port)))
            
            reply = QtGui.QMessageBox.question(self, 'question',"rpi detected\nwanna update ip/port?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)

            if reply == QtGui.QMessageBox.Yes:
                print("old ip and port="+str(self.address))
                self.address=(str(ip),port)
                print("new ip and port="+str(self.address)+"\n")
                self.getMaze.setEnabled(True)    
                self.rpiLabel.setText("Connected to "+str(self.address))
                self.rpiLabel.resize(self.rpiLabel.sizeHint())
            else:
                pass
        
    def clientSendNumber(self):
        self.clientSend("number")
    
    def clientSendMaze(self):
        data = {'message':"maze"}
        print"maze called"
        self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.clientSocket.connect(self.address)
        self.clientSocket.send(json.dumps(data))
        data = self.clientSocket.recv(16384)  # limit reply to 16K
        print data
        received = json.loads(data)
        status=received.get("status")
        if status=="error":
            print "error: "+received.get("cause")
        else:
            print status        
            currentPos=received.get("currentpos")
            maze=Maze(received.get("maze"))

            self.mazeView=MazeView(maze,currentPos,self.address)
            self.mazeView.repaint()
            self.mazeView.show()
            
        self.clientSocket.close()

        print("closed socket")
        #print maze
        
    def clientSend(self,string):
        received="nothing received"
        data = {'message':string, 'test':123.4}
        try:
            self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.clientSocket.connect(self.address)
            self.clientSocket.send(json.dumps(data))
            received = json.loads(self.clientSocket.recv(1024))
        finally:
            tmp=received.get(string)
            if tmp!=None:
                print tmp
            else:
                print received
    
    def closeTcpClient(self):
        try:
            self.clientSocket.close()
            print("closed client")
        finally:
            pass   
Esempio n. 5
0
 def initThreads(self):
     self.initTcp()    
     self.bonjour=Bonjour(self.name,self.regType,self.port)
Esempio n. 6
0
class ZeroconfTcpServer():
    def __init__(self):
        self.host="0.0.0.0"
        self.name="robotMaze"
        self.regType='_maze._tcp'
        self.eventHandlers={}

    def initThreads(self):
        self.initTcp()    
        self.bonjour=Bonjour(self.name,self.regType,self.port)
        
    def start(self):
        self.tcpThread=threading.Thread(target=self.tcpServer.serve_forever)
        self.tcpThread.start()
        self.bonjour.runRegister()
        print("lol started everything")
        
        
    def stop(self):
        self.tcpServer.shutdown()
        self.bonjour.stopRegister()
    
    def addHandler(self,string,handler):
        self.eventHandlers[string]=handler
        
    def initTcp(self):
        class DebugTCPServer(SocketServer.TCPServer):
            def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True, eventHandlers=None):
                #self.debug = debug
                self.eventHandlers=eventHandlers
                SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate=True)
        
        class DebugMETCPHandler(SocketServer.BaseRequestHandler):
            def handle(self):
                # self.server is an instance of the DebugTCPServer
                try:
                    while True:
                        #data=self.request.recv(1024)      try:
                        data = json.loads(self.request.recv(1024).strip())
                        if data!=0:
                            funcName=data.get("message")
                            print funcName
                            func=self.server.eventHandlers.get(funcName)
                            if func!=None:
                                params=data.get("params")
                                #print str(params)
                                response=func(params)
                                print("tcp sending sending"+funcName)
                                self.request.sendall(response)
                except Exception:
                    pass
                print "finnished handling tcp request"

        while True:
            try:
                self.port=9000+random.randint(0,900)
                self.tcpServer = DebugTCPServer((self.host, self.port), DebugMETCPHandler, eventHandlers=self.eventHandlers)
                break
            finally:
                time.sleep(0.1)
        print ("got port "+str(self.port))