Exemplo n.º 1
0
class Receiver(object):
    def __init__(self):
        self.netReader = IO_Network(DEFAULT_PORT, 'localhost')
        self.netReader.bind()
        self.netWriter = IO_Network(BROKER_PORT, 'localhost')
        print 'Socket bind complete'

    def listen(self):
        while True:
            msg = self.netReader.read(None)
            if msg.body == 'close':
                self.netWriter.close_socket()
                self.netReader.close_socket()
            print '\nReceived message:{0}'.format(msg.body)

    def send(self):
        myself = Subscriber()
        myself.port = DEFAULT_PORT
        myself.ip = 'localhost'
        myself.id = 'sender'

        message = Message()
        message.type = 'subscription'
        message.senderID = 'sender'
        message.receiverID = 'receiver'
        message.body = myself

        self.netWriter.write('broker', message)
Exemplo n.º 2
0
class Broker(object):
    def __init__(self):
        self.messageQueue = Queue()
        self.netReader = IO_Network(BROKER_PORT, 'localhost')
        self.netReader.bind()
        self.netWriter = IO_Network(DEFAULT_PORT, 'localhost')
        self.subscribers = []

    def listen(self):
        while True:
            msg = self.netWriter.read(None)
            self.messageQueue.put(msg)

    def send(self):
        while True:
            msg = self.messageQueue.get()
            if msg.type == 'subscription':
                self.subscribers.append(msg.body)
            elif msg.type == 'message':
                for subscriber in self.subscribers:
                    if subscriber.id == msg.receiver:
                        self.netWriter.port = subscriber.port
                        self.netWriter.host = subscriber.ip
                        break
                if msg.body == 'close':
                    self.netWriter.write(msg.receiver, msg)
                    self.netReader.close_socket()
                    self.netWriter.close_socket()

    def main(self):
        print 'here'
        thread.start_new_thread(self.listen, ())
        # thread.start_new_thread(self.send, ())
        self.send()
Exemplo n.º 3
0
class Sender(object):
    def __init__(self):

        self.netWriter = IO_Network(BROKER_PORT, "localhost")

    def send(self):
        myself = Subscriber()
        myself.port = DEFAULT_PORT  # sender port
        myself.ip = 'localhost'
        myself.id = 'sender'

        message = Message()
        message.type = 'subscription'
        message.senderID = 'sender'
        message.receiverID = 'receiver'
        message.body = myself

        self.netWriter.write('broker', message)

        message.type = 'message'

        while True:
            text = raw_input("Enter message to send: ")
            if text == 'close':
                self.netWriter.close_socket()
                break
            recipient = raw_input('Enter receiver: ')
            message.body = text
            message.receiverID = recipient
            self.netWriter.write('receiver', message)
        return