This repository was archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadInput.c
More file actions
92 lines (75 loc) · 2.35 KB
/
Copy pathreadInput.c
File metadata and controls
92 lines (75 loc) · 2.35 KB
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
// reads off keyboard and adds to list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "list.h"
#include "queueOperations.h"
#include "readInput.h"
#include "sendUDP.h"
#include "threadCanceller.h"
// Max size of the message, using theoretical max length for a UDP packet of 65507 - 1 byte for null terminator
#define MAXBUFLEN 65506
static List* list;
static pthread_t readerThread;
static void* readLoop(void* useless){
while(1)
{
// Declaring variables
char* message;
char bufStorageOfMessage[MAXBUFLEN];
int numbytes;
int iteration = 0;
do
{
iteration++;
// Emptying input string buffer
memset(&bufStorageOfMessage, 0, MAXBUFLEN);
// Reading user input
numbytes = read(0,bufStorageOfMessage, MAXBUFLEN);
if(numbytes==-1)
{
perror("reader: read() failed");
exit(-1);
}
// Downsizing the size of the message to be more space efficient
message = (char*)malloc(sizeof(char)*(numbytes+1));
strncpy(message, bufStorageOfMessage, numbytes);
message[numbytes] = '\0';
// Adding the message to the list
int enqVal = enqueueMessage(list, message);
if(enqVal==-1)
{
fprintf(stderr,"reader: enqueue error, queue full. Message too long to transmit\n");
}
// Checking for exit code
// Ends the process if exit code was in the first iteration of read
if (!strcmp(message,"!\n") && iteration==1)
{
senderSignaller();
cancelReceiverWriter();
return NULL;
}
} while (bufStorageOfMessage[numbytes-1]!='\n' && iteration != 100);
//send signal for the senderUDP
senderSignaller();
}
return NULL;
}
void readerInit(List* l){
list = l;
int readingThread = pthread_create(&readerThread, NULL, readLoop, NULL);
if(readingThread !=0){//if gave error code of not 0 (0 is success)
perror("reader: thread creation error");
exit(-1);
}
}
void readerCancel()
{
pthread_cancel(readerThread);
}
void readerShutdown()
{
pthread_join(readerThread,NULL);
}