def reconnect():
    unixIPC.reconnect()
# the subprocesses
children = []

#timeouts
last_db_check = time.time()

while True:
    if ( not scheduledb.connected() ):
        scheduledb.connect()
    else:
        if ( time.time() - last_db_check > DB_CHECK_INTERVAL ):
            check_db()
            last_db_check = time.time()
        
    if ( not unixIPC.connected ):
        unixIPC.reconnect()
    else:
        msgs = unixIPC.tick()
        for type,msg,client in msgs:
            if type=='scheduler_cmd_pb2':
                scheduler_cmd_msg = scheduler_cmd_pb2.Scheduler_Cmd();
                scheduler_cmd_msg.ParseFromString(msg)
                if scheduler_cmd_msg.command == scheduler_cmd_pb2.Scheduler_Cmd.FORCE_START:
                    task = scheduledb.get_task_by_id(scheduler_cmd_msg.task_id)
                    if ( task != None ):
                        child_start_task(task)
                    else:
                        print "Error task ID %d not found"%scheduler_cmd_msg.task_id
                elif scheduler_cmd_msg.command == scheduler_cmd_pb2.Scheduler_Cmd.PAUSE:
                    child = find_child_by_id(scheduler_cmd_msg.task_id)
                    if ( child != None ):

    
config.force_load();

cmap = config.map;
   
    
############# START THE SERVER ############################

unixIPC = unixIPC.UnixIPC();
unixIPC.run_server("mod_config", mod_config_IF.CONFIG_PORT);

while(1):
    if not unixIPC.connected:
        unixIPC.reconnect();
    else:
        msgs = unixIPC.tick();
        for (mtype, msg, client) in msgs:
            #print "Got message of type ",type," : ",msg
            if mtype == 'config_cmd_pb2':
                config_cmd_msg = config_cmd_pb2.Config_Cmd()
                try:
                    config_cmd_msg.ParseFromString(msg)
                    if config_cmd_msg.cmd == config_cmd_pb2.Config_Cmd.SET:
                        mod_name = config_cmd_msg.mod_name;
                        key = config_cmd_msg.key;
                        val = config_cmd_msg.val;
                        
                        if ( not config.map.has_key(mod_name) ):
                            config.map[mod_name] = config.IPCMap();