Code:
#include "../hdr/isamhdr.h"
#include <mcheck.h>
int main(int argc, char *argv[])
{
struct addrinfo hints, *res, *p;
struct sockaddr *accepted_conc;
int status, socket_fd,accept_fd,sockaddr_size=sizeof(accepted_conc),curr_conc=0,max_conc;
pthread_t *threads;
if(argc != 3)
{
printf("ITF_ERR: Usage %s <port numebr> <max_connections>\n",argv[0]);
return 2;
}
max_conc=atoi(argv[2]);
threads=calloc(max_conc,sizeof(pthread_t));
accepted_conc=calloc(max_conc,sizeof(struct sockaddr));
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; // open tcp connection
hints.ai_flags = AI_PASSIVE; // server always runs on localhost
if ((status = getaddrinfo(NULL,argv[1], &hints, &res)) != 0)
{
printf("ITF_ERR: getaddrinfo():%s\n", gai_strerror(status));
return 2;
}
// socket()
if((socket_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) <0 )
{
printf("ITF_ERR: socket() %s\n",strerror(errno));
return 2;
}
else
printf("socket() call sucess\n");
// bind()
if((status = bind(socket_fd,res->ai_addr,res->ai_addrlen)) < 0)
{
printf("ITF_ERR: bind() %s\n",strerror(errno));
return 2;
}
else
printf("bind() call sucess\n");
// listen()
if((status = listen(socket_fd,10)) < 0)
{
printf("ITF_ERR: listen() %s\n",strerror(errno));
return 2;
}
else
printf("listen() call success\n");
while(curr_conc < max_conc)
{
printf("waiting for new connections ...\n");
// accept()
if((accept_fd = accept(socket_fd,accepted_conc+curr_conc,&sockaddr_size)) <0 )
{
printf("ITF_ERR: accept() %s\n",strerror(errno));
return 2;
}
else
{
curr_conc++;
printf("%d -accept() call success\n",curr_conc);
}
status = pthread_create(&threads[curr_conc], NULL,_server_thread, (void *)accept_fd);
if(status)
{
printf("ITF_ERR; return code from pthread_create() is %d\n", status);
return 2;
}
}
close(socket_fd);
printf("no room for further threads\n");
return 0;
}
void *_server_thread(void *sock_fd)
{
void *lib_hdl;
USR_qry_ptr qry;
USR_rslt_set_ptr rslt;
unsigned char qry_str[LINE_MAX];
int fd=(int)sock_fd,sts;
if((lib_hdl=dlopen(ISAM_LIB,RTLD_NOW)) == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
dl_qry_parser=dlsym(lib_hdl,"qry_parser");
if(dl_qry_parser == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
dl_execute_qry=dlsym(lib_hdl,"execute_qry");
if(dl_execute_qry == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
dl_print_rslt_set=dlsym(lib_hdl,"_print_rslt_set");
if(dl_print_rslt_set == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
dl_free_rslt_set=dlsym(lib_hdl,"_free_rslt_set");
if(dl_free_rslt_set == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
dl_free_qry=dlsym(lib_hdl,"_free_qry");
if(dl_free_qry == NULL)
{
printf("%s\n",dlerror());
return NULL;
}
while(1)
{
sts=recv(fd,qry_str,LINE_MAX,0);
if(sts < 0)
{
printf("SRV_ERR: recv() %s\n",strerror(errno));
close(fd);
pthread_exit(NULL);
}
if(!strcmp(qry_str,"quit"))
{
printf("SRV_ERR: Thread exiting by quit\n");
close(fd);
pthread_exit(NULL);
}
qry=(*dl_qry_parser)((unsigned char *)qry_str);
rslt=(*dl_execute_qry)(qry);
(*dl_print_rslt_set)(rslt);
// do clean up
(*dl_free_rslt_set)(rslt);
(*dl_free_qry)(qry);
}
return NULL;
}