Пример #1
0
    def __init__(self):
        self.output_pipe = PIPE()
        self.signal_pipe = PIPE()

        self.mtcp = MTCP(self.output_pipe, self.signal_pipe)

        self.socks = {}  #key=addr, value=socket
        self.input_pipes = {}  #key=addr, value=input_pipes

        self.sending_lock = threading.Lock()
Пример #2
0
    def __init__(self): 
        self.output_pipe = PIPE()
        self.signal_pipe = PIPE()

        self.mtcp = MTCP(self.output_pipe, self.signal_pipe)

        self.socks = {} #key=addr, value=socket
        self.input_pipes = {} #key=addr, value=input_pipes

        self.sending_lock = threading.Lock()
Пример #3
0
class BroadCast():
    def __init__(self): 
        self.output_pipe = PIPE()
        self.signal_pipe = PIPE()

        self.mtcp = MTCP(self.output_pipe, self.signal_pipe)

        self.socks = {} #key=addr, value=socket
        self.input_pipes = {} #key=addr, value=input_pipes

        self.sending_lock = threading.Lock()
    def add_addr(self, addr, sock):
        if addr in self.socks:
            #print addr, "already in the socket list"
            return None# error

        self.socks[addr] = sock

        input_pipe = PIPE()
        self.input_pipes[addr] = input_pipe

        if (sock == None):
            return None
        self.mtcp.new_connect(sock, addr, input_pipe)

    def remove_addr(self, addr):
        #This will send the socket.close()

        if addr not in self.socks:
            #print addr, "not in the socket list"
            return False

        sock = self.socks[addr]
        input_pipe = self.input_pipes[addr]

        sock.close()
        input_pipe.close()

        self.mtcp.close(addr)

        self.socks[addr] = None
        self.input_pipes[addr] = None
        del self.socks[addr]
        del self.input_pipes[addr]

        return True


    def get_addrs(self):
        return self.socks.keys()

    def get_sock(self, addr):
        if addr not in self.socks:
            return None
        return self.socks[addr]

    def get_pipe(self, addr):
        if addr not in self.input_pipes:
            return None
        return self.input_pipes[addr]

    def read(self):
        return self.output_pipe.read()

    def send(self, addr, message): #return the input_pipe
        self.sending_lock.acquire()
        try:
            if self.socks[addr] == None:
                self.output_pipe.write(message)
                self.sending_lock.release()
                return self.output_pipe
            if addr not in self.input_pipes:
                #print addr, "not in input_pipe"
                self.sending_lock.release()
                return None

            input_pipe = self.input_pipes[addr]
            input_pipe.write(message)
        except:
            print 'sending error'
        self.sending_lock.release()
        return None

    def sendall(self,message):
        return self.broadcast(message)
    def broadcast(self, message): #return how many message has been sent
        self.sending_lock.acquire()
        #print 'start to broadcast'
        for addr in self.input_pipes.keys():
            #print 'pipe selection'
            pipe = self.input_pipes[addr]
            #print 'end of pipe selection'
            if (self.socks[addr] == None):
                #print 'write to locol'
                self.output_pipe.write(message)
                #print 'write locol succ'
            else:
                try:
                    #print 'pipe write'
                    pipe.write(message)
                    #print 'pipe write succ'
                except:
                    self.sending_lock.release()
                    self.remove_addr(addr)

        self.sending_lock.release()
        #print 'end of broadcast'
        return len(self.input_pipes)

    def get_signal_message(self):
        return self.signal_pipe.read()
Пример #4
0
class BroadCast():
    def __init__(self):
        self.output_pipe = PIPE()
        self.signal_pipe = PIPE()

        self.mtcp = MTCP(self.output_pipe, self.signal_pipe)

        self.socks = {}  #key=addr, value=socket
        self.input_pipes = {}  #key=addr, value=input_pipes

        self.sending_lock = threading.Lock()

    def add_addr(self, addr, sock):
        if addr in self.socks:
            #print addr, "already in the socket list"
            return None  # error

        self.socks[addr] = sock

        input_pipe = PIPE()
        self.input_pipes[addr] = input_pipe

        if (sock == None):
            return None
        self.mtcp.new_connect(sock, addr, input_pipe)

    def remove_addr(self, addr):
        #This will send the socket.close()

        if addr not in self.socks:
            #print addr, "not in the socket list"
            return False

        sock = self.socks[addr]
        input_pipe = self.input_pipes[addr]

        sock.close()
        input_pipe.close()

        self.mtcp.close(addr)

        self.socks[addr] = None
        self.input_pipes[addr] = None
        del self.socks[addr]
        del self.input_pipes[addr]

        return True

    def get_addrs(self):
        return self.socks.keys()

    def get_sock(self, addr):
        if addr not in self.socks:
            return None
        return self.socks[addr]

    def get_pipe(self, addr):
        if addr not in self.input_pipes:
            return None
        return self.input_pipes[addr]

    def read(self):
        return self.output_pipe.read()

    def send(self, addr, message):  #return the input_pipe
        self.sending_lock.acquire()
        if self.socks[addr] == None:
            self.output_pipe.write(message)
            self.sending_lock.release()
            return self.output_pipe
        if addr not in self.input_pipes:
            #print addr, "not in input_pipe"
            self.sending_lock.release()
            return None

        input_pipe = self.input_pipes[addr]
        input_pipe.write(message)
        self.sending_lock.release()
        return input_pipe

    def sendall(self, message):
        return self.broadcast(message)

    def broadcast(self, message):  #return how many message has been sent
        self.sending_lock.acquire()
        #print 'start to broadcast'
        for addr in self.input_pipes.keys():
            #print 'pipe selection'
            pipe = self.input_pipes[addr]
            #print 'end of pipe selection'
            if (self.socks[addr] == None):
                #print 'write to locol'
                self.output_pipe.write(message)
                #print 'write locol succ'
            else:
                try:
                    #print 'pipe write'
                    pipe.write(message)
                    #print 'pipe write succ'
                except:
                    self.sending_lock.release()
                    self.remove_addr(addr)

        self.sending_lock.release()
        #print 'end of broadcast'
        return len(self.input_pipes)

    def get_signal_message(self):
        return self.signal_pipe.read()