Example #1
0
int main(int argc, char *argv[])
{
	if(!(home_dir = getenv("HOME"))){
		LOG_ERR();
		REPORT_ERREXIT();
	}
	if(!(cwd = getcwd(cwd, PATH_MAX))){
		LOG_ERR();
		REPORT_ERREXIT();
	}
	int flag = 0;
	char *corelog;
	while((flag = getopt(argc, argv, "c:l:")) != -1){
		switch(flag){
			case 'c':{
				config_file = optarg;
				break;
			}
			case 'l':{
				corelog = optarg;
				break;		
			}
			case '?':{
				fprintf(stderr, "Invalid argument!\n");
				return -1;
			}
		}
	}
	int clog = open((const char *)corelog, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
	if(clog == -1){
		LOG_ERR();
		return -1;
	}
	core_log = fdopen(clog, "a+");
	if(!core_log){
		LOG_ERR();
		return -1;
	}

	int ret;

	ret = parse_config_file((const char *)config_file);
	MAIN_CHECK_RETVAL(ret);

	ret = get_inotify_limits();
	MAIN_CHECK_RETVAL(ret);

	ret = init_inotify_actions();
	MAIN_CHECK_RETVAL(ret);

	init_pollfd_structures();

	ret = create_log_streams(0);
	MAIN_CHECK_RETVAL(ret);

	ret = init_event_struct();
	MAIN_CHECK_RETVAL(ret);

	
	ret = start_daemon();
	MAIN_CHECK_RETVAL(ret);

	print_createtime();

	ret = savepid();
	MAIN_CHECK_RETVAL(ret);

	set_sigusr1_handler();
	set_sigusr2_handler();

	for(;;){
		ret = wait_events();
		MAIN_CHECK_RETVAL(ret);
		ret = handle_events();
		MAIN_CHECK_RETVAL(ret);
	}
	return 0;
}
int main(int argc, char *argv[])
{
    int res = 0;

    /* Parse command line parameters */
    if (0 != (res = parse_peer_params(argc, argv, &proc_id, &fname))) {
        dbg_err("parse_args() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Parse the DME simulation config file.
     */
    if (0 != (res = parse_file(fname, proc_id, &nodes, &nodes_count))) {
        dbg_err("parse_file() returned nonzero status:%d", res);
        goto end;
    }

    /* Initialize and allocate any structures/vars used by the generic algorithm */

    /* struct genericstruct = calloc(...) */

    /*
     * Init connections (open listening socket)
     */
    if (0 != (res = open_listen_socket(proc_id, nodes, nodes_count))) {
        dbg_err("open_listen_socket() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Register signals (for I/O, alarms, etc.)
     */
    if (0 != (res = init_handlers(nodes[proc_id].sock_fd))) {
        dbg_err("init_handlers() returned nonzero status");
        goto end;
    }

    /* Register event handlers that will be called automatically on event occurrence */
    register_event_handler(DME_EV_SUP_MSG_IN, handle_supervisor_msg);
    register_event_handler(DME_EV_PEER_MSG_IN, handle_peer_msg);
    register_event_handler(DME_EV_WANT_CRITICAL_REG, process_ev_want_cr);
    register_event_handler(DME_EV_ENTERED_CRITICAL_REG, process_ev_entered_cr);
    register_event_handler(DME_EV_EXITED_CRITICAL_REG, process_ev_exited_cr);

    /*
     * Main loop: just sit here and wait for events (triggered by the supervisor).
     * All work is done in interrupt handlers mapped to registered functions.
     */
    wait_events();

end:
    /*
     * Do cleanup (deallocating dynamic structures)
     */
    deinit_handlers();

    /* Close our listening socket */
    if (nodes && nodes[proc_id].sock_fd > 0) {
        close(nodes[proc_id].sock_fd);
    }

    /* Free allocated structures */
    safe_free(nodes);

    return res;
}
int main(int argc, char *argv[])
{
    int res = 0;
    
    if (0 != (res = parse_sup_params(argc, argv, &fname, &logfname,
                                     &concurency_ratio, &max_concurrent_proc,
                                     &election_interval, &number_of_runs))) {
        dbg_err("parse_args() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Parse the file fname
     */
    if (0 != (res = parse_file(fname, proc_id, &nodes, &nodes_count))) {
        dbg_err("parse_file() returned nonzero status:%d", res);
        goto end;
    }
    dbg_msg("nodes has %d elements", nodes_count);

    /* compute the number of maximum concurrent processes (nearest integer) */
    if (max_concurrent_proc != 0) {
        /* The '-c' option was specified on the command line */
        fixed_concurent_num = TRUE;
    } else {
        /* Compute based on concurrency ratio */
        max_concurrent_proc = (nodes_count * concurency_ratio + 50) / 100;
        fixed_concurent_num = FALSE;
    }

    if (max_concurrent_proc < 2) {
        dbg_err("concurrency ratio or number set too low. At least 2 processes must be concurrent.");
        goto end;
    } else if (max_concurrent_proc > nodes_count) {
        dbg_err("concurrency number is greater than total number of processes. Setting it to maximum.");
        max_concurrent_proc = nodes_count;
    }

    dbg_msg("max_concurrent_proc=%d", max_concurrent_proc);

    randomizer_init();
    
    /* Allocate the statistics collection storage and open the log file */
    synchro_delays = calloc(nodes_count, sizeof(timespec_t));
    response_times = calloc(nodes_count, sizeof(timespec_t));

    if (NULL == (log_fh = fopen(logfname, "w"))) {
        dbg_err("Could not open log file %s for writing", logfname);
        res = ERR_BADFILE;
        goto end;
    }


    /*
     * Init connections (open listening socket)
     */
    if (0 != (res = open_listen_socket(proc_id, nodes, nodes_count))) {
        dbg_err("open_listen_socket() returned nonzero status:%d", res);
        goto end;
    }

    /*
     * Register signals (for I/O, alarms, etc.)
     */
    if (0 != (res = init_handlers(nodes[proc_id].sock_fd))) {
        dbg_err("init_handlers() returned nonzero status");
        goto end;
    }
    
    register_event_handler(DME_SEV_PERIODIC_WORK, do_work);
    register_event_handler(DME_SEV_SYNCRO, syncro);
    register_event_handler(DME_SEV_ENDSIMULATION, end_simulation);
    register_event_handler(DME_SEV_MSG_IN, process_messages);

    /* wait for peers processes to init, then kick start the supervisor */
    sleep(1);
    
    /* Start working; mark time */
    clock_gettime(CLOCK_REALTIME, &tstamp_supervisor_start);

    handle_event(DME_SEV_SYNCRO, &tstamp_supervisor_start);
    sleep(1);

    handle_event(DME_SEV_PERIODIC_WORK, NULL);

    /*
     * Main loop: just sit here and wait for interrupts.
     * All work is done in interrupt handlers mapped to registered functions.
     */
    wait_events();
    
end:
    /*
     * Do cleanup (deallocating dynamic structures)
     */
    deinit_handlers();

    /* Close our listening socket */
    if (nodes && nodes[proc_id].sock_fd > 0) {
        close(nodes[proc_id].sock_fd);
    }

    if (log_fh) {
        fclose(log_fh);
    }

    safe_free(nodes);
    safe_free(synchro_delays);
    safe_free(response_times);
    
    return res;
}