当前位置: 首页>>代码示例>>Python>>正文


Python Channel.unicast方法代码示例

本文整理汇总了Python中channel.Channel.unicast方法的典型用法代码示例。如果您正苦于以下问题:Python Channel.unicast方法的具体用法?Python Channel.unicast怎么用?Python Channel.unicast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在channel.Channel的用法示例。


在下文中一共展示了Channel.unicast方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Process

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import unicast [as 别名]
class Process(multiprocessing.Process):
    """
        This process could take arguments from command line and execute unicast/multicast actions.
        It can also receive messages from its channel.
    """

    def __init__(self, id, ordering):
        super(Process, self).__init__() # call __init__ from multiprocessing.Process
        self.id = id

        # Read config from file
        self.process_info, self.addr_dict = configreader.get_processes_info()
        address = self.process_info[id]
        ip, port = address[0], address[1]
        print(ip, port)

        # Init a socket
        self.socket = socket.socket(
            socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.bind(address)

        # Init channel

        # Causal Order Channel
        if ordering == 'causal':
            print 'causal ordering'
            self.channel = CausalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict)
        elif ordering == 'total':
            print 'total ordering'
            # Total Order Channel
            if id == 1:
                # Select process 1 to be the sequencer
                self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict, True)
            else:
                self.channel = TotalOrderChannel(self, self.id, self.socket, self.process_info, self.addr_dict)
        else:
            # Regular channel
            print 'no ordering'
            self.channel = Channel(self, self.id, self.socket, self.process_info, self.addr_dict)

    def run(self):
        while True:
            data, address = self.socket.recvfrom(4096)
            self.channel.recv(data, address)

    def unicast_receive(self, source, message):
        print (message.receive_str())

    def unicast(self, destination, message):
        self.channel.unicast(message, destination)

    def multicast(self, message):
        self.channel.multicast(message)
开发者ID:weiyangedward,项目名称:cs425_MP,代码行数:55,代码来源:process.py

示例2: Client

# 需要导入模块: from channel import Channel [as 别名]
# 或者: from channel.Channel import unicast [as 别名]
class Client(multiprocessing.Process):
    """
        a client sends requests to a server
    """

    def __init__(self, client_id, serverID):
        super(Client, self).__init__() # call __init__ from multiprocessing.Process
        self.serverID = serverID - 1 # (0,9)
        self.client_id = client_id

        """
            Read config from file
            process_info[id] = (ip, port)
            addr_dict[(ip, port)] = id
        """
        self.process_info, self.addr_dict = configreader.get_processes_info()
        self.total_server = configreader.get_total_servers()
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        self.batch_cmd = ''
        self.client_id = client_id
        self.reconnect_try = 0

        """
            init TCP connect to server
        """
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # build a socket
            except socket.error as msg:
                self.socket = None
                continue
            try:
                self.socket.connect(sa) # connect to socket 
            except socket.error as msg:
                self.socket.close()
                self.socket = None
                continue
            break
        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)

    def socketTCP(self):
        self.address = self.process_info[self.serverID+1]
        self.ip, self.port = self.address[0], self.address[1]
        for res in socket.getaddrinfo(self.ip, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
            af, socktype, proto, canonname, sa = res
            try:
                self.socket = socket.socket(af, socktype, proto) # build a socket
            except socket.error as msg:
                self.socket = None
                continue
            try:
                self.socket.connect(sa) # connect to socket 
            except socket.error as msg:
                self.socket.close()
                self.socket = None
                continue
            break
        if self.socket is None:
            print('could not open socket')
        else:
            print("successfully connected to server %d" % (self.serverID+1))

    def channelSet(self):
        self.channel = Channel(self, self.client_id, self.socket, self.process_info, self.addr_dict)
            # sys.exit(1)

    """
        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 KeyboardInterrupt:
            print("CTRL C occured")
        except:
            print("Disconnected from server")
            # ====
            # choose another server if the connected server is crashed
            # handle server crash: pick a server with higher id
            # relaunch run()
            # ====
            
#.........这里部分代码省略.........
开发者ID:weiyangedward,项目名称:cs425_MP,代码行数:103,代码来源:client.py


注:本文中的channel.Channel.unicast方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。