int srv_server_read(t_srv_data *srv_data, t_srv_socket *socket) { int socket_id; print_info("Accepting new client... "); if (srv_data->connections_nb == srv_data->slots_nb) { print_warning("Server is full\n"); return (accept_new_client(NULL, socket->fd)); } if ((socket_id = accept_new_client(srv_data->sockets, socket->fd)) == -1) return (EXIT_FAILURE); print_info("OK\n"); print_info("New client connected.\n"); print_info("Server slots: %d / %d\n", ++srv_data->connections_nb, srv_data->slots_nb); return (socket_id - srv_data->ports_nb + 1); }
int vm_main() { int ret; ret = start_server(); if (ret < 0) return 0; for (;;) { int socket_fd = accept_new_client(); if (socket_fd < 0) return -1; vm_run(socket_fd); } stop_server(); return 0; }
/* * RUN() * run() to start the epoll_server * param time_out is to wait running epoll_wait once * determine what the m_epoll_events[i].data.fd equals * 1. if it equals listen sock fd, then do accept_new_client() * 2. if it equals client sock fd, and the property is EPOLLIN, * then recv_data() from client * 3. if it equals client sock fd, and the property is EPOLLOUT, * then send_data() to client */ void epoll_server::run(int time_out) { while (1) { int ret = epoll_server_wait(time_out); if (ret == 0) { continue; } else if (ret == -1) { cout << "epoll wait error: " << strerror(errno) << "(errno: " << errno << ")" << endl; continue; } else { for (int i = 0; i < ret; ++i) { if (m_epoll_events[i].data.fd == m_listen_sock) { accept_new_client(); } else { handle_msg(m_epoll_events[i].data.fd); } } } } }
int main(){ fd_set target_fds; fd_set org_target_fds; int sock_optval = 1; int port = 5000; /* リスニングソケットを作成 */ listening_socket = socket(AF_INET, SOCK_STREAM, 0); /* ソケットオプション設定 */ if ( setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR, &sock_optval, sizeof(sock_optval)) == -1 ){ perror("setsockopt"); exit(1); } /* アドレスファミリ・ポート番号・IPアドレス設定 */ sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = htonl(INADDR_ANY); if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){ perror("bind"); exit(1); } if ( listen(listening_socket, SOMAXCONN) == -1 ){ perror("listen"); exit(1); } printf("ポート %d を見張ります。\n", port); /* 監視対象のディスクリプタ一覧をゼロクリア */ FD_ZERO(&org_target_fds); /* リスニングソケットを監視対象に追加 */ FD_SET(listening_socket, &org_target_fds); while (1){ int i; time_t now_time; struct timeval waitval; /* select に待ち時間を指定するための構造体 */ waitval.tv_sec = 2; /* 待ち時間に 2.500 秒を指定 */ waitval.tv_usec = 500; /* org_target_fds を target_fds にコピー */ memcpy(&target_fds, &org_target_fds, sizeof(org_target_fds)); select(FD_SETSIZE, &target_fds, NULL, NULL, &waitval); /* ソケットが読み出し可能か順にチェック */ for ( i=0 ; i<FD_SETSIZE ; i++ ){ if ( FD_ISSET(i, &target_fds) ){ printf("ディスクリプタ %d 番が読み込み可能です。\n", i); if ( i == listening_socket ){ int new_sock; /* 新しいクライアントがやってきた */ new_sock = accept_new_client(i); if ( new_sock != -1 ){ /* 監視対象に新たなソケットを追加 */ FD_SET(new_sock, &org_target_fds); } } else { int read_size; /* 接続済みソケットからデータが送信されてきた */ read_size = read_and_reply(i); if ( read_size == -1 || read_size == 0 ){ /* 切断したソケットを監視対象から削除 */ FD_CLR(i, &org_target_fds); } } } } time(&now_time); /* 現在時刻を取得 */ for ( i=0 ; i<FD_SETSIZE ; i++ ){ /* 監視対象でないソケットはスキップ */ if ( ! FD_ISSET(i, &org_target_fds) ) continue; /* リスニングソケットはスキップ */ if ( i == listening_socket ) continue; if ( now_time-10 > client_info[i].last_access ){ printf("%s (%s) ポート %d ディスクリプタ %d 番から10秒以上アクセスがありません。切断します。\n", client_info[i].hostname, client_info[i].ipaddr, client_info[i].port, i); close(i); /* 切断したソケットを監視対象から削除 */ FD_CLR(i, &org_target_fds); } } } close(listening_socket); return 0; }
static void check_internal_msg(void) { struct timeval timeout = { .tv_sec = 0, .tv_usec = EXAMSG_TIMEOUT }; static Examsg msg; command_end_t *end; int i, ret; ret = examsgWaitTimeout(cli_mh, &timeout); if (ret < 0 && ret != -ETIME) { exalog_error("Message wait failed %s (%d)", exa_error_msg(ret), ret); return; } if (ret == -ETIME) return; ret = examsgRecv(cli_mh, NULL, &msg, sizeof(msg)); if (ret == 0) return; EXA_ASSERT_VERBOSE(ret > 0, "Message receive failed: %s (%d)", exa_error_msg(ret), ret); if (ret < 0) exalog_error("Message receive failed: %s (%d)", exa_error_msg(ret), ret); /* The CLI server can only receive EXAMSG_ADM_CLUSTER_CMD_END messages for now */ EXA_ASSERT(msg.any.type == EXAMSG_ADM_CLUSTER_CMD_END); end = (command_end_t *)msg.payload; for (i = 0; i < MAX_CONNECTION; i++) if (end->cuid == connectlist[i].uid) { cli_command_end_complete(connectlist[i].fd, &end->err_desc); connectlist[i].uid = CMD_UID_INVALID; break; } EXA_ASSERT(i < MAX_CONNECTION); } static void check_tcp_connection(void) { static struct timeval timeout = { .tv_sec = 0, .tv_usec = 0 }; fd_set setSave = setSocks; int ret, conn_id; do ret = os_select(FD_SETSIZE, &setSave, NULL, NULL, &timeout); while (ret == -EINTR); if (ret < 0) { /* FIXME should assert ? */ exalog_debug("Select failed %m"); return; } /* Check working sockets */ for (conn_id = 0; conn_id < MAX_CONNECTION; ++conn_id) { int sock_fd = connectlist[conn_id].fd; if (sock_fd >= 0 && FD_ISSET(sock_fd, &setSave)) handle_inputdata(conn_id, sock_fd); } /* Must be done at the end to make sure messages for current * working threads are processed first */ if (FD_ISSET(listen_fd, &setSave)) accept_new_client(); } /*-------------------------------------------------------------------------*/ /** \brief Connection thread: wait on xml message and pass the command * to the work thread. * * \param[in] sock_xml: socket xml on which it receives commands. * */ /*-------------------------------------------------------------------------*/ static void cli_server(void *data) { int i; /* Initialize exalog */ exalog_as(EXAMSG_ADMIND_ID); exalog_debug("cli_server: started"); /* Initialization */ FD_ZERO(&setSocks); FD_SET(listen_fd, &setSocks); for (i = 0; i < MAX_CONNECTION; i++) { connectlist[i].fd = -1; /* A command cannot be CMD_UID_INVALID, so CMD_UID_INVALID means here * no command running */ connectlist[i].uid = CMD_UID_INVALID; } while (!stop) { check_tcp_connection(); check_internal_msg(); } os_closesocket(listen_fd); os_net_cleanup(); examsgDelMbox(cli_mh, EXAMSG_ADMIND_CLISERVER_ID); examsgExit(cli_mh); } int cli_server_start(void) { listen_fd = listen_socket_port(ADMIND_SOCKET_PORT); if (listen_fd < 0) return listen_fd; cli_mh = examsgInit(EXAMSG_ADMIND_CLISERVER_ID); if (!cli_mh) return -EINVAL; /* The mailbox needs to be able to receive command end messages from the * event manager; as there can be at most MAX_CONNECTION client connections * we can receive at the time at most 10 command end messages. */ examsgAddMbox(cli_mh, EXAMSG_ADMIND_CLISERVER_ID, MAX_CONNECTION, sizeof(command_end_t)); stop = false; if (!exathread_create_named(&thr_xml_proto, ADMIND_THREAD_STACK_SIZE+MIN_THREAD_STACK_SIZE, &cli_server, NULL, "exa_adm_xml")) return -EXA_ERR_DEFAULT; return EXA_SUCCESS; }
int main(int argc,char ** argv){ int sockfd; //socket number for Computing Nodes (CN) int sockfd_balancer; //socket number for Load Balancer (LB) int port; //port number for CN int port_balancer; //port number for LB int index; //int sock_dgram; pthread_attr_t pthread_custom_attr; pthread_t tid; communication_timeout.tv_sec = COMMUNICATION_TIMEOUT; communication_timeout.tv_usec = 0; if (argc != 4) { /*** TODO: added argv[0] to avoid warning caused by %s ***/ printf("Usage: %s vm_port_number load_balancer_port_number ml_model_number\n", argv[0]); exit(1); } else if (atoi(argv[1]) == atoi(argv[2])) { printf("Port numbers must be different! Both the ports have the same port number %d\n", atoi(argv[1])); exit(1); } port = atoi(argv[1]); port_balancer = atoi(argv[2]); ml_model = atoi(argv[3]); //Allocating initial memory for the VMs /*** ATTENZIONE : Problema con l'allocazione della malloc, mi permette di scrivere in altre zone di memoria di almeno 4B ***/ for (index = 0; index < NUMBER_GROUPS; index++) { current_vms[index] = 0; // actually no VMs are connected yet vm_data_set[index] = malloc(sizeof(struct vm_data)*NUMBER_VMs); // allocate the same memory initially for each groups allocated_vms[index] = NUMBER_VMs; // number of slots allocated for each group rej_rate = (float *)malloc(sizeof(float)*NUMBER_GROUPS*NUMBER_VMs); } //Open the local connection start_server(&sockfd,port); //Open the connection with the load_balancer start_server(&sockfd_balancer,port_balancer); //start_server_dgram(&sock_dgram); //Init of broadcast and leader primitives initialize_broadcast(PATH); initialize_leader(PATH); //Start dedicated thread to communicate with LB //It must block until the system is not ready if((accept_load_balancer(sockfd_balancer,pthread_custom_attr)) < 0) exit(1); pthread_attr_init(&pthread_custom_attr); pthread_create(&tid,&pthread_custom_attr,mttf_thread,NULL); //Accept new clients while(1){ accept_new_client(sockfd,pthread_custom_attr); } return 0; }
int main(int argc, char *argv[]) { int s; int i, rv; /* getopt options */ static struct option long_options[] = { /* These options set a flag. */ {"master", no_argument, &master_flag, 1}, {"target", required_argument, 0, 't'}, {"port", required_argument, 0, 'p'}, {"lport", required_argument, 0, 'l'}, {"chunk", required_argument, 0, 'c'}, {"memory", required_argument, 0, 'm'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; int c; setlocale(LC_ALL, ""); while (1) { c = getopt_long(argc, argv, "p:t:l:c:1m:h", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: /* If this option set a flag, do nothing else now. */ if (long_options[option_index].flag != 0) break; printf("option %s", long_options[option_index].name); if (optarg) { printf(" with arg %s", optarg); } printf("\n"); break; case 'p': target_port = optarg; break; case 't': target_addr = optarg; break; case 'l': local_port = optarg; break; case 'c': sscanf(optarg, "%i", &chunk_size); break; case '1': master_flag = 1; break; case 'm': sscanf(optarg, "%i", &memory_size); break; case 'h': puts("--port | -p <target_port>\n" "--target | -t <target_address>\n" "--lport | -l <local_port>\n" "--chunk | -c <chunk_size>\n" "--memory | -m <memory_size> (number of chunks)\n" "--master | -1 start as a master\n"); return 0; } } /* check if user supplied all needed infos */ if ((master_flag == 0) && ((target_addr == NULL) || (target_port == NULL))) { fprintf(stderr, "You have to specify target address and port\n"); return 1; } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; //AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; hints.ai_protocol = IPPROTO_TCP; s = getaddrinfo(NULL, local_port, &hints, &result); if (s != 0) { fprintf(stderr, "local getaddrinfo: %s\n", gai_strerror(s)); exit(EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully bind(2). If socket(2) (or bind(2)) fails, we (close the socket and) try the next address. */ for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0) { gethostname(address_book[addbookidx].hostname, sizeof(address_book[0].hostname)); short *pport = (short *)rp->ai_addr->sa_data; address_book[addbookidx].port = ntohs(((struct sockaddr_in *)rp->ai_addr)-> sin_port); fprintf(stderr, "I am: %s:%i \n", address_book[addbookidx].hostname, address_book[addbookidx].port); addbookidx++; break; /* Success */ } close(sfd); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { /* No address succeeded */ fprintf(stderr, "Could not bind\n"); close(sfd); exit(EXIT_FAILURE); } if (listen(sfd, MAX_CLIENTS_NO) != 0) { close(sfd); fprintf(stderr, "Cannot listen\n"); return 2; } /* if I am a master - initial node, enter mainloop * otherwise connect to node structure */ srandom(time(NULL)); if (master_flag == 0) { /* connect to structure of node (I am just a client) */ strncpy(address_book[addbookidx].hostname, target_addr, sizeof(address_book[0].hostname)); sscanf(target_port, "%i", &address_book[addbookidx++].port); int fd = connect_2_master(target_addr, target_port); if (fd != -1) { insert_fd_to_addrbook(fd, target_addr, target_port); /* send own address and port to structure */ send_host_list(targetlist[0].sd); /* send to target */ } else { is_terminated = 1; } } else { /* allocate given chunk of memory */ allocate_shared_mem(); /* wait for the first client */ accept_new_client(); } fprintf(stderr, "before mainloop:\n" "\tsfd: %i\n\tmaster_flag: %i\n", sfd, master_flag); print_shared_memory(); signal(SIGINT, handle_signal); while (is_terminated == 0) { /* infinite main loop */ FD_ZERO(&fdclientset); FD_SET(sfd, &fdclientset); int maxfd = sfd + 1; /* my clients */ if (clientcount > 0) { for (i = (clientcount - 1); i >= 0; --i) { if (clientlist[i].sd > 0) { if (clientlist[i].sd >= maxfd) { maxfd = clientlist[i].sd + 1; } FD_SET(clientlist[i].sd, &fdclientset); } } } /* my targets */ if (targetcount > 0) { for (i = (targetcount - 1); i >= 0; --i) { if (targetlist[i].sd > 0) { if (targetlist[i].sd >= maxfd) { maxfd = targetlist[i].sd + 1; } FD_SET(targetlist[i].sd, &fdclientset); } } } if (select(maxfd, &fdclientset, NULL, NULL, &timeout) == -1) { rv == errno; fprintf(stderr, "Error - select() failed maxfd\n"); } /* test all fd's after select */ if (FD_ISSET(sfd, &fdclientset)) { accept_new_client(); } /* my clients */ if (clientcount > 0) { for (i = (clientcount - 1); i >= 0; --i) { /* iterate over clients and wait for message */ if (FD_ISSET(clientlist[i].sd, &fdclientset)) { rv = recv(clientlist[i].sd, buf, BUF_SIZE - 1, 0); buf[rv] = 0; switch (rv) { case 0: /* shutdown */ case -1: /* error */ if (rv == 0) { fprintf(stderr, "Got goodbye from %i\n", clientlist [i].sd); } else { rv = errno; fprintf(stderr, "Error - recv() client %d\n", clientlist[i]. sd); } close_remove_id(clientlist, &clientcount, i); break; default: handle_message(clientlist[i].sd, buf, rv); } } } } /* my targets */ if (targetcount > 0) { for (i = (targetcount - 1); i >= 0; --i) { /* iterate over targets and wait for message */ if (FD_ISSET(targetlist[i].sd, &fdclientset)) { rv = recv(targetlist[i].sd, buf, BUF_SIZE - 1, 0); buf[rv] = 0; switch (rv) { case 0: /* shutdown */ case -1: /* error */ if (rv == -1) { rv = errno; fprintf(stderr, "Error - recv() target %d\n", targetlist [i].sd); } else { fprintf(stderr, "Got goodbye from target %i\n", targetlist[i]. sd); } int disc_node = targetlist[i].sd; close_remove_id(targetlist, &targetcount, i); remove_fd_from_addrbook (disc_node); break; default: handle_message(targetlist[i].sd, buf, rv); } } } } if ((targetcount > 0) || (clientcount > 0)) { if (((double)random() / RAND_MAX) >= 0.3) { /* write */ handle_send(); } else { /* read */ } } sleep(0.7); } print_shared_memory(); free(shared_memory); free(timestamps); for (i = 0; i < clientcount; ++i) { shutdown(clientlist[i].sd, SHUT_RDWR); close(clientlist[i].sd); } for (i = 0; i < targetcount; ++i) { shutdown(targetlist[i].sd, SHUT_RDWR); close(targetlist[i].sd); } shutdown(sfd, SHUT_RDWR); close(sfd); puts("Died..."); return 0; }
int main(int argc, const char *argv[]) { if (argc != 2) { printf("Zla liczba argumentow! Podaj tylko jeden!\n"); return -1; } time_t t; srand((unsigned) time(&t)); unsigned short int port = ((rand() % 500) + 8000); printf("%d\n", port); int CNT = atoi(argv[1]); struct sockaddr_in srv_addr; struct epoll_event e, es[CNT]; //e dany event es[] tablica eventow do obslugi epoll-wielu klientow memset(&srv_addr, 0, sizeof(srv_addr)); // memset zeruje nam te strukturki memset(&e, 0, sizeof(e)); srv_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (srv_fd < 0) { printf("Cannot create socket\n"); return 1; } srv_addr.sin_family = AF_INET; srv_addr.sin_addr.s_addr = htonl(INADDR_ANY); srv_addr.sin_port = htons(port); if (bind(srv_fd, (struct sockaddr*) &srv_addr, sizeof(srv_addr)) < 0) { printf("Cannot bind socket\n"); close(srv_fd); return 1; } if (listen(srv_fd, 1) < 0) { printf("Cannot listen\n"); close(srv_fd); return 1; } epoll_fd = epoll_create(2); if (epoll_fd < 0) { printf("Cannot create epoll\n"); close(srv_fd); return 1; } e.events = EPOLLIN; e.data.fd = srv_fd; if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, srv_fd, &e) < 0) { printf("Cannot add server socket to epoll\n"); close(epoll_fd); close(srv_fd); return 1; } size_t cap = CNT; user_list* lista = create_ul(cap); int i = 0; for (;;) { i = epoll_wait(epoll_fd, es, CNT, -1); if (i < 0) { printf("Cannot wait for events\n"); close(epoll_fd); close(srv_fd); return 1; } for (--i; i > -1; --i) { if (es[i].data.fd == srv_fd) { //pobiera nam deskryptor i jesli to bedzie deskryptor serwera, to wchodzi w if'a if (accept_new_client() != 0) return 1; //shut down server } else { serve_client(es[i].data.fd, es[i].events, lista); } } } return 0; }