Exemple #1
0
def register(address, port, init_topic):
	publisher = Publisher(address, port, init_topic)
	publisher.register_handler()
	logfile_name = './Output/' + publisher.myID + '-publisher.log'
	with open(logfile_name, 'w') as log:
		log.write('ID: %s \n' % publisher.myID)
		log.write('Init Topic: %s\n' % publisher.init_topic)
		log.write('Connection Info: tcp://' + address + ':' + port + '\n')
	return publisher
Exemple #2
0
def parse(argv):
    opt = argv[0]
    global pub
    global broker
    global sub
    if opt == 'pub':
        if argv[1] == '-r' and argv[3] == '-P' and argv[5] == '-t':
            if pub is None:
                address = argv[2]
                port = argv[4]
                topic = argv[6]
                pub = Publisher(address, port, topic)
                if pub.register_handler():
                    return True
                else:
                    return False
            else:
                print('You already registered a publisher.')
                return False
        elif argv[1] == 'send' and argv[2] == '-t' and argv[4] == '-p':
            if pub is None:
                print('Please register a publisher firstly.')
                return False
            else:
                topic = argv[3]
                publication = ' '.join(argv[5:])
                pub.send_pub(topic, publication)
        elif argv[1] == '-d':
            if pub is None:
                print('Please register a publisher firstly.')
                return False
            else:
                topic = argv[2]
                pub.drop_topic(topic)
        elif argv[1] == 'shutoff':
            if pub is None:
                print('Please register a publisher firstly.')
                return False
            else:
                pub.shutoff()
                return True
        else:
            print('Illegal command.')
            return False

    elif opt == 'broker':
        if argv[1] == '-l':
            xsubport = argv[2]
            xpubport = argv[3]
            broker = Broker(xsubport, xpubport)
            broker.handler()
        else:
            print('Illegal command.')
            return False

    elif opt == 'sub':
        if argv[1] == '-r' and argv[3] == '-P' and argv[5] == '-t' and argv[
                7] == '-h':
            address = argv[2]
            port = argv[4]
            topic = argv[6]
            count = argv[8]
            sub = Subscriber(address, port, topic, count)
            sub.prepare()
            sub.handler()
        else:
            print('Illegal command.')
            return False
    else:
        print('Illegal command.')
        return False