def run(self): tries = config.config()['log_synchronization']['tries'] period = 60*config.config()['log_synchronization']['period'] server_ip = config.config()['network']['server_ip'] server_port = config.config()['network']['server_port'] while True: try: with db.manager().transaction(): log.logger().info('Sending log to server...') _log = proto.man_message() _log.message_type = 'log' db.manager().get_log(_log.db) while tries > 0: try: self.__conn.send_message((server_ip, server_port), _log.SerializeToString()) break except Exception as exc: log.logger().error('Log synchronizer: ' + repr(exc)) tries -= 1 if tries > 0: db.manager().drop_log() log.logger().info('Log successfully sent.') else: raise LogSyncError('Log sending failed.') except LogSyncError as err: log.logger().info(err.value) except Exception as exc: log.logger().error(repr(exc)) tries = config.config()['log_synchronization']['tries'] time.sleep(period)
def start(self): try: log.logger().info('Server started.') self.__servers.start_servers() while True: wait = 60 * float(config.config()['server']['sync_period']) new_db = proto.man_message() new_db.message_type = 'new_db' database.manager().get_sync_data(new_db.db) sync = Synchronizer(new_db, self.__servers.server_out).start() time.sleep(wait) except KeyboardInterrupt: log.logger().warning('Stopping server on user command.') except Exception as exc: log.logger().error(repr(exc)) finally: self.stop()
#31.10.2010 #13.11.2010 from shared.network import ConnServer import shared.man_db_msg_pb2 as proto import time def test_handler(msg): print('MESSAGE:<\n', msg, '\n>') if __name__ == '__main__': try: net = ConnServer(("localhost", 10001), {'default': test_handler}) net.start_servers() msg = proto.man_message() msg.message_type = 'db_to_print' db_node1 = msg.db.node.add() db_node1.id = 1 db_node1.ip = '120.45.210.10' db_node2 = msg.db.node.add() db_node2.id = 2 db_node2.ip = '120.45.210.11' db_node3 = msg.db.node.add() db_node3.id = 3 db_node3.ip = '120.45.210.12' db_point1 = msg.db.point.add() db_point1.id = 1 db_point1.type = 'type_1' db_point1.controller_id = 1 db_point1.reader = 'Reader_1'