-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
73 lines (66 loc) · 1.58 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "p2p.h"
#include "rds.h"
#include "sender.h"
#include "list.h"
#include "directory.h"
#include "receiver.h"
int sock;
int main(int argc, char *argv[])
{
int len, i;
int port, count = 2;
char *result = NULL;
char address[16];
struct sockaddr_in peer;
pthread_t *rdsThread = malloc(sizeof(pthread_t));
pthread_t *listingThread = malloc(sizeof(pthread_t));
pthread_t *receiverThread = malloc(sizeof(pthread_t));
if (argc < 2) {
printf("Usage: portnumber [ip/port]\n");
exit(0);
}
ListInit();
DirectoryInit();
CacheInit();
port = atoi(argv[1]);
while ((count < argc) && (count < 20)) {
result = strchr(argv[count], '/');
if (result == NULL) {
ListAdd(argv[count]);
}
else {
memset(&peer, 0, sizeof(struct sockaddr_in));
peer.sin_family = AF_INET;
peer.sin_port = htons(atoi(result+1));
len = strlen(argv[count]) - strlen(result);
strncpy(address, argv[count], len);
address[len] = '\0';
if (inet_aton(address, &peer.sin_addr) == 0)
return -1;
DirectoryAdd(NULL, peer);
}
count++;
}
// Start the application threads
RdsInit();
ReceiverInit(port);
pthread_create(rdsThread, NULL, RdsMain, NULL);
pthread_create(listingThread, NULL, SendListing, NULL);
pthread_create(receiverThread, NULL, Receiver, (void *)&port);
ClientMain();
exit(1);
}
int ClientMain()
{
char buf[20];
while(1) {
scanf("%s",buf);
SendRequest(buf);
}
}