// a func that is for foreach static void shortest_path(void* data, void* func_data) { node_t* c**t = (node_t*)((MList*)data)->data; node_t* this_node = NULL; mlist_foreach(nodes, unmark, NULL); MQueue* q = mqueue_new(); mqueue_push_tail(q, (void*)c**t); rt_t* new_entry = (rt_t*)malloc(sizeof(rt_t)); routing_table = mlist_append(routing_table, (void*)new_entry); new_entry->c**t = c**t; new_entry->serv = NULL; c**t->mark = 1; // this node has reached while (!mqueue_is_empty(q)) { this_node = (node_t*)mqueue_pop_head(q); MList* neighbor_iter = this_node->neighbors; while (neighbor_iter) { node_t* tmp = (node_t*)((MList*)neighbor_iter->data)->data; if (tmp->mark) { // this node has reached before neighbor_iter = neighbor_iter->next; continue; } if (mlist_find(servs, neighbor_iter->data)) { // a server find, search complete new_entry->serv = tmp; return; } mqueue_push_tail(q, (void*)tmp); tmp->mark = 1; } } // should never reach here assert(0); }
int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Usage: %s: <ip> <port>\n", argv[0]); exit(1); } initscr(); nonl(); cbreak(); noecho(); keypad(stdscr, TRUE); if (has_colors()) { start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_YELLOW, COLOR_BLACK); init_pair(4, COLOR_BLUE, COLOR_BLACK); init_pair(5, COLOR_CYAN, COLOR_BLACK); init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(7, COLOR_WHITE, COLOR_BLACK); } int h, w; getmaxyx(stdscr, h, w); dispW = newwin(h - 2, w, 0, 0); inputW = newwin(2, w, h - 2, 0); nodelay(inputW, TRUE); scrollok(dispW, TRUE); sock = joinServer(argv[1], atoi(argv[2])); signal(SIGINT, cleanup); signal(SIGSEGV, cleanup); mvwaddch(inputW, 0, 0, ACS_ULCORNER); for (int i = 1; i < w; i++) { mvwaddch(inputW, 0, i, ACS_HLINE); } mvwaddch(inputW, 1, 0, ACS_VLINE); wrefresh(inputW); int pos = 0; char command[256]; memset(command, 0, 256); events = mqueue_create(); dispEvents = mqueue_create(); // Create event thread pthread_t networkThreadID; thread_args *args = malloc(sizeof(thread_args)); if (args == NULL) { fprintf(stderr, "Error allocating memory\n"); exit(1); } args->sock = sock; args->events = events; args->dispEvents = dispEvents; if (pthread_create(&networkThreadID, NULL, networkThread, (void *)args)) { fprintf(stderr, "Error creating thread\n"); exit(1); } pthread_t eventThreadID; if (pthread_create(&eventThreadID, NULL, eventThread, (void *)args)) { fprintf(stderr, "Error creating thread\n"); exit(1); } free(args); sendMessage(sock, "SYN"); while (1) { //char c = mvwgetch(inputW, 1, pos + 1); char c = wgetch(inputW); if (c == ERR) { // Run through display events while (!mqueue_is_empty(dispEvents)) { char *e = mqueue_dequeue(dispEvents); logMessage(e, CLIENT); free(e); } } else { // Check enter if (c == 13) { for (int i = 1; i <= pos; i++) { mvwaddch(inputW, 1, i, ' '); } command[pos] = 0; pos = 0; wmove(inputW, 1, 1); if (strlen(command) > 0) { msg_command *mc = malloc(sizeof(msg_command)); if (mc == NULL) { fprintf(stderr, "Error allocating memory\n"); exit(1); } mqueue_enqueue(events, (void *)mc, sizeof(msg_command)); } } else if (c == 127) { // Check backspace if (pos > 0) { --pos; mvwaddch(inputW, 1, pos + 2, ' '); command[pos] = 0; } } else { mvwaddch(inputW, 1, pos + 1, c); command[pos] = c; ++pos; } } } cleanup(0); return 0; }