int initialize_modules(void) { int rc = PLCTAG_STATUS_OK; /* loop until we get the lock flag */ while (!lock_acquire((lock_t*)&library_initialization_lock)) { sleep_ms(1); } if(!library_initialized) { pdebug(DEBUG_INFO,"Initialized library modules."); rc = lib_init(); if(rc == PLCTAG_STATUS_OK) { rc = ab_init(); } library_initialized = 1; /* hook the destructor */ atexit(destroy_modules); pdebug(DEBUG_INFO,"Done initializing library modules."); } /* we hold the lock, so clear it.*/ lock_release((lock_t*)&library_initialization_lock); return rc; }
int main (int argc, char *argv[]) { printf ("%s %s\n", __TIME__, __DATE__); char data[256] = {0}; long i = -1; long addr = 0x10001c10; struct pt_regs regs = {{0}}; pid_t traced_process = 0; if(argc != 2) { printf("Usage: %s <pid to be traced>\n", argv[0], argv[1]); exit(1); } traced_process = atoi(argv[1]); lib_init (traced_process); getregs(traced_process, ®s); long *ptr = (long *)®s; for (i = 0;i < sizeof (struct pt_regs) / sizeof (long);i ++) { printf ("0x%08x\n", *(ptr ++)); } getdata(traced_process, addr, data, 256); i = -1; while (++i < 256) { printf ("0x%02x\n", *(data+i)); } lib_destroy (traced_process); return 0; }
int main() { lib_init(); socket_t * server = socket_new(); int port = 5001; if(-1 == socket_bind(server, port)) { printf("port %i is busy\n", port); exit(1); } socket_listen(server); char buf[10000]; socket_t * client = NULL; while(1) { client = socket_accept(server); socket_read(client, buf, sizeof(buf)); printf("%s",buf); if (strlen(buf) != 0){ http_request_t rs; rs = http_request_parse(buf); if (strcmp(rs.method,"GET") == 0 && strcmp(rs.uri, "/info") == 0 ) { server_info(client); } } } return 0; }
int main(void) { // initializing first three investors for program to start with // initializing_END int PORT = 5000; lib_init(); winSock = socket_new(); db_t * db = db_new("teacher.db"); // Checking if socket is not busy, closing app if it is if (socket_bind(winSock, PORT) == SOCKET_ERROR) { printf("Cannot bind %i port\n", PORT); socket_close(winSock); socket_free(winSock); return 0; } socket_listen(winSock); char buf[10000]; socket_t * client = NULL; // main cycle of the program while (1) { printf("Awaiting for connections...\n"); client = socket_accept(winSock); // Checking if client is not null, closing app if he is if (client == NULL) { printf("NULL client, closing app...\n"); break; } int readStatus = socket_read(client, buf, sizeof(buf)); // Skipping empty request (may appear from time to time) if (readStatus <= 0) { printf("Empty request, skipping...\n"); socket_close(client); socket_free(client); continue; } // Printing info about the received request to console printf(">> Got request (readStatus: %i):\n'%s'\n", readStatus, buf); http_request_t request = http_request_parse(buf); // check the type/path of request (API/HTML) & analyze the method (GET/POST/DELETE) // and provide the client with proper answer server_analyzeRequest(&request, client, db); socket_free(client); } // end of program socket_close(winSock); socket_free(winSock); db_free(db); lib_free(); return 0; }
void mrb_mruby_digest_gem_init(mrb_state *mrb) { struct RClass *b, *d, *h; lib_init(); d = mrb_define_module(mrb, "Digest"); b = mrb_define_class_under(mrb, d, "Base", mrb->object_class); mrb_define_method(mrb, b, "block_length", mrb_digest_block_length, MRB_ARGS_NONE()); mrb_define_method(mrb, b, "digest", mrb_digest_digest, MRB_ARGS_NONE()); mrb_define_method(mrb, b, "digest!", mrb_digest_digest_bang, MRB_ARGS_NONE()); /* XXX: can be defined in mrblib... */ mrb_define_method(mrb, b, "digest_length", mrb_digest_digest_length, MRB_ARGS_NONE()); //mrb_define_method(mrb, b, "file", mrb_digest_file, MRB_ARGS_REQ(1)); mrb_define_method(mrb, b, "hexdigest", mrb_digest_hexdigest, MRB_ARGS_NONE()); //mrb_define_method(mrb, b, "hexdigest!", mrb_digest_hexdigest_bang, MRB_ARGS_NONE()); /* XXX: can be defined in mrblib... */ mrb_define_method(mrb, b, "initialize", mrb_digest_init, MRB_ARGS_NONE()); mrb_define_method(mrb, b, "initialize_copy", mrb_digest_init_copy, MRB_ARGS_REQ(1)); mrb_define_method(mrb, b, "reset", mrb_digest_reset, MRB_ARGS_NONE()); mrb_define_method(mrb, b, "update", mrb_digest_update, MRB_ARGS_REQ(1)); #define DEFCLASS(n) \ do { \ struct RClass *a = mrb_define_class_under(mrb, d, #n, b); \ MRB_SET_INSTANCE_TT(a, MRB_TT_DATA); \ mrb_define_const(mrb, a, TYPESYM, mrb_fixnum_value(MD_TYPE_##n)); \ } while (0) #ifdef HAVE_MD5 DEFCLASS(MD5); #endif #ifdef HAVE_RMD160 DEFCLASS(RMD160); #endif #ifdef HAVE_SHA1 DEFCLASS(SHA1); #endif #ifdef HAVE_SHA256 DEFCLASS(SHA256); #endif #ifdef HAVE_SHA384 DEFCLASS(SHA384); #endif #ifdef HAVE_SHA512 DEFCLASS(SHA512); #endif h = mrb_define_class_under(mrb, d, "HMAC", mrb->object_class); MRB_SET_INSTANCE_TT(h, MRB_TT_DATA); mrb_define_method(mrb, h, "block_length", mrb_hmac_block_length, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "digest", mrb_hmac_digest, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "digest_length", mrb_hmac_digest_length, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "hexdigest", mrb_hmac_hexdigest, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "initialize", mrb_hmac_init, MRB_ARGS_REQ(2)); mrb_define_method(mrb, h, "initialize_copy", mrb_hmac_init_copy, MRB_ARGS_REQ(1)); mrb_define_method(mrb, h, "update", mrb_hmac_update, MRB_ARGS_REQ(1)); }
int main(int argc, char *argv[]) { SSL_CTX *ctx; const SSL_METHOD *method = SSLv3_client_method(); int client_fd; char *host; char *portnum; int bench_send = 0; int bench_recv = 0; int i; enum cipher_choice cipher_choice = CIPHER_ALL; if (argc < 3) { printf("Usage: %s <host_ip> <portnum> [opts]\n", argv[0]); exit(-1); } host = argv[1]; portnum = argv[2]; lib_init(); for (i = 3; i < argc; i++) { if (strcmp("tls-1.2", argv[i]) == 0) { method = TLSv1_2_client_method(); } else if (strcmp("tls-1.1", argv[i]) == 0) { method = TLSv1_1_client_method(); } else if (strcmp("tls-1.0", argv[i]) == 0) { method = TLSv1_client_method(); } else if (strcmp("ssl-3.0", argv[i]) == 0) { method = SSLv3_client_method(); } else if (strcmp("bench-send", argv[i]) == 0) { bench_send = atoi(argv[++i]); } else if (strcmp("bench-recv", argv[i]) == 0) { bench_recv = atoi(argv[++i]); } else { printf("warning: unknown option: \"%s\"\n", argv[i]); } } ctx = client_init(method, cipher_choice); client_fd = connect_socket(host, atoi(portnum)); printf("[status] connected. handshaking\n"); SSL *ssl; ssl = SSL_new(ctx); SSL_set_fd(ssl, client_fd); if (bench_send > 0 || bench_recv > 0) benchmark(ssl, bench_send, bench_recv); else process(ssl); close(client_fd); SSL_CTX_free(ctx); return 0; }
/** Initializes this process. @param first Whether shared resources should also be initialized. @return 0 if successful and -1 otherwise. **/ int init(const bool first) { /* Initializes the configuration. */ if (cfg_init_lib() == -1) { return -1; } /* Initializes the functions. */ if (lib_init() == -1) { return -1; } /* Initializes the save-quit-load emulation. */ if (cfg_emulate_sql) { if (asm_inject(&save_quit_load) == -1) { return -1; } } else { if (asm_inject(NULL) == -1) { return -1; } } /* Initializes the shared memory segment. */ if (first) { if (shm_init() == -1) { return -1; } } if (shm_attach() == -1) { return -1; } /* Sets variables that should be automatic. */ record.timestamp = cfg_timestamp; options.play_on = cfg_play_instantly; options.play_paused = FALSE; options.gui_menu = FALSE; options.gui_info = FALSE; options.gui_overlay = FALSE; options.gui_condensed = FALSE; options.gui_hidden = FALSE; options.roll_on = FALSE; options.roll_cataloged = FALSE; return 0; }
void test_init(void) { test_prefix = NULL; failure_count = 0; total_count = 0; lib_init(); i_set_error_handler(test_error_handler); }
static void vUpdateTask(void *pvParameters) { drv_Init(); lib_init(); task_init(); while(1) { lib_update(); task_update(); } }
int main(void) { lib_init(); http_server_t * server = http_server_new(); puts("Server started on port 8080..."); http_server_start(server, 8080); http_server_delete(server); lib_free(); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { int err = 0; int hsize, i, tc; if (argc == 3) { hsize = atoi(argv[1]); tc = atoi(argv[2]); } else { hsize = 1024; tc = 20; } hvfs_info(mds, "DH UNIT TESTing (%d)...\n", hsize); /* init mds unit test */ lib_init(); err = mds_init(10); if (err) { hvfs_err(mds, "mds_init() failed %d\n", err); goto out; } /* insert to DH */ for (i = 0; i < tc; i++) { dh_insert(i); } /* search in DH */ for (i = 0; i < tc; i++) { dh_search(i); } /* remove from DH */ for (i = 0; i < tc; i++) { dh_remove(i); } /* let us insert the GDT DH */ hvfs_info(mds, ">>>>>>>>>>>>\n"); dh_insert(0); hvfs_info(mds, ">>>>>>>>>>>>\n"); bitmap_insert(0, 0); hvfs_info(mds, ">>>>>>>>>>>>\n"); /* re-search in DH */ for (i = 0; i < tc; i++) { dh_search(i); } mds_destroy(); out: return err; }
int main(int argc, char ** argv) { Giggle* giggle = lib_init(argc, argv); giggle->renderer = sdl_nonthreaded_renderer(giggle); giggle->logic = default_gamelogic(giggle); giggle->audio = sdl_streaming_audio(giggle); giggle->renderer->initialize(); giggle->audio->initialize(); giggle->logic->run(); return 0; }
// HTTP SERVER ON 80 PORT // TO POST USE THE ACTUAL NAMES OF VARIABLES WITHOUT STRUCTS IN STRUCTS etc // /exit to exit from http server int main(void) { // Lin init LIBXML_TEST_VERSION lib_init(); http_server_t * server = http_server_new(80); http_server_start(server, "/designers.xml"); http_server_delete(server); lib_free(); return EXIT_SUCCESS; }
int main(int argc, char** argv){ int rx_mode = DMA_MODE,tx_mode = DMA_MODE; int recv_mode = RECEIVE_MODE; char ifname[10] = {0}, *bindstr; char rxif[128], *pif; int numproc = 4, i = 0; lib_init(); for(i=1; i<argc; i++){ if(!strncmp(argv[i], "-r", 2)){ sprintf(rxif, "%s", argv[i]+2); } } pif = rxif; sscanf(pif, "eth%d", &rx_port); sprintf(ifname, "eth%d", rx_port); sockfd = open_sock(ifname, rx_mode, tx_mode, recv_mode, numproc); if(sockfd < 0) { printf("open_sock %s failure!\n",ifname); } else { printf("open_sock %s success!\n",ifname); } bindstr = "1:2:3:4"; set_sockopt(sockfd,SET_BINDING,bindstr); start_proc(sockfd); printf("start now...\n"); while(1) { tIfStat rxstat; get_sockopt(sockfd, GET_IF_RXTX_STAT, (int *)&rxstat); //system("clear"); //write_in_file(rxstat.rx_bps, rxstat.rx_pps); db_insert(rxstat.rx_bps*8+rxstat.rx_pps*PAD_LEN*8, rxstat.rx_pps); printf("eth%d %lu bps, %u pps.\n", sockfd, rxstat.rx_bps*8+rxstat.rx_pps*PAD_LEN*8, rxstat.rx_pps); sleep(10); //system("clear"); } close_sock(sockfd); return 0; }
int main(){ lib_init(); socket_t * serverSocket = socket_new(); socket_bind(serverSocket, 5000); socket_listen(serverSocket); film_maker_t* film_makers[FILM_MAKERS_AMOUNT]; for (int i = 0; i < FILM_MAKERS_AMOUNT; i++) { film_makers[i] = film_maker_new(); } parse(film_makers,"XML_format.xml"); while(1) { socket_t * clientSocket = socket_accept(serverSocket); char buf[102400]; if(socket_read(clientSocket, buf, 102400) == 0) { socket_close(clientSocket); socket_free(clientSocket); puts("Skipping empty request"); continue; } http_request_t req = http_request_parse(buf); server_reply(clientSocket, req, film_makers); //puts("fghj"); socket_close(clientSocket); socket_free(clientSocket); } for (int i = 0; i < 3; i++) film_maker_free(film_makers[i]); socket_close(serverSocket); socket_free(serverSocket); lib_free(); return 0; }
static int test_input_file(const char *path) { struct istream *file_input, *input, *input2; const unsigned char *data; size_t size; struct sha1_ctxt hash; unsigned char hash_file[SHA1_RESULTLEN], hash_converter[SHA1_RESULTLEN]; int ret = 0; lib_init(); file_input = i_stream_create_file(path, 64); /* get hash when directly reading input */ input = i_stream_create_crlf(file_input); sha1_init(&hash); while (i_stream_read_data(input, &data, &size, 0) > 0) { sha1_loop(&hash, data, size); i_stream_skip(input, size); } sha1_result(&hash, hash_file); i_stream_unref(&input); /* get hash when going through converter */ i_stream_seek(file_input, 0); input = i_stream_create_crlf(file_input); input2 = i_stream_create_binary_converter(input); sha1_init(&hash); while (i_stream_read_data(input2, &data, &size, 0) > 0) { sha1_loop(&hash, data, size); i_stream_skip(input2, size); } sha1_result(&hash, hash_converter); i_stream_unref(&input2); i_stream_unref(&input); if (memcmp(hash_file, hash_converter, SHA1_RESULTLEN) != 0) { fprintf(stderr, "istream-binary-converter: mismatch on file %s\n", path); ret = 1; } i_stream_unref(&file_input); lib_deinit(); return ret; }
int main(int argc,char *argv[]) { if(init_arg(argc,argv,workspace,sizeof(workspace))!=0) exit(1); if(read_config_file()<0) exit(1); start_log(); if(lib_init(datamodel_file)<0){ tr_log(ERROR,"initial datamodel_file:%s failed!",datamodel_file); exit(1); } tr_log(DEBUG,"hello debug"); tr_log(WARNING,"hello warning"); tr_log(NOTICE,"hello notice"); tr_log(ERROR,"hello error"); return 0; }
int main() { lib_init(); list_t* list=list_new(); lanser * freeLanser[10]; for (int i = 0; i < 10; i++) freeLanser[i] = Freelanser_new(); // parse(freeLanser); socket_t * serverSocket = socket_new(); socket_bind(serverSocket, 5000); socket_listen(serverSocket); while (1) { socket_t * clientSocket = socket_accept(serverSocket); char buf[10000]=""; if (!socket_read(clientSocket, buf, sizeof(buf))) { puts("Skipping empty request"); socket_close(clientSocket); socket_free(clientSocket); continue; } http_request_t req = http_request_parse(buf); server_answer(list,req,clientSocket,freeLanser); socket_close(clientSocket); socket_free(clientSocket); } socket_close(serverSocket); socket_free(serverSocket); lib_free(); list_free(list); return 0; }
int main() { lib_init(); socket_t * server = socket_new(); socket_bind(server, 5000); socket_listen(server); char buf[10000]; char pathBuf[256]; socket_t * client = NULL; while(1) { client = socket_accept(server); socket_read(client, buf, sizeof(buf)); if (strlen(buf) == 0) continue; printf(">> Got request:\n%s\n", buf); http_getPath(buf, pathBuf, sizeof(pathBuf)); http_request_t request = http_request_parse(buf); if (strcmp(request.uri, "/") == 0) { server_homepage(client); } else if (strcmp(request.uri, "/database") == 0) { server_students(client, &request); } else if (strcmp(request.uri, "/info") == 0) { server_info(client); } else if (strcmp(request.uri, "/filename") == 0) { server_file_parse(client); } else { server_notFound(client); } socket_free(client); } socket_free(server); lib_free(); return 0; }
int main(int argc,const char *argv[]) { mrb_state *mrb; mrb_value ret; // initialize mruby if (!(mrb = mrb_open())) { fprintf(stderr,"%s: could not initialize mruby\n",argv[0]); return -1; } mrb_value args = mrb_ary_new(mrb); int i; // convert argv into mruby strings for (i=1; i<argc; i++) { mrb_ary_push(mrb, args, mrb_str_new_cstr(mrb,argv[i])); } mrb_define_global_const(mrb, "ARGV", args); // load the compiled library ret = lib_init(mrb); // check for exception if (mrb->exc) { // print exception mrb_print_error(mrb); } // cleanup mrb_close(mrb); return mrb->exc != NULL; }
void http_request_startServer(pupils_t pupils){ lib_init(); socket_t * serverSocket = socket_new(); socket_bind(serverSocket, 5000); socket_listen(serverSocket); while(1){ puts("Waiting for connections"); socket_t * clientSocket = socket_accept(serverSocket); puts("New client"); char buff[BUFFER_LENGTH]; int readLength = socket_read(clientSocket, buff, BUFFER_LENGTH); if(readLength == 0){ socket_close(clientSocket); socket_free(clientSocket); puts("Skipping empty request"); continue; } printf("Got Request:\n---------------\n%s\n----------------\n", buff); http_request_t req = http_request_parse(buff); printf("Method: %s\nURI: %s\n", req.method, req.uri); puts("Data:"); for(int i = 0; i < req.formLength; i++){ char * kvStr = keyvalue_toString(&req.form[i]); printf("\t%s\n", kvStr); free(kvStr); } http_request_chooseMethod(req, clientSocket, pupils); socket_close(clientSocket); socket_free(clientSocket); } socket_close(serverSocket); socket_free(serverSocket); lib_free(); }
int main() { lib_init(); socket_t * server = socket_new(); socket_bind(server, 5000); socket_listen(server); char buf[10000]; char pathBuf[256]; socket_t * client = NULL; while(1) { client = socket_accept(server); socket_read(client, buf, sizeof(buf)); http_request_t rs; rs = http_request_parse(buf); server_reply(client,rs); } socket_free(server); socket_free(client); lib_free(); return 0; }
int main() { lib_init(); socket_t * server = socket_new(); socket_bind(server, 5000); socket_listen(server); char buf[10000]; char pathBuf[256]; socket_t * client = NULL; while(1) { client = socket_accept(server); socket_read(client, buf, sizeof(buf)); if (strlen(buf) == 0) continue; printf(">> Got request:\n%s\n", buf); http_getPath(buf, pathBuf, sizeof(pathBuf)); http_request_t request = http_request_parse(buf); if (strcmp(request.uri, "/") == 0) { server_homepage(client); } else if (strcmp(request.uri, "/api/admins") == 0) { server_database(client); } else if (strcmp(request.uri, "/api/admins/") > 0) { server_database_id(client, &request); } else { error_massage(client, "404 - NOT FOUND!"); } socket_free(client); } socket_free(server); lib_free(); return 0; }
int main() { db_t* base = db_new("workers.db"); lib_init(); socket_t* server = socket_new(); socket_bind(server, PORT); socket_listen(server); char buffer[10000]; while(true) { puts("Waiting for client...!"); socket_t* client = socket_accept(server); if(socket_read(client, buffer, sizeof(buffer)) <= 0) { socket_close(client); socket_free(client); continue; } printf("Request:\n%s\n", buffer); http_request_t request = http_request_parse(buffer); server_answerRequest(request, client, base); socket_free(client); } socket_free(server); lib_free(); db_free(base); return 0; }
int root_init(void) { int err = 0; /* lib init */ lib_init(); /* FIXME: decode the cmdline */ /* FIXME: configrations */ /* default configurations */ hro.conf.ring_push_interval = 600; /* 600 seconds */ /* get configs from env */ err = root_config(); if (err) goto out_config; /* verify the configs */ err = root_verify(); if (err) goto out_verify; err = root_init_signal(); if (err) goto out_signal; /* init hro */ err = site_mgr_init(&hro.site); if (err) goto out_site_mgr; err = ring_mgr_init(&hro.ring); if (err) goto out_ring_mgr; err = root_mgr_init(&hro.root); if (err) goto out_root_mgr; err = addr_mgr_init(&hro.addr); if (err) goto out_addr_mgr; /* FIXME: in the service threads' pool */ err = root_spool_create(); if (err) goto out_spool; /* FIXME: setup the timers */ err = root_setup_timers(); if (err) goto out_timers; /* FIXME: setup the profile unit */ err = root_setup_profile(); if (err) goto out_profile; /* maks the SIGUSR1 signal for main thread */ { sigset_t set; sigemptyset(&set); sigaddset(&set, SIGUSR1); pthread_sigmask(SIG_BLOCK, &set, NULL); } /* ok to run */ hro.state = HRO_STATE_RUNNING; out_profile: out_timers: out_spool: out_addr_mgr: out_root_mgr: out_ring_mgr: out_site_mgr: out_signal: out_verify: out_config: return err; }
int main(){ lib_init(); socket_t * server = socket_new(); socket_bind(server, 5000); socket_listen(server); char buffer[10000]; socket_t * client = NULL; list_t * pupils = list_new(); char jsonText[10000]; FILE * fp = fopen("pupil.json","r"); fread(jsonText,sizeof(char),10000,fp); pupil_createPupilsList(pupils,cJSON_Parse(jsonText)); while (1) { socket_t * client = socket_accept(server); socket_read(client, buffer, sizeof(buffer)); printf("%s", buffer); server_request_t request = server_request_parse(buffer); if (strcmp(request.method, "GET") == 0){ if(strcmp(request.uri,"/")==0){ server_openPage(client); } else if (strcmp(request.uri,"/api/pupils")==0){ server_sendJson(client,pupil_listToJson(pupils)); } else if(strncmp(request.uri,"/api/pupils/",strlen("/api/pupils/"))==0){ int id; int num = sscanf(request.uri, "/api/pupils/%d", &id); if(num != 0){ pupil_t * p = server_getPupilById(pupils,id); if(p == NULL){ server_sendWrongIndex(client); } else server_sendJson(client,pupil_pupilToJson(p)); } } else if(strcmp(request.uri, "/pupils")==0){ server_sendListAsHtml(client,pupils); } else if(strncmp(request.uri,"/pupils/",strlen("/pupils/"))==0){ int id; int num = sscanf(request.uri, "/pupils/%d", &id); if(num != 0){ pupil_t * p = server_getPupilById(pupils,id); if(p == NULL){ server_wrongIndex_html(client); } else server_sendHtml(client,pupil_getHtml(p,pupil_getId(p))); } } else if(strcmp(request.uri, "/new-pupil")==0){ server_addPupilHtml(client,pupils); } } else if (strcmp(request.method, "DELETE") == 0){ if (strstr(request.uri, "/api/pupils/") != NULL){ int id; int isNumber = sscanf(request.uri, "/api/pupils/%d", &id); if (isNumber){ server_deletePupil(client,pupils, id); } else{ server_sendWrongIndex(client); } } else if (strstr(request.uri,"/pupils/")!= NULL){ int Id; int num = sscanf(request.uri,"/pupils/%d", &Id); if(num){ server_deletePupil_html(client,pupils,Id); } else { server_wrongIndex_html(client); } } } else if (strcmp(request.method, "POST") == 0){ if (strcmp(request.uri,"/api/pupils")==0) { server_addPupil(client,pupils,buffer); } else if (strcmp(request.uri,"/pupils")==0) { server_newHtmlPupil(client,pupils,buffer); } } socket_free(client); } socket_free(client); socket_free(server); list_free(pupils); lib_free(); return 0; }
/* mdsl_init() */ int mdsl_init(void) { int err; /* lib init */ lib_init(); mdsl_pre_init(); /* FIXME: decode the cmdline */ /* FIXME: configurations */ mdsl_config(); /* init the txg_compact_cache */ err = mdsl_tcc_init(); if (err) goto out_tcc; /* Init the signal handlers */ err = mdsl_init_signal(); if (err) goto out_signal; /* FIXME: setup the timers */ err = mdsl_setup_timers(); if (err) goto out_timers; /* init storage */ err = mdsl_storage_init(); if (err) goto out_storage; /* FIXME: init the service threads' pool */ err = mdsl_spool_create(); if (err) goto out_spool; /* init the aio threads */ err = mdsl_aio_create(); if (err) goto out_aio; /* mask the SIGUSR1 signal for main thread */ { sigset_t set; sigemptyset(&set); sigaddset(&set, SIGUSR1); pthread_sigmask(SIG_BLOCK, &set, NULL); } /* ok to run */ hmo.state = HMO_STATE_RUNNING; out_aio: out_spool: out_storage: out_timers: out_signal: out_tcc: return err; }
int main() { lib_init(); printf("PORT: %i\n\n", PORT); socket_t * server = socket_new(); socket_bind(server, PORT); socket_listen(server); char buffer[10000]; socket_t * client = NULL; // работа с базой данных const char * dbFile = "ScrumMaster.db"; db_t * db = db_new(dbFile); // работа с базой данных while(1) { client = socket_accept(server); socket_read(client, buffer, sizeof(buffer)); if(strlen(buffer) != 0) { printf(">> Got request:\n%s\n", buffer); http_request_t request = http_request_parse(buffer); if (strcmp(request.uri, "/") == 0) { server_homepage(client); } else if (strcmp(request.uri, "/api/ScrumMasters") == 0) // else if (strncmp(request.uri, "/api/ScrumMasters?", 18) == 0) { server_masters(client, &request, db); } else if (strncmp(request.uri, "/api/ScrumMasters/", 18) == 0) { server_mastersByID(client, &request, db); } else if (strcmp(request.uri, "/ScrumMasters") == 0) { server_mastersHtml(client, &request, db); } else if (strncmp(request.uri, "/ScrumMasters/", 14) == 0) { server_mastersHtmlByID(client, &request, db); } else if (strcmp(request.uri, "/new-ScrumMaster") == 0) { server_mastersHtmlPOST(client, &request, db); } else { server_notFound(client); } } } db_free(db); socket_free(client); socket_free(server); lib_free(); return 0; }
int main(void) { WSADATA Data; SOCKADDR_IN recvSockAddr; SOCKET recvSocket; int status; int numrcv = 0; struct hostent * remoteHost; char * ip; const char * host_name = "pb-homework.appspot.com"; char buffer[MAXBUFLEN]; memset(buffer,0,MAXBUFLEN); // Initialize Windows Socket DLL status = WSAStartup(MAKEWORD(2, 2), &Data); if(status != 0) { printf("ERROR: WSAStartup unsuccessful\r\n"); return 0; } // Get IP address from host name remoteHost = gethostbyname(host_name); ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list); printf("IP address is: %s.\n", ip); memset(&recvSockAddr, 0, sizeof(recvSockAddr)); // zero the sockaddr_in structure recvSockAddr.sin_port=htons(PORT); // specify the port portion of the address recvSockAddr.sin_family=AF_INET; // specify the address family as Internet recvSockAddr.sin_addr.s_addr= inet_addr(ip); // specify ip address // Create socket recvSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(recvSocket == INVALID_SOCKET) { printf("ERROR: socket unsuccessful\r\n"); WSACleanup(); system("pause"); return 0; } // Connect if(connect(recvSocket,(SOCKADDR*)&recvSockAddr,sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { printf("ERROR: socket could not connect\r\n"); closesocket(recvSocket); WSACleanup(); return 0; } // Send request char request[200]; sprintf(request, "GET /test/var/24?format=json HTTP/1.1\r\nHost:%s\r\n\r\n", host_name); // add Host header with host_name value send(recvSocket, request, strlen(request), 0); // Receieve numrcv = recv(recvSocket, buffer, MAXBUFLEN, NO_FLAGS_SET); if (numrcv == SOCKET_ERROR) { printf("ERROR: recvfrom unsuccessful\r\n"); status = closesocket(recvSocket); if(status == SOCKET_ERROR) printf("ERROR: closesocket unsuccessful\r\n"); status = WSACleanup(); if (status == SOCKET_ERROR) printf("ERROR: WSACleanup unsuccessful\r\n"); system("pause"); return(1); } // Print out receieved socket data printf("%s\r\n", buffer); char * json=strstr(buffer,"{"); cJSON * SI = cJSON_CreateObject(); SI=cJSON_Parse(json); author_t * author=malloc(sizeof(author_t)); author->author=malloc(strlen(cJSON_GetObjectItem(SI,"author")->valuestring)+1); strcpy(author->author,cJSON_GetObjectItem(SI,"author")->valuestring); author->quote=malloc(strlen(cJSON_GetObjectItem(SI,"quote")->valuestring)+1); strcpy(author->quote,cJSON_GetObjectItem(SI,"quote")->valuestring); time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); author->time=malloc(strlen(asctime(timeinfo))); strcpy(author->time,asctime(timeinfo)); author->time[strlen(author->time)-1]='\0'; cJSON * SM = cJSON_CreateObject(); cJSON_AddItemToObject(SM, "author", cJSON_CreateString(author->author)); cJSON_AddItemToObject(SM, "quote", cJSON_CreateString(author->quote)); cJSON_AddItemToObject(SM, "time", cJSON_CreateString(author->time)); char * jsonSM = cJSON_Print(SM); system("pause"); lib_init(); socket_t * server = socket_new(); socket_bind(server, 5000); socket_listen(server); char buf[10000]; socket_t * client = NULL; while(1) { client = socket_accept(server); socket_read(client, buf, sizeof(buf)); printf("%s",buf); if (strlen(buf) != 0){ http_request_t rs; rs = http_request_parse(buf); if (strcmp(rs.method,"GET") == 0 && strcmp(rs.uri, "/info") == 0 ) { server_info(client); } if (strcmp(rs.method,"GET") == 0 && strcmp(rs.uri, "/external") == 0 ) { server_sent(client,jsonSM); } } } system("pause"); return 0; }
void startServer(list_t *list) { lib_init(); socket_t * serverSocket = socket_new(); socket_bind(serverSocket, 5000); socket_listen(serverSocket); while(1) { puts("Waiting for connections"); socket_t * clientSocket = socket_accept(serverSocket); char buff[BUFFER_LENGTH]; int readLength = socket_read(clientSocket, buff, BUFFER_LENGTH); if(readLength == 0) { socket_close(clientSocket); socket_free(clientSocket); puts("Skipping empty request"); continue; } printf("Got Request:\n---------------\n%s\n----------------\n", buff); http_request_t req = http_parse(buff); printf("Method: %s\nURI: %s\n", req.method, req.uri); puts("Data:"); for(int i = 0; i < req.formLength; i++) { char * kvStr = keyvalue_toString(&req.form[i]); printf("\t%s\n", kvStr); free(kvStr); } if(!strcmp(req.uri, "/")) { char smallMSG[100]; sprintf(smallMSG, "<h1>HELLO ALL!!!</h1>"); char result_msg[MSG_LENGTH]; //resp_form(HTML, smallMSG, 200, result_msg); sprintf(result_msg, "HTTP/1.1 200 OK\n" "Content-length: %zu\n" "Content-type: application/xml\n" "\n" "%s\0", strlen(smallMSG), smallMSG); socket_write_string(clientSocket, result_msg); } else if(!strcmp(req.uri, "/info")) { char result_msg[MSG_LENGTH]; resp_form(XML, first_task_func(), 200, result_msg); socket_write_string(clientSocket, result_msg); } else if (!strcmp(req.uri, "/external")) { static const char * requestFormat = "%s %s HTTP/1.1\r\n" "Content-Type: text\r\n" "Content-Length: %zu\r\n\r\n" "%s"; socket_t* serverSock = socket_new (); socket_connect(serverSock, "216.58.209.49", 80); char uri [256]; strcpy (uri, "http://pb-homework.appspot.com/test/var/5?format=xml"); char req [1024]; sprintf (req, requestFormat, "GET", uri, NULL, NULL, NULL); socket_write(serverSock, req, strlen (req)); char responce [1024]; socket_read (serverSock, responce, 1024); int contentLength = 0; sscanf (strstr(responce, "Content-Length: ") + strlen ("Content-Length: "), "%d", &contentLength); char* data = strstr (responce, "\r\n\r\n") + 4; socket_free(serverSock); data = second_task_func(data); char result_msg[MSG_LENGTH]; resp_form(XML, data, 200, result_msg); socket_write_string(clientSocket, result_msg); } else { char result_msg[MSG_LENGTH]; resp_form(XML, NULL, 404, result_msg); socket_write_string(clientSocket, result_msg); return; } socket_close(clientSocket); socket_free(clientSocket); } socket_close(serverSocket); socket_free(serverSocket); lib_free(); }