Esempio n. 1
0
consensus_component* init_consensus_comp(struct node_t* node,uint32_t node_id, FILE* log,
        int sys_log,int stat_log,const char* db_name,
        int deliver_mode,void* db_ptr,int group_size,
        view* cur_view,view_stamp* to_commit,view_stamp* committed,view_stamp* highest,user_cb u_cb,up_call uc,void* arg){
    
    consensus_component* comp = (consensus_component*)
        malloc(sizeof(consensus_component));
    memset(comp,0,sizeof(consensus_component));

    if(NULL!=comp){
        if(deliver_mode==50){
            comp->db_ptr = db_ptr;
        }else{
            comp->db_ptr = initialize_db(db_name,0);
            if(NULL==comp->db_ptr){
                goto consensus_error_exit;
            }
        }    
        comp->sys_log = sys_log;
        comp->stat_log = stat_log;
        comp->sys_log_file = log;
        comp->my_node = node;
        comp->node_id = node_id;
        comp->deliver_mode = deliver_mode;
        comp->group_size = group_size;
        comp->cur_view = cur_view;
        if(comp->cur_view->leader_id == comp->node_id){
            comp->my_role = LEADER;
        }else{
            comp->my_role = SECONDARY;
        }
        comp->ucb = u_cb;
        comp->uc = uc;
        comp->up_para = arg;
        comp->highest_seen_vs = highest;
        comp->highest_seen_vs->view_id = 1;
        comp->highest_seen_vs->req_id = 0;
        comp->highest_committed_vs = committed; 
        comp->highest_committed_vs->view_id = 1; 
        comp->highest_committed_vs->req_id = 0; 
        comp->highest_to_commit_vs = to_commit;
        comp->highest_to_commit_vs->view_id = 1;
        comp->highest_to_commit_vs->req_id = 0;
        goto consensus_init_exit;

    }
consensus_error_exit:
    if(comp!=NULL){
        if(NULL!=comp->db_ptr && comp->deliver_mode == 1){
            close_db(comp->db_ptr,0);
        }
        free(comp);
    }

consensus_init_exit:
    return comp;
}
int main(int argc, char * argv[]) {

	/* part (a): execute the first three SQL queries */
	// const char sql[] = "SELECT * FROM movies WHERE ProductionYear < 1950";
	// const char sql[] = "SELECT * FROM movies WHERE Format == \"VHS\"";
	// const char sql[] = "SELECT * FROM movies WHERE Language == \"Spanish\"";

	if (argc < 2) {
		fprintf(stderr,"Error: database name not specified!\n");
		return 1;
	}

	char * database_name = argv[1];
  int error = initialize_db(database_name);

  if (error == 0) {
    puts("Database loaded successfully on b-tree.");
  } else {
    puts("Error: database was not loaded!");
    return error;
  }

  char input[INPUT_MAX_LENGTH];
  unsigned int len;

  dump_sorted_list("output.txt");

  do {
		printf("Enter a movie title: ");
		fflush(stdout);
		if (!fgets(input, INPUT_MAX_LENGTH, stdin))
			abort(); /* failed to read stdin */

		len = strlen(input);
		/* remove trailing newline character */
		if (len > 0 && input[len-1] == '\n') {
			input[len-1] = '\0';
			--len;
		}

		if (len == 0) /* empty expression signals exit */
    {
      break;
    }

    locate_movie(input);

	} while (1);

  cleanup();

	return 0;
}
Esempio n. 3
0
consensus_component* init_consensus_comp(const char* config_path, const char* log_path, node_id_t node_id, const char* start_mode){
    consensus_component* comp = (consensus_component*)malloc(sizeof(consensus_component));
    memset(comp, 0, sizeof(consensus_component));

    if(NULL != comp){
        comp->node_id = node_id;
        comp->cur_view.view_id = 1;
        comp->cur_view.req_id = 0;
        if(*start_mode == 's'){
            comp->cur_view.leader_id = comp->node_id;
        }else{
            comp->cur_view.leader_id = 0; //TODO
        }
        if(comp->cur_view.leader_id == comp->node_id){
            comp->my_role = LEADER;
        }else{
            comp->my_role = SECONDARY;
        }
        comp->highest_seen_vs.view_id = 1;
        comp->highest_seen_vs.req_id = 0;
        comp->committed.view_id = 1; 
        comp->committed.req_id = 0;
        consensus_read_config(comp, config_path);
        pthread_mutex_init(&comp->mutex, NULL);

        int build_log_ret = 0;
        if(log_path == NULL){
            log_path = ".";
        }else{
            if((build_log_ret = mkdir(log_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0){
                if(errno != EEXIST){
                    con_err_log("Log Directory Creation Failed, No Log Will Be Recorded.\n");
                }else{
                    build_log_ret = 0;
                }
            }
        }

        if(!build_log_ret){
            char* con_log_path = (char*)malloc(sizeof(char)*strlen(log_path) + 50);
            memset(con_log_path, 0, sizeof(char)*strlen(log_path) + 50);
            if(NULL != con_log_path){
                sprintf(con_log_path, "%s/node-%u-consensus.log", log_path, comp->node_id);
                comp->con_log_file = fopen(con_log_path, "w");
                free(con_log_path);
            }
        }
        comp->db_ptr = initialize_db(comp->db_name, 0);
    }
    return comp;
}
Esempio n. 4
0
static int open_and_initialize_db(conflate_handle_t *handle,
                                  const char *filename,
                                  sqlite3 **db)
{
    int err = 0;

    if ((err = sqlite3_open(filename, &*db)) != SQLITE_OK) {
        return err;
    }

    if (!initialize_db(handle, *db)) {
        CONFLATE_LOG(handle, LOG_LVL_ERROR, "Error initializing tables");
        return SQLITE_ERROR;
    }

    return SQLITE_OK;
}
Esempio n. 5
0
void start_client(void)
{
	extern char currmaildir[];

	initialize_mdb();
#ifndef ENABLE_SSH
	initialize_db();
#endif

	initialize_convert_env();
	system_init();

	if (setjmp(byebye)) {
		system_abort();
	}

	strlcpy(BoardName, BBSNAME, sizeof(BoardName));

	if (login_query() == -1) {
		terminal_flush();
		sleep(3);
		exit(1);
	}
#ifndef ENABLE_SSH
	screen_negotiate_size();
#endif // ENABLE_SSH
	screen_init(0);

	user_login();

	setmdir(currmaildir, currentuser.userid);
	RMSG = NA;
	screen_clear();
	editor_restore();
#ifdef TALK_LOG
	tlog_recover();
#endif

	if (strcmp(currentuser.userid, "guest")) {
		if (check_maxmail())
			pressanykey();
		screen_move(9, 0);
		screen_clrtobot();
		if (!DEFINE(DEF_NOLOGINSEND))
			if (session_visible())
				login_msg();
		screen_clear();
		set_numofsig();
	}

	active_board_init(false);
	fill_date();

	if (DEFINE(DEF_LOGFRIEND)
			&& session_count_online_followed(!HAS_PERM(PERM_SEECLOAK)) > 0)
		show_online_followings();

	menu_load("menu.img");
	while (1) {
		if (DEFINE(DEF_NORMALSCR))
			menu_loop("TOPMENU");
		else
			menu_loop("TOPMENU2");
		Goodbye();
	}
}
Esempio n. 6
0
/***********************************************************************************************
                        MAIN MENU FOR RETAIL

***********************************************************************************************/
int
main (void)
{
    struct tm intim;
    int Ret_val;
    double amount;
    unsigned char str[100] = "";
    pthread_t syncT;
    pthread_attr_t attr;
    int newprio;
    struct sched_param param;
    int ret;


//fp = fopen (logFile, "w+");
    fp = stdout;

    fprintf(fp,"Begun main()");
    fflush(fp);

    lk_open ();
    prn_open ();
    mscr_open ();
    lk_dispinit ();

    lk_dispclr();

    strcpy(DATABASE,"/mnt/jffs2/fps.sqlite");

    initialize_db ();

    open_sqlite (DATABASE);

    strcpy(CONFIG_FILE, get_config("CONFIG_FILE",tmpbuf,"0"));	//Need OFFICE ID From config file before init_globals
    read_config (CONFIG_FILE);
    strcpy(OFFICE_ID,getVal("office_id")); // Get from config file
    strcpy(SESSION_ID,"");

    fprintf(fp,"office_id:",OFFICE_ID);
    fflush(fp);

    init_globals();

    strcpy(IP,getVal("remote_url"));
    strcpy(shop_id,getVal("shop"));
    strcpy(terminal,getVal("terminal"));
    strcpy(tempFile,"/mnt/jffs2/dat");
    strcpy(svr_username,getVal("server_user"));
    strcpy(svr_password,getVal("server_pwd"));

    setAuthType();

    if(strcmp(toUpper(getVal("show_splash")),"TRUE")==0)
    {
        lk_disptext (1, 0, "        PDS ", 1);
        lk_disptext (3, 0, "     Govt of AP", 0);
        lk_disptext (4, 0, "    Version:0.9", 0);
        lk_disptext (5, 0, "  Enter to continue", 0);
        lk_getkey ();
        lk_dispclr ();
    }

    if(strcmp(toUpper(getVal("commMode")),"ETHERNET") == 0)
        ip_setup ();

    strcpy(background,getVal("background"));

    pthread_attr_init(&attr);
    pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
    newprio = 1;
    param.sched_priority = newprio;
    pthread_attr_setschedparam(&attr,&param);

    if(strcmp(background,"true")==0) {
        fprintf(fp,"Starting a background thread");
        fflush(fp);

        if (pthread_create (&syncT, &attr, syncThread, NULL) != 0)
            fprintf (stderr, "Error creating the thread");
    }

    while(1) {
        ret = login();

        printFlag = FALSE;

        if(ret==USER)
            main_menu();
        else if(ret == ADMIN)
            admin_menu();
        else {
            lk_dispclr();
            lk_disptext (2, 0, "Invalid credentials", 0);
            lk_getkey ();
            lk_dispclr ();
        }
    }

    prn_close ();			// closing printer
    lk_close ();			// closing pinpad
    mscr_close ();		// closing magnetic swipe
    close_sqlite ();

    fprintf(fp,"Ended main()");
    fflush(fp);

    return SUCCESS;
}
Esempio n. 7
0
event_manager* mgr_init(node_id_t node_id, const char* config_path, const char* log_path){
    
    event_manager* ev_mgr = (event_manager*)malloc(sizeof(event_manager));

    if(NULL==ev_mgr){
        err_log("EVENT MANAGER : Cannot Malloc Memory For The ev_mgr.\n");
        goto mgr_exit_error;
    }

    memset(ev_mgr, 0, sizeof(event_manager));

    ev_mgr->node_id = node_id;

    if(mgr_read_config(ev_mgr,config_path)){
        err_log("EVENT MANAGER : Configuration File Reading Error.\n");
        goto mgr_exit_error;
    }

    int build_log_ret = 0;
    if(log_path==NULL){
        log_path = ".";
    }else{
        if((build_log_ret=mkdir(log_path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))!=0){
            if(errno!=EEXIST){
                err_log("EVENT MANAGER : Log Directory Creation Failed,No Log Will Be Recorded.\n");
            }else{
                build_log_ret = 0;
            }
        }
    }

    if(!build_log_ret){
            char* sys_log_path = (char*)malloc(sizeof(char)*strlen(log_path)+50);
            memset(sys_log_path,0,sizeof(char)*strlen(log_path)+50);
            if(NULL!=sys_log_path){
                sprintf(sys_log_path,"%s/node-%u-mgr-sys.log",log_path,ev_mgr->node_id);
                ev_mgr->sys_log_file = fopen(sys_log_path,"w");
                free(sys_log_path);
            }
            if(NULL==ev_mgr->sys_log_file && (ev_mgr->sys_log || ev_mgr->stat_log)){
                err_log("EVENT MANAGER : System Log File Cannot Be Created.\n");
            }
            char* req_log_path = (char*)malloc(sizeof(char)*strlen(log_path)+50);
            memset(req_log_path,0,sizeof(char)*strlen(log_path)+50);
            if(NULL!=req_log_path){
                sprintf(req_log_path,"%s/node-%u-mgr-req.log",log_path,ev_mgr->node_id);
                ev_mgr->req_log_file = fopen(req_log_path,"w");
                free(req_log_path);
            }
            if(NULL==ev_mgr->req_log_file && ev_mgr->req_log){
                err_log("EVENT MANAGER : Request Log File Cannot Be Created.\n");
            }
    }

    ev_mgr->db_ptr = initialize_db(ev_mgr->db_name,0);

    if(ev_mgr->db_ptr==NULL){
        err_log("EVENT MANAGER : Cannot Set Up The Database.\n");
        goto mgr_exit_error;
    }

    ev_mgr->leader_tcp_map = NULL;
    ev_mgr->replica_tcp_map = NULL;
    ev_mgr->leader_udp_map = NULL;

    ev_mgr->con_node = system_initialize(&ev_mgr->node_id,config_path,log_path,update_state,check_point_condtion,get_mapping_fd,ev_mgr->db_ptr,ev_mgr);

    if(NULL==ev_mgr->con_node){
        err_log("EVENT MANAGER : Cannot Initialize Consensus Component.\n");
        goto mgr_exit_error;
    }

    mgr_on_process_init(ev_mgr);

	return ev_mgr;

mgr_exit_error:
    if(NULL!=ev_mgr){
        if(NULL!=ev_mgr->con_node){
        }
        free(ev_mgr);
    }
    return NULL;
}
Esempio n. 8
0
void on_receive(Os::Radio::node_id_t from, Os::Radio::size_t len, Os::Radio::block_data_t *data) {
#if APP_DATABASE_DEBUG
    debug_->debug("R %x %x l%d r%d", (int)data[0], (int)data[1], (int)len, (int)receiving);
#endif


    if(!receiving) {
        return;
    }

    if(data[0] == 0x99 && data[1] == (EXP_NR & 0xff)) {
        if(first_receive) {
            first_receive = false;
            initialize_db();
        }

        ::uint16_t pos = wiselib::read<Os, block_data_t, ::uint16_t>(data + 2);
        if(pos != 0 && pos != nextpos) {
#if APP_DATABASE_DEBUG
            debug_->debug("ignP %d!=%d", (int)pos, (int)nextpos);
#endif
            block_data_t ack[] = { 0xAA, EXP_NR & 0xff, 0, 0 };
            wiselib::write<Os, block_data_t, ::uint16_t>(ack + 2, pos);
            radio_->send(from, 4, ack);
            //radio_->send(from, 4, ack);
            //radio_->send(from, 4, ack);
            //radio_->send(from, 4, ack);
            //radio_->send(from, 4, ack);

            // only accept "later" writes, especially important
            // so we dont process the end marker twice (triggering
            // timers at the double, ouch!)
            return;
        }
        nextpos = pos + len - 4;

        //gateway_address = from;

#if APP_DATABASE_DEBUG
        debug_->debug("rcv p%d l%d p'%d", (int)pos, (int)len, (int)nextpos);
#endif

        if(len == 4) {
#if APP_DATABASE_DEBUG
            debug_->debug("rcvE");
#endif
            receiving = false;
            nextpos = 0;
            timer_->set_timer<App, &App::start_insert>(START_INSERT_INTERVAL, this, 0);
            timer_->set_timer<App, &App::disable_radio>(DISABLE_RADIO_INTERVAL, this, 0);
        }
        else {
            memcpy(rdf_buffer_ + pos, data + 4, len - 4);
        }

        block_data_t ack[] = { 0xAA, EXP_NR & 0xff, 0, 0 };
        wiselib::write<Os, block_data_t, ::uint16_t>(ack + 2, pos);
        radio_->send(from, 4, ack);
    }
    else if(data[0] == 0xbb && data[1] == (EXP_NR & 0xff)) {
#if APP_DATABASE_DEBUG
        debug_->debug("rb!");
#endif
        reboot();
    }
    else {
#if APP_DATABASE_DEBUG
        debug_->debug("ign %02x %02x %02x %02x E%02x",
                      (int)data[0], (int)data[1], (int)data[2], (int)data[3],
                      (int)EXP_NR);
#endif
    }
}