-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.c
116 lines (100 loc) · 3.79 KB
/
receiver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <pthread.h>
#include <string.h>//strncpy
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "list.h"
#include "receiver.h"
#include "arpa/inet.h"
#define MSG_MAX_LEN 100
#define BUFFERSIZE 100
//static pthread_t thread;
static int rxPort;
static char* msgToPrint;
static int socketFD;
static socklen_t serverAddressSize;
static socklen_t clientAddressSize;
static struct sockaddr_in serverAddr;
static struct sockaddr_in clientAddr;
static List* pReceiverToPrinterBuffer;
static pthread_t threadRecieverID;
static pthread_t threadPrinterID;
static pthread_mutex_t rxtoptMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t received = PTHREAD_COND_INITIALIZER;
//static pthread_cond_t printed = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t* pListAddRemoveMutex;
static pthread_cond_t* pEndCond;
static pthread_mutex_t* pMainMutex;
void *printerThread(void*){
while(1){
pthread_mutex_lock(&rxtoptMutex);
if(List_count(pReceiverToPrinterBuffer) == 0){
pthread_cond_wait(&received,&rxtoptMutex);
}
pthread_mutex_lock(pListAddRemoveMutex);
char* messageToPrint = (char*)List_trim(pReceiverToPrinterBuffer);
pthread_mutex_unlock(pListAddRemoveMutex);
printf("%s",messageToPrint);
free(messageToPrint);
pthread_mutex_unlock(&rxtoptMutex);
}
printf("Done printer thread");
return NULL;
}
void *receiverThread(void*){
printf("Receive thread active\n");
while (1){
pthread_mutex_lock(&rxtoptMutex);
char messageRx[MSG_MAX_LEN]; //buffer to store Message Recieved
recvfrom(socketFD,messageRx,MSG_MAX_LEN,0,
(struct sockaddr *) (struct sockaddr *)&clientAddr,&clientAddressSize); //fills buffer with whatever data comes in on the network
clientAddressSize = sizeof(clientAddr);
if(strcmp(messageRx,"!\n")==0){
pthread_cond_signal(pEndCond);
}
pthread_mutex_lock(pListAddRemoveMutex);
char* messageToAdd = malloc(100);
strcpy(messageToAdd,messageRx);
List_prepend(pReceiverToPrinterBuffer,messageToAdd);
messageToAdd = NULL;
pthread_mutex_unlock(pListAddRemoveMutex);
pthread_cond_signal(&received);
pthread_mutex_unlock(&rxtoptMutex);
}
printf("Done rx thread!");
return NULL;
}
void Receiver_init(const char* localPort,pthread_mutex_t* pListAddRmMutex,pthread_cond_t* pEndCondition,pthread_mutex_t* pMMutex){
//Initialize variables
pListAddRemoveMutex = pListAddRmMutex;
pMainMutex = pMMutex;
pEndCond = pEndCondition;
pReceiverToPrinterBuffer = List_create();
rxPort = atoi(localPort);
msgToPrint = (char*)malloc(500);
socketFD = socket(AF_INET,SOCK_DGRAM,0);
//Initialize Server address
memset(&serverAddr,0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET; //Connection may be from network
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(rxPort); //Host to Network short
serverAddressSize = sizeof(serverAddr);
//Bind server address to localPort
bind(socketFD,(struct sockaddr*) &serverAddr,serverAddressSize);
printf("Recieving From Port#%d\n",rxPort);
//Initialize Client address
memset(&clientAddr,0, sizeof(clientAddr));
clientAddressSize = sizeof(clientAddr);
pthread_create(&threadRecieverID,NULL,receiverThread,NULL);
pthread_create(&threadPrinterID,NULL,printerThread,NULL);
}
void Receiver_shutDown(void){
printf("Receiver is off");
pthread_join(threadRecieverID,NULL);
pthread_join(threadPrinterID,NULL);
free(msgToPrint);
close(socketFD);
}