void * dispatch(void * arg) { int fd; char filename[1024]; while(1) { if((fd = accept_connection()) < 0) { perror("accept_connection failed"); continue; } if(pthread_mutex_lock(&queue_access) !=0){ printf ("Queue access lock failed in disatch\n"); } if(get_request(fd, filename) != 0) { if(pthread_mutex_unlock(&queue_access) != 0){ printf ("Queue access unlock failed in dispatch\n"); } continue; } if(pthread_mutex_unlock(&queue_access) != 0){ printf ("Queue access unlock failed in dispatch\n"); } request_queue_t req; req.m_socket = fd; strcpy(req.m_szRequest, filename); insert_request(req); } return NULL; }
//////////////////////////////////////////////////////////////////////////////// // Listener Service //////////////////////////////////////////////////////////////////////////////// void listener_service(void) { enum codes code = OK; int sockfd; struct sockaddr_in self; char buffer[MAXBUF]; setlogmask (LOG_UPTO (LOG_INFO | LOG_ERR)); syslog (LOG_INFO, "Starting Listener Service... pid:[%d]", getpid()); // Create streaming socket if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { syslog (LOG_ERR, "[Listener Service]: Error in create Socket..."); exit(errno); } // Initialize address/port structure bzero(&self, sizeof(self)); self.sin_family = AF_INET; self.sin_port = htons(MY_PORT); self.sin_addr.s_addr = INADDR_ANY; // Assign a port number to the socket if ( bind(sockfd, (struct sockaddr*)&self, sizeof(self)) != 0 ) { syslog (LOG_ERR, "[Listener Service]: Error in Socket-bind..."); exit(errno); } // Make it a listening socket if ( listen(sockfd, 20) != 0 ) { syslog (LOG_ERR, "[Listener Service]: Error in Socket-listen..."); exit(errno); } syslog (LOG_INFO, "[Listener Service]: Started..."); //Forever while (1) { int clientfd; struct sockaddr_in client_addr; int addrlen=sizeof(client_addr); // accept a connection (creating a data pipe) clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &addrlen); syslog (LOG_INFO, "[Listener Service]: Socket %s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); recv(clientfd, buffer, MAXBUF, 0); syslog (LOG_INFO, "[Listener Service]: Data receive [%s]\n", buffer); char **partes= calloc(10,sizeof(char *)); size_t tamano= strsplit(buffer,partes,"|"); //Divide hasta un | //Validate message structure //Insert into sqlite int customer_id = atoi(partes[0]); char *transaction_id = partes[1]; char *card_number = partes[2]; int amount = atoi(partes[3]); if ( insert_request(customer_id, transaction_id, card_number, amount) == 0 ) code = OK; //Back response 202 (Received) else code = INTERNAL_SERVER_ERROR; //Back response 500 // (Internal server error) char *str_code = get_response_code(code); send(clientfd, str_code, strlen(str_code), 0); //Close data connection close(clientfd); } // Clean up (should never get here!) close(sockfd); }