当前位置: 首页>>代码示例>>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;未经允许,请勿转载。