Exemplo n.º 1
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.netReader.read(None)
            self.messageQueue.put(msg)

    def send(self):
        while True:
            msg = self.messageQueue.get()
            if msg.type == 'subscription':
                print 'received subscription from: ' + msg.senderID
                self.subscribers.append(msg.body)
            elif msg.type == 'message':
                for subscriber in self.subscribers:
                    if subscriber.id == msg.receiverID:
                        self.netWriter.port = subscriber.port
                        self.netWriter.host = subscriber.ip
                        print 'message for ' + str(subscriber)
                        self.netWriter.write(msg.receiverID, msg)
                        break

    def main(self):
        print 'here'
        thread.start_new_thread(self.send, ())
        self.listen()
Exemplo n.º 2
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 = 'app1'

        message = Message()
        message.type = 'subscription'
        message.senderID = 'app1'
        message.receiverID = 'broker'
        message.body = myself

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

        message.type = 'message'
        tree = ET.parse('country_data.xml')
        root = tree.getroot()

        recipient = 'app2'
        message.body = open('country_data.xml').read()
        message.receiverID = recipient
        self.netWriter.write('app2', message)
Exemplo n.º 3
0
class Receiver(object):
    def __init__(self):
        self.netReader = IO_Network(DEFAULT_PORT, 'localhost')
        self.netReader.bind()
        self.netWriter = IO_Network(BROKER_PORT, 'localhost')
        self.fileWriter = IO_File()
        print 'Socket bind complete'

    def listen(self):
        while True:
            msg = self.netReader.read(None)
            print '\nReceived message:{0}'.format(msg.body)
            self.fileWriter.write('received.xml', msg.body)


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

        message = Message()
        message.type = 'subscription'
        message.senderID = 'app2'
        message.receiverID = 'broker'
        message.body = myself

        self.netWriter.write('broker', message)
Exemplo n.º 4
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.netReader.read(None)
            self.messageQueue.put(msg)

    def send(self):
        while True:
            msg = self.messageQueue.get()
            if msg.type == 'subscription':
                print 'received subscription from: ' + msg.senderID
                self.subscribers.append(msg.body)
            elif msg.type == 'message':
                for subscriber in self.subscribers:
                    if subscriber.id == msg.receiverID:
                        self.netWriter.port = subscriber.port
                        self.netWriter.host = subscriber.ip
                        print 'message for ' + str(subscriber)
                        self.netWriter.write(msg.receiverID, msg)
                        break

    def main(self):
        print 'here'
        thread.start_new_thread(self.send, ())
        self.listen()
Exemplo n.º 5
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.º 6
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
Exemplo n.º 7
0
    def __init__(self):

        self.netWriter = IO_Network(BROKER_PORT, "localhost")
Exemplo n.º 8
0
 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 = []
Exemplo n.º 9
0
 def __init__(self):
     self.netReader = IO_Network(DEFAULT_PORT, 'localhost')
     self.netReader.bind()
     self.netWriter = IO_Network(BROKER_PORT, 'localhost')
     print 'Socket bind complete'
Exemplo n.º 10
0
 def __init__(self):
     self.netReader = IO_Network(DEFAULT_PORT, 'localhost')
     self.netReader.bind()
     self.netWriter = IO_Network(BROKER_PORT, 'localhost')
     self.fileWriter = IO_File()
     print 'Socket bind complete'
Exemplo n.º 11
0
 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 = []