-
Notifications
You must be signed in to change notification settings - Fork 3
/
directory.c
124 lines (112 loc) · 2.82 KB
/
directory.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
115
116
117
118
119
120
121
122
123
124
#include <time.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "directory.h"
static struct Directory directory[ENTRIES];
static pthread_mutex_t directoryMutex;
/* Setup the directory to an empty state.
* Make sure that the entries are marked as unused.
* Also sets up the mutex.
*/
void DirectoryInit()
{
int i;
pthread_mutex_init(&directoryMutex, NULL);
for (i = 0; i < ENTRIES; i++) {
directory[i].used = 0;
}
}
/* Add a file/peer pair to the directory.
* Will find either an empty place in the directory
* find the oldest element to replace, or return if two
* elemnts have the same name and peer.
*/
void DirectoryAdd(char *file, struct sockaddr_in address)
{
int i, old;
time_t current;
char dest[INET_ADDRSTRLEN];
pthread_mutex_lock(&directoryMutex);
time(¤t);
// Find where to place the info
for (i = 0; i < ENTRIES; i++) {
if (directory[i].used == 0) {
old = i;
break;
}
else if (file != NULL && directory[i].file != NULL) {
if (strcmp(file, directory[i].file) == 0) {
if ((directory[i].peer.sin_port == address.sin_port)
&& directory[i].peer.sin_addr.s_addr == address.sin_addr.s_addr) {
directory[i].timestamp = current;
pthread_mutex_unlock(&directoryMutex);
return;
}
}
}
else {
if (directory[i].timestamp < current) {
current = directory[i].timestamp;
old = i;
}
}
}
// Copy information into the list.
if (file != NULL) {
strcpy(directory[old].file, file);
}
memcpy(&directory[old].peer, &address, sizeof(struct sockaddr_in));
time(&directory[old].timestamp);
directory[i].used = 1;
pthread_mutex_unlock(&directoryMutex);
}
/* Return a copy of the directory.
* This way we do not need to worry about
* thread safety.
*/
struct Directory * DirectoryGet()
{
return directory;
}
int DirectoryGetList(struct sockaddr_in *addresses)
{
int i, j, count = 0;
char unique;
struct sockaddr_in current;
pthread_mutex_lock(&directoryMutex);
for (i = 0; i < ENTRIES; i++) {
unique = 1;
if (directory[i].used == 1) {
current = directory[i].peer;
}
else
break;
memcpy(&addresses[count], ¤t,
sizeof(struct sockaddr_in));
count++;
}
pthread_mutex_unlock(&directoryMutex);
return count;
}
int DirectoryGetListBySearch(char *search, struct sockaddr_in *addresses)
{
int i, j, count = 0;
struct sockaddr_in current;
pthread_mutex_lock(&directoryMutex);
for (i = 0; i < ENTRIES; i++) {
if (directory[i].used == 1
&& strcmp(search, directory[i].file) == 0) {
current = directory[i].peer;
}
else
continue;
memcpy(&addresses[count], ¤t,
sizeof(struct sockaddr_in));
count++;
}
pthread_mutex_unlock(&directoryMutex);
return count;
}