-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
44 lines (34 loc) · 1.28 KB
/
main.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
#include <stdio.h>// printf()
#include <stdlib.h>// exit()
#include <pthread.h>
#include "receiver.h"
#include "sender.h"
//Note: printerthread encapsulated inside receiver.h
//Note: keyboardthread encapsulated inside sender.h
static pthread_mutex_t listAddOrRemoveMutex;
static pthread_mutex_t mainMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t EndCondVar;
int main(int argc,char** args){
if(argc != 4){
printf("usage: s-talk <localPort> <remoteIP> <remotePort>\n");
exit(1);
}
const char* localPort = args[1];
const char* remoteIp = args[2];
const char* remotePort = args[3];
printf("localPort: %s ip: %s remotePort: %s\n",localPort,remoteIp,remotePort);
pthread_mutex_init(&listAddOrRemoveMutex,NULL);
pthread_cond_init(&EndCondVar,NULL);
Sender_init(remoteIp,remotePort,&EndCondVar,&listAddOrRemoveMutex,&mainMutex);
Receiver_init(localPort,&listAddOrRemoveMutex,&EndCondVar,&mainMutex);
pthread_mutex_lock(&mainMutex);
pthread_cond_wait(&EndCondVar,&mainMutex);
pthread_mutex_unlock(&mainMutex);
Receiver_shutDown();
Sender_shutDown();
pthread_mutex_destroy(&mainMutex);
pthread_mutex_destroy(&listAddOrRemoveMutex);
pthread_cond_destroy(&EndCondVar);
printf("program is done :)");
return 0;
}