當前位置: 首頁>>代碼示例>>Python>>正文


Python Channel.unicastTCP方法代碼示例

本文整理匯總了Python中channel.Channel.unicastTCP方法的典型用法代碼示例。如果您正苦於以下問題:Python Channel.unicastTCP方法的具體用法?Python Channel.unicastTCP怎麽用?Python Channel.unicastTCP使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在channel.Channel的用法示例。


在下文中一共展示了Channel.unicastTCP方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Client

# 需要導入模塊: from channel import Channel [as 別名]
# 或者: from channel.Channel import unicastTCP [as 別名]

#.........這裏部分代碼省略.........
        if self.socket is None:
            print('could not open socket')
            sys.exit(1)

        """
            Init unicast channel
        """
        print("client, no ordering")
        # pass Client obj as arg to Channel
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)

    """
        override run(), receiving message from server
    """
    def run(self):
        try:

            while True:
                data, address = self.socket.recvfrom(4096)
                print("get server message: ", data, address)
                # break if connected server crashed
                if not data: break
                self.channel.recv(data)
        except:
            print("run() Exceptions")
        finally:
            print("exit client thread")
            if self.socket != None: 
                self.socket.close()
                self.socket = None
                with self.socket_status.get_lock():
                    self.socket_status.value = 0 # set socket_status to be not available


    """
        handle 'delay'
        addBatchCmd keeps adding commands
    """
    def addBatchCmd(self):
        print("addBatchCmd...")
        try:
            while True:
                self.batch_cmd += input()
        except:
            pass

    """
        handle 'delay'
        executeBatchCmd execute each command line received during delay
    """
    def executeBatchCmd(self):
        print("executeBatchCmd...")
        if (self.batch_cmd):
            for cmd in self.batch_cmd.splitlines():
                self.parseCommand(cmd)
        self.batch_cmd = ''

    """
        execute user commands from std-input
    """
    def parseCommand(self, cmd):
        print("parseCommand...")
        socket_status = 0
        with self.socket_status.get_lock():
            socket_status = self.socket_status.value
        if socket_status == 1: # if socket is still connected
            serverID = 0
            with self.server_id.get_lock():
                serverID = self.server_id.value
            print("serverID ", str(serverID))
            cmd_args = cmd.split()
            # write value to variable
            if cmd_args[0] == "put" and len(cmd_args) == 3:
                var_name, value = cmd_args[1], int(cmd_args[2])
                message = str(self.client_id) + " " + cmd
                self.channel.unicastTCP(serverID, message)
            # get variable's value and print to stdout
            elif cmd_args[0] == "get" and len(cmd_args) == 2:
                var_name = cmd_args[1]
                message = str(self.client_id) + " " + cmd
                self.channel.unicastTCP(serverID, message)
            # delay input to allow a batch of commands
            elif cmd_args[0] == "delay" and len(cmd_args) == 2:
                sleep_time = float(cmd_args[1])
                t_batchCmd = multiprocessing.Process(target = self.addBatchCmd, args=())
                t_batchCmd.start()
                # wait until thread timeout
                time.sleep(sleep_time / 1000.0)
                t_batchCmd.terminate()
                print("timeout")
                self.executeBatchCmd()
            # requests server to print all variables to stdout
            elif cmd_args[0] == "dump" and len(cmd_args) == 1:
                message = str(self.client_id) + " " + cmd
                self.channel.unicastTCP(serverID, message)
            else:
                print("command not understood, enter new command:")
            return 1 # return server available signal to main
        else:
            return 0 # return server crash signal to main
開發者ID:weiyangedward,項目名稱:cs425_MP,代碼行數:104,代碼來源:client.py

示例2: Client

# 需要導入模塊: from channel import Channel [as 別名]
# 或者: from channel.Channel import unicastTCP [as 別名]

#.........這裏部分代碼省略.........
            # relaunch run()
            # ====
            
        finally:
            print("exit client thread")
            if self.socket != None: self.socket.close()
            if (self.reconnect_try < self.total_server * 10):
                print("Trying to connect to next server...")
                self.reconnect_try += 1
                self.serverID = (self.serverID + 1) % (self.total_server)
                print("Trying to connect to next server %d" % (self.serverID+1))
                self.socketTCP()
                self.channelSet()
                self.run()
            else:
                print("Hit maximum attempts, please enter 'exit'")
            
        # except:

               # ====
               # choose another server if the connected server is crashed
               # handle server crash: pick a server with higher id
               # relaunch run()
               # ====

            # print("Disconnected from server")
        # try:
        #     self.serverID = (self.serverID + 1) % self.total_server
        #     self.run()
        # except:
        #     print("cannot connect to server")
            
    """
        unicast_receive(int, Message)
    """
    # def unicast_receive(self, source, message):
    #     print (message.receive_str())

    """
        unicast(int, Message)
    """
    def unicast(self, destination, message):
        self.channel.unicast(message, destination)

    """
        handle 'delay'
        addBatchCmd keeps adding commands
    """
    def addBatchCmd(self):
        print("addBatchCmd...")
        try:
            while True:
                self.batch_cmd += raw_input()
        except:
            print("timeout")

    """
        handle 'delay'
        executeBatchCmd execute each command line received during delay
    """
    def executeBatchCmd(self):
        print("executeBatchCmd...")
        if (self.batch_cmd):
            for cmd in self.batch_cmd.splitlines():
                self.parseCommand(cmd)
        self.batch_cmd = ''

    """
        execute user commands from std-input
    """
    def parseCommand(self, cmd):
        cmd_args = cmd.split()
        # write value to variable
        if cmd_args[0] == "put" and len(cmd_args) == 3:
            var_name, value = cmd_args[1], int(cmd_args[2])
            message = str(self.client_id) + " " + cmd
            self.channel.unicastTCP(self.serverID, message)
        # get variable's value and print to stdout
        elif cmd_args[0] == "get" and len(cmd_args) == 2:
            var_name = cmd_args[1]
            message = str(self.client_id) + " " + cmd
            self.channel.unicastTCP(self.serverID, message)
        # delay input to allow a batch of commands
        elif cmd_args[0] == "delay" and len(cmd_args) == 2:
            sleep_time = float(cmd_args[1])
            # t_batchCmd = Thread(target = self.addBatchCmd, args=())
            # t_batchCmd.start()
            t_batchCmd = multiprocessing.Process(target=self.addBatchCmd, args=())
            t_batchCmd.start()
            # wait until thread timeout
            time.sleep(sleep_time / 1000.0)
            # t_batchCmd.join(sleep_time / 1000.0)
            t_batchCmd.terminate()
            self.executeBatchCmd()
        # requests server to print all variables to stdout
        elif cmd_args[0] == "dump" and len(cmd_args) == 1:
            message = str(self.client_id) + " " + cmd
            self.channel.unicastTCP(self.serverID, message)
        else:
            print("command not understood, enter new command:")
開發者ID:weiyangedward,項目名稱:cs425_MP,代碼行數:104,代碼來源:client.py


注:本文中的channel.Channel.unicastTCP方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。