/********************* ****************************************/ int voole_open (const char *path, struct fuse_file_info *finfo) { int err = 0 ; if ((finfo->flags & O_ACCMODE) == O_RDONLY || (finfo->flags & O_ACCMODE) == O_WRONLY || (finfo->flags & O_ACCMODE) == O_RDWR) err = 0; else { printf("open mask error\n"); return -EINVAL; } session_t *psession; tcp_cin_init(&psession); if (!psession) return -ENOSYS; finfo_t *fi = mpcalloc(sizeof(finfo_t)); fi->list_session = gdsl_list_alloc(NULL,NULL,NULL); fi->list_result = gdsl_list_alloc(NULL,NULL,NULL); fi->hash_session = gdsl_hash_alloc(NULL,NULL,NULL,(gdsl_key_func_t)keyHash, /* Session Table */ (gdsl_hash_func_t)hashHash, (gdsl_comp_func_t)compHash, 200); pthread_mutex_init(&fi->flock,NULL); finfo->fh = (uint64_t)fi; psession->iscin = 1; vfs_get_fid(path,psession->fid,sizeof(psession->fid)); gdsl_list_insert_head(fi->list_session,psession); return err; }
int tcp_session_init(session_t *psession) { psession->send_queue = gdsl_list_alloc(NULL, NULL, NULL); psession->recv_queue = gdsl_list_alloc(NULL, NULL, NULL); pthread_mutex_init(&psession->send_lock,NULL); pthread_mutex_init(&psession->recv_lock,NULL); psession->tv.tv_sec = 60; if (!psession->pdev) { psession->pdev = mpcalloc(sizeof(struct event)); event_set(psession->pdev,psession->fd,EV_READ|EV_PERSIST,tcp_session_cback,psession); event_add(psession->pdev,NULL); } return 0; }
int main() { struct sockaddr_in server_addr; bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0 server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htons(INADDR_ANY); server_addr.sin_port = htons(8000); int server_socket = socket(PF_INET,SOCK_STREAM,0); if( server_socket < 0) { printf("Create Socket Failed!"); exit(1); } int opt =1; setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) { perror("Server Bind Port 53 Failed: "); exit(1); } if ( listen(server_socket, 5) ) { printf("Server Listen Failed!"); exit(1); } pserver_context pcontext = (pserver_context) calloc(1,sizeof(server_context)); pcontext->client_context_lt = gdsl_list_alloc("client socket",NULL,NULL); pcontext->kdpfd = epoll_create(5); pthread_attr_t attr; pthread_t threadId; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* 设置线程为分离属性*/ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if(pthread_create(&threadId,&attr,message_handler,(void*)pcontext)) { perror("pthread_creat error!"); exit(-1); } while(1) { struct sockaddr_in client_addr; socklen_t length = sizeof(client_addr); printf("waiting for connection...\n"); int new_client_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length); if ( new_client_socket < 0) { printf("Server Accept Failed!\n"); /*break;*/ continue; } setnonblocking(new_client_socket); pconnect_context pconnection = calloc(1,sizeof(connect_context)); if( pconnection == NULL ) { perror("calloc event fail: "); close(new_client_socket); continue; } pconnection->ev.events = EPOLLIN; pconnection->session.socket = new_client_socket; pconnection->socket = new_client_socket; pconnection->ev.data.ptr = (void*)pconnection;/*(void*)&(pconnection->session);*/ if(epoll_ctl(pcontext->kdpfd, EPOLL_CTL_ADD, new_client_socket,&(pconnection->ev))<0) { perror("epoll_ctl fail: "); close(new_client_socket); continue; } gdsl_list_insert_tail( pcontext->client_context_lt,(int*)(size_t)new_client_socket); } return 0; }
int main (void) { int choix = 0; gdsl_list_t l = gdsl_list_alloc ("MY LIST", alloc_string, free_string); do { printf ("\t\tMENU - LIST\n\n"); printf ("\t 1> Create a cell\n"); printf ("\t 2> Remove the first cell\n"); printf ("\t 3> Remove the last cell\n"); printf ("\t 4> Remove a cell\n"); printf ("\t 5> Display list in forward order\n"); printf ("\t 6> Display list in backward order\n"); printf ("\t 7> Flush list\n"); printf ("\t 8> Size of list\n"); printf ("\t 9> Dump list\n"); printf ("\t10> XML dump of list\n"); printf ("\t11> Search for a place\n"); printf ("\t12> Search for an element\n"); printf ("\t13> Sort of list\n"); printf ("\t14> Greatest element of list\n"); printf ("\t 0> Quit\n\n"); printf ("\t\tYour choice: "); scanf ("%d", &choix); switch (choix) { case 1: { char nom[100]; int done = 0; printf ("Nom: "); scanf ("%s", nom); do { int choix; printf ("\t\tMENU - CELL INSERTION\n\n"); printf ("\t1> Insert cell at the beginning of the list\n"); printf ("\t2> Insert cell at end of list\n"); printf ("\t3> Insert cell after another cell\n"); printf ("\t4> Insert cell before another cell\n"); printf ("\t5> Display the list\n"); printf ("\t0> RETURN TO MAIN MENU\n\n"); printf ("\t\tYour choice: "); scanf ("%d", &choix ); switch (choix) { case 1: { gdsl_list_insert_head (l, nom); done = 1; } break; case 2: { gdsl_list_insert_tail (l, nom); done = 1; } break; case 3: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { char Nom[100]; gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l); printf ("Name of cell after which you want to insert: "); scanf ("%s", Nom); if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom)) { printf ("The cell '%s' doesn't exist\n", Nom); } else { gdsl_list_cursor_insert_after (c, nom); done = 1; } gdsl_list_cursor_free (c); } break; case 4: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { char Nom[100]; gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l); printf ("Name of cell before which you want to insert: "); scanf ("%s", Nom); if (!gdsl_list_cursor_move_to_value (c, compare_strings, Nom)) { printf ("The cell '%s' doesn't exist\n", Nom); } else { gdsl_list_cursor_insert_before (c, nom); done = 1; } gdsl_list_cursor_free (c); } break; case 5: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { affiche_liste_chaines_fwd (l); } break; case 0: done = 1; break; } } while (!done); } break; case 2: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { gdsl_list_delete_head (l); } break; case 3: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { gdsl_list_delete_tail (l); } break; case 4: { char nom[100]; if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { printf ("Name of cell to remove: "); scanf ("%s", nom); if (!gdsl_list_delete (l, compare_strings, nom)) { printf ("The cell '%s' doesn't exist\n", nom); } else { printf ("The cell '%s' is removed from list\n", nom); } } } break; case 5: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { affiche_liste_chaines_fwd (l); } break; case 6: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { affiche_liste_chaines_bwd (l); } break; case 7: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { gdsl_list_flush (l); } break; case 8: printf ("Card( %s ) = %ld\n", gdsl_list_get_name (l), gdsl_list_get_size (l)); break; case 9: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { gdsl_list_dump (l, print_string, stdout, NULL); } break; case 10: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { gdsl_list_write_xml (l, print_string, stdout, NULL); } break; case 11: { int pos; gdsl_element_t e; printf ("Enter the position of the place to search for: "); scanf ("%d", & pos); e = gdsl_list_search_by_position (l, (ulong) pos); if (e != NULL) { print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL); } } break; case 12: { char nom [100]; gdsl_element_t e; printf ("Name of cell to search for: "); scanf ("%s", nom); e = gdsl_list_search (l, compare_strings, nom); if (e == NULL) { printf ("The cell '%s' doesn't exist\n", nom); } else { printf ("The cell '%s' was found: ", nom); print_string (e, stdout, GDSL_LOCATION_UNDEF, NULL); printf ("\n"); } } break; case 13: gdsl_list_sort (l, compare_strings); break; case 14: if (gdsl_list_is_empty (l)) { printf ("The list is empty.\n"); } else { printf ("Max Element: %s\n", (char*) gdsl_list_search_max (l, compare_strings)); } break; case 15: /* case for my own tests... */ { int i; gdsl_perm_t p = gdsl_perm_alloc ("p", PERMUTATION_NB); gdsl_list_t g = gdsl_list_alloc ("MY LIST 2", alloc_string, free_string); gdsl_perm_randomize (p); for (i = 0; i < PERMUTATION_NB; i++) { char c[2]; c[0] = 65 + gdsl_perm_get_element (p, i); c[1] = '\0'; gdsl_list_insert_tail (g, c); } gdsl_perm_free (p); affiche_liste_chaines_fwd (g); affiche_liste_chaines_bwd (g); printf ("SORT\n"); gdsl_list_sort (g, compare_strings); affiche_liste_chaines_fwd (g); affiche_liste_chaines_bwd (g); gdsl_list_free (g); } { int i = 0; gdsl_list_cursor_t c = gdsl_list_cursor_alloc (l); for (gdsl_list_cursor_move_to_head (c); gdsl_list_cursor_get_content (c); gdsl_list_cursor_step_forward (c)) { char toto[50]; sprintf (toto, "%d", i++); gdsl_list_cursor_insert_before (c, toto); gdsl_list_cursor_step_backward (c); gdsl_list_cursor_delete_after (c); } gdsl_list_cursor_free (c); } break; } } while (choix != 0); gdsl_list_free (l); exit (EXIT_SUCCESS); }
centernode_t * centernode_init (const char *progname, const char *cfgfile) { char filename[100]; centernode_t *cn = (centernode_t *) calloc(sizeof(centernode_t), sizeof(char)); if (!cn) abort(); //Initialize the global mutex. pthread_mutex_init(&cn->seqlock, 0); pthread_mutex_init(&cn->objlock, 0); pthread_mutex_init(&cn->reglock, 0); pthread_mutex_init(&cn->evtlock, 0); pthread_mutex_init(&cn->glock, 0); pthread_mutex_init(&cn->sqllock, 0); //Initialiaze and load the config file. cn->config = calloc(sizeof(config_t), sizeof(char)); if (!cn->config) abort(); config_init(cn->config); /* initialize configuration */ if (!config_read_file(cn->config, cfgfile)) { #ifdef CDNDEBUG fprintf(stderr, "%s.\n", config_error_text(cn->config)); #endif return (free(cn), NULL); } if (centernode_config(cn)) /* load config */ return NULL; strncpy(cn->confile, cfgfile, sizeof(cn->confile) -1); if (archive_init(cn->homepath, progname, ATASR|ATLOG, (cbsink_t)NULL, cn)) return NULL; /* archive module */ /* if (mysqldb_init(cn->dbhost, cn->dbname, cn->dbuser, cn->dbpasswd)) // db return (centernode_cleanup(cn), NULL); */ mongoc_init (); cn->MongoNum = cn->threads; cn->Mongo = (struct Mongo*)calloc (sizeof(struct Mongo)*cn->MongoNum, sizeof(char)); int i = 0; for (i=0; i<cn->MongoNum; i++) { cn->Mongo[i].MongoClient = mongoc_client_new (cn->mongoString); cn->Mongo[i].MongoCollection = mongoc_client_get_collection (cn->Mongo[i].MongoClient, "mydb", "mycoll"); pthread_mutex_init (&cn->Mongo[i].MongoLock, 0); if (cn->Mongo[i].MongoClient == NULL ) abort(); } cn->rdb_conn = (redisContext *) redisConnect(cn->rdb_host, cn->rdb_port);//, cn->rdb_pswd); printf("init cn %p rdb %p\n", cn, cn->rdb_conn); if (cn->rdb_conn == NULL) { LOG("WR", "Conn redis er\n"); abort(); return NULL; } pthread_mutex_init (&cn->rdb_lk, 0); redis_get_udpinfo(cn, (char **)&cn->udp_addr, &cn->udp_port); //Allocate management object cn->accesses = gdsl_hash_alloc(NULL, NULL, NULL, (gdsl_key_func_t)keyHash, (gdsl_hash_func_t)hashHash, (gdsl_hash_comp_t)compHash, 20000); cn->events = gdsl_queue_alloc(NULL, NULL, NULL); cn->registration = gdsl_queue_alloc(NULL, NULL, NULL); cn->oemconfig_lt = gdsl_list_alloc(NULL, NULL, NULL); cn->service_lt = gdsl_list_alloc(NULL,NULL,NULL); if (!(cn->tpool = threadpool_init(cn->threads, 4))) return NULL; cn->rdbInterval = 0; cn->rdbLastNull = time(NULL); cn->rdbStatus = 0; //Bind the network interface. cnaccess_t *ca = NULL; //Connect management server for (i=0; i<cn->local_num; ++i) { /* Bind the network devices */ ca = centernode_server(cn, cn->local[i].host, cn->local[i].port, NT_INETSRV, cn->local[i].isp); if (!ca) { return NULL; } } #if 0 if ((cn->isservice & 0x02) > 0) { for( i=0; i<cn->service_num; ++i ) { ca = centernode_connect(cn, cn->service[i].host, cn->service[i].port, 0, NT_SERVICE_CLIENT, 0); if( ca ) { centernode_cmd_service_startup(cn, ca,NULL,i); //break; } } if( !ca ) return (centernode_cleanup(cn), NULL); } #endif sprintf(filename, "%s/%s.pid", cn->homepath, progname); FILE *fp = fopen(filename, "w+"); /* version number */ assert(fp && fprintf(fp, "%u", getpid()) > 0); fclose(fp); kill(getpid(), SIGUSR1); #ifdef CDNDEBUG LOG("WR", "Service CCN %s startup.\n", cn->nodeid); #endif //if (appinit(cn) != 0) //return (centernode_cleanup(cn), NULL); return cn; }