int main() { int fd; if(( fd = open(FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC)) == -1 ) { perror("Failed to open input file"); return 1; } close(fd); int i=1; int childpid; printf("%s\n", "enes" ); for(i=1; i < 259; i++) { if ((childpid = fork()) == 0 && (i < 255) ) { char des[LAN_IP_SIZE]; snprintf(des, LAN_IP_SIZE, "%s%d",LAN_IP,i); const char *destination = des; discover(destination, PORT0); break; } else if(childpid == -1) { perror("FORK NOT OPERATED"); exit(1); } if((childpid == 0) && (i == 255) ) listenRequest(); else if((childpid == 0) && (i == 256)) listenResponce(); else if((childpid == 0) && (i == 257)) listenMessage(); else if((childpid == 0) && (i == 258)) responceMessage(); } while(childpid = wait(NULL) ) if ((childpid == -1) && (errno != EINTR)) break; return 0; }
void clientReciveMails(Message* msg, int id){ sendData(msg->referer, fillMessageData("mail", "continue", ""), sizeof(Message)); mail* info = (mail*)listenMessage(msg->referer, sizeof(mail)); if(info != NULL){ pthread_mutex_lock(&mut); if(findUserByUsername(info->to) != NULL){ pushMail(info); sendData(msg->referer, fillMessageData("mail", "success", "Mail Sent Correctly\n"), sizeof(Message)); pthread_cond_signal(&newMail); }else{ sendData(msg->referer, fillMessageData("mail", "error", "Wrong Username\n"), sizeof(Message)); } pthread_mutex_unlock(&mut); } }
void recieveEmails(int num){ int i, error; mail* msg; for(i=0; i<num; i++){ msg = listenMessage(0, sizeof(mail)); if(msg != NULL){ printf("%d: From: %s, Subject: %s Time: %s", i+1, msg->from, msg->header, ctime(&(msg->senttime))); printf("Message:%s\n", msg->body); if(msg->attachments[0] != '0'){ printf("Attachment: %s\n", msg->attachments); } } } free(msg); }
static void * clientConn(void* info){ Message* msg; int pid = ((Message*)info)->referer; int id = getClientNum(); addClientNode(pid, id); sendData(pid, fillMessageData("client", "success", ""), sizeof(Message)); while(1){ printf("Waiting for new Message\n"); Message* data = (Message*)listenMessage(pid, sizeof(Message)); if(data != NULL){ if(strcmp(data->resource, "mail") == 0){ if(strcmp(data->method, "send") == 0){ clientReciveMails(data, id); }else if(strcmp(data->method, "receive") == 0){ clientSendEmails(data, id); } }else if(strcmp(data->resource, "client") == 0){ if(strcmp(data->method, "close") == 0){ printf("%d\n", pid); closeConnection(pid); break; } }else{ pushWait(id,data); while((msg = grabMessage(pid)) != NULL){ sendData(pid, msg, sizeof(Message)); } } } } printf("Salio \n"); return NULL; }
void getResponce(int pid){ Message* msg = listenMessage(pid, sizeof(Message)); if(msg != NULL){ dispatchEvent(msg); } }