-
Notifications
You must be signed in to change notification settings - Fork 3
/
rds.c
182 lines (168 loc) · 4 KB
/
rds.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include "rds.h"
#include "p2p.h"
static struct Entries rds[3];
pthread_mutex_t *rds_mutex;
/* Main RDS loop
* If the entry has passed its life span
* remove it and print a timeout message.
*/
void * RdsMain(void *args)
{
int retval, i;
time_t current;
struct timespec sleep, wait;
sleep.tv_sec = 0;
sleep.tv_nsec = 0x1dcd6500; // Half second
while (1) {
time(¤t);
retval = pthread_mutex_lock (rds_mutex);
if (retval != 0) {
pthread_mutex_unlock(rds_mutex);
return;
}
for (i = 0; i < 3; i++) {
if (((current - rds[i].timestamp) > 5) && (rds[i].used)) {
printf("File %s timed out\n", rds[i].filename);
rds[i].used = 0;
}
}
retval = pthread_mutex_unlock(rds_mutex);
if (retval != 0)
return;
nanosleep(&sleep, &wait);
}
}
/* Initialize the mutex
* Returns OK if the mutex was created correctly
* Otherwise it returns ERROR
*/
int RdsInit()
{
int retval, i;
rds_mutex = malloc(sizeof(pthread_mutex_t));
if (rds_mutex == NULL)
return ERROR;
retval = pthread_mutex_init (rds_mutex, NULL);
if (retval != 0)
return ERROR;
for (i = 0; i < 3; i++) {
rds[i].used = 0;
}
return OK;
}
/* Free the resources allocated to RDS
* Return OK if everything went well
* or ERROR if something went wrong
*/
int RdsDestroy()
{
int retval;
pthread_mutex_destroy(rds_mutex);
if (retval != 0)
return ERROR;
return OK;
}
/* Add a file to the queue
* Returns OK if everything went ok
* Error if it did not.
*/
int RdsAdd(char filename[])
{
int retval, i, oldest;
time_t oldtime;
retval = pthread_mutex_lock (rds_mutex);
if (retval != 0) {
printf("Mutex Lock\n");
pthread_mutex_unlock(rds_mutex);
return ERROR;
}
// If the rds is not used then use it
// and return OK
for (i = 0; i < 3; i++) {
if (!rds[i].used) {
rds[i].used = 1;
strcpy(rds[i].filename, filename);
time(&oldtime);
rds[i].timestamp = oldtime;
pthread_mutex_unlock(rds_mutex);
return OK;
}
else if (strcmp(filename, rds[i].filename) == 0) {
pthread_mutex_unlock(rds_mutex);
return OK;
}
else {
time(&oldtime);
if (oldtime > rds[i].timestamp) {
oldest = i; oldtime = rds[i].timestamp;
}
}
}
// Otherwise find the oldest file and replace it
time(&oldtime);
printf("Replacing %s, with %s\n", rds[oldest].filename, filename);
strcpy(rds[oldest].filename, filename);
rds[oldest].timestamp = oldtime;
retval = pthread_mutex_unlock(rds_mutex);
if (retval != 0) {
printf("Error on Mutex unlock\n");
return ERROR;
}
return OK;
}
/* Removes a file from the queue
* Returns OK if the file was removed succesfully
* Returns NOT_FOUND if the file was not in the queue
* Returns ERROR if something went wrong
*/
int RdsRemove(char filename[])
{
int retval, i;
retval = pthread_mutex_lock (rds_mutex);
if (retval != 0) {
pthread_mutex_unlock(rds_mutex);
return ERROR;
}
// Loop through the rds and if the filename
// matches remove it. Then return OK.
for (i = 0; i < 3; i++) {
if (strcmp(rds[i].filename, filename) == 0) {
rds[i].used = 0;
strcpy(rds[i].filename, "");
pthread_mutex_unlock(rds_mutex);
return OK;
}
}
retval = pthread_mutex_unlock(rds_mutex);
if (retval != 0)
return ERROR;
// The file was not found in the RDS
return NOT_FOUND;
}
int RdsSearch(char *filename)
{
int retval, i;
retval = pthread_mutex_lock (rds_mutex);
if (retval != 0) {
pthread_mutex_unlock(rds_mutex);
return ERROR;
}
// Loop through the rds and if the filename
// matches then return OK.
for (i = 0; i < 3; i++) {
if (strcmp(rds[i].filename, filename) == 0) {
pthread_mutex_unlock(rds_mutex);
return OK;
}
}
retval = pthread_mutex_unlock(rds_mutex);
if (retval != 0)
return ERROR;
// The file was not found in the RDS
return NOT_FOUND;
}