-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.h
228 lines (210 loc) · 7.01 KB
/
main.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <list>
#include <unistd.h>
#include <ctype.h>
#include <map>
#include <openssl/sha.h>
#include <stdlib.h>
#include <string.h>
#include <set>
#include <queue>
#define HEADER_SIZE 27
#define SHA_DIGEST_LENGTH 20
using namespace std ;
//keeps the join responses
extern set<struct joinResNode> joinResponse ;
//keeps the status messgae
extern set< set<struct node> > statusResponse ;
extern map<struct node, list<string> > statusResponseTypeFiles ;
struct node{
unsigned short int portNo;
unsigned char hostname[256];
bool operator<(const struct node& node1) const{
return node1.portNo < portNo ;
}
bool operator==(const struct node& node1) const{
return ((node1.portNo == portNo) && !strcmp((char *)node1.hostname, (char *)hostname)) ;
}
};
struct joinResNode{
unsigned short int portNo;
unsigned char hostname[256];
uint32_t location ;
bool operator<(const struct joinResNode& node1) const{
return node1.location > location ;
}
bool operator==(const struct joinResNode& node1) const{
return ((node1.portNo == portNo) && !strcmp((char *)node1.hostname, (char *)hostname) && (node1.location == location)) ;
}
};
extern map<struct node, int> nodeConnectionMap ; // To store all the neighboring nodes
extern pthread_mutex_t nodeConnectionMapLock ;
struct metaData
{
unsigned int fileNumber;
unsigned char fileID[20];
unsigned char fileName[256];
unsigned long int fileSize;
unsigned char sha1[20];
unsigned char nonce[20];
list<string > *keywords;
//map<string, int> keywords;
unsigned char bitVector[128];
};
//message structure that goes into the messgae queues
struct Message{
uint8_t type;
unsigned char *buffer ;
unsigned char *query ;
unsigned char *metadata;
unsigned char *fileName ;
uint8_t ttl ;
uint32_t location ;
int status ; // 0 - originated from here
unsigned char uoid[SHA_DIGEST_LENGTH] ;
bool fromConnect ; // 1 - The message was created by the node which
// initiated the connection
int buffer_len ;
uint8_t status_type ;
unsigned char query_type ;
uint8_t errorCode;
} ;
//information for the neighbors stored in this structures
struct connectionNode{
list<struct Message> MessageQ ;
pthread_mutex_t mesQLock ;
pthread_cond_t mesQCv ;
int shutDown ;
unsigned int keepAliveTimer;
int keepAliveTimeOut;
int myReadId;
int myWriteId;
int isReady;
bool joinFlag;
struct node n;
};
struct parsedDeleteMessage
{
unsigned char fileName[256];
unsigned char sha1[20];
unsigned char nonce[20];
unsigned char password[20];
};
extern unsigned char tempLogFile[512], tempInitFile[512];
extern bool shutDown ;
extern int accept_pid;
extern int nSocket_accept;
extern int keepAlive_pid;
extern int toBeClosed;
extern int joinTimeOutFlag;
extern int inJoinNetwork;
extern int statusTimerFlag ;
extern int checkTimerFlag ;
extern int searchTimerFlag ;
extern int node_pid;
extern int softRestartFlag ;
//extern int globalFileNumber ;
extern int globalSearchCount ;
extern int currentCacheSize;
extern unsigned char extFile[256];
extern unsigned char filesDir[256];
extern FILE *f_log;
extern map<int, struct connectionNode> connectionMap ; // Stores all the info related to a connection
extern list<pthread_t > childThreadList ;
extern map<pthread_t , bool > myConnectThread ;
extern map<string, list<int> > bitVectorIndexMap;
extern map<string, list<int> > fileNameIndexMap;
extern map<string, list<int> > sha1IndexMap;
extern map<string, int> fileIDMap;
extern map<int, struct metaData> getFileIDMap;
extern list<int > cacheLRU;
//extern list<struct metaData> metadataList;
extern pthread_mutex_t connectionMapLock ;
extern pthread_mutex_t statusMsgLock ;
extern pthread_mutex_t searchMsgLock ;
extern pthread_mutex_t getMsgLock ;
extern pthread_mutex_t logEntryLock ;
extern pthread_cond_t statusMsgCV;
extern pthread_cond_t searchMsgCV;
extern pthread_cond_t getMsgCV;
extern list<string> tmpFileNameList ;
//pakcet structre stored in the cache at the nodes
struct Packet{
struct node receivedFrom ;
int status; // 0 - originally Sent from here,
// 1 - Forwarded from here, route back, else -1
int sockfd ;
int msgLifeTime;
int status_type ;
};
extern map<string, struct Packet> MessageDB ; // Keeps a track of all the messages it sends/forwards
extern pthread_mutex_t MessageDBLock ;
// Thread function declarations
void *keyboard_thread(void *) ;
void *timer_thread(void *) ;
void *accept_connectionsT(void *); // Waits for other nodes to connect
void *read_thread(void *) ;
void *write_thread(void *) ;
void *connectBeacon(void *);
// Function declarations
int isBeaconNode(struct node n);
int connectTo(unsigned char *, unsigned int) ;
extern pthread_t k_thread;
void notifyMessageSend(int resSock, uint8_t errorCode);
void pushMessageinQ(int, struct Message ) ;
void closeConnection(int) ;
void joinNetwork() ;
void getStatus() ;
void writeToStatusFile() ;
void getStatusTypeFiles() ;
void writeToStatusFile_TypeFiles() ;
unsigned char *createLogEntry(unsigned char mode, int origin, unsigned char header[HEADER_SIZE], unsigned char *buffer);
void writeLogEntry(unsigned char *logEntry);
void eraseValueInMap(int val);
extern unsigned char *GetUOID(char *, unsigned char *, long unsigned int) ;
void initiateCheck() ;
void init() ;
void cleanup() ;
void writeMetaData(struct metaData metadata, int globalFileNumber);
int updateGlobalFileNumber();
void writeData(struct metaData, int globalFileNumber);
void populateBitVectorIndexMap(unsigned char*, unsigned int);
void populateSha1IndexMap(unsigned char*, unsigned int);
void populateFileNameIndexMap(unsigned char*, unsigned int);
unsigned char* toHex(unsigned char *str, int len);
struct metaData populateMetaData(int fileNumber);
void initiateSearch(unsigned char, unsigned char *) ;
string MetaDataToStr(struct metaData) ;
struct metaData populateMetaDataFromString(unsigned char *input);
struct metaData populateMetaDataFromCPPString(string);
int searchResponseDisplay(list<struct metaData> metadataList, int count);
void writeLRUToFile();
void readLRUFromFile();
void updateLRU(int fileNumber);
void removeFromLRU();
int storeInLRU(struct metaData metadata, unsigned int fileNumber);
list<int> getAllFiles() ;
struct parsedDeleteMessage parseDeleteMessage(unsigned char *message);
void deleteFile(struct parsedDeleteMessage pd);
int getFileNumberFromIndex(unsigned char *fileName, unsigned char *nonce);
void deleteFromIndex(int);
void deleteAllFiles();
void initiateDelete(unsigned char *) ;
void initiateStore(string, string) ;
void writeFileToCache(unsigned char *metadata_str, unsigned char *fileName);
void writeFileToPermanent(unsigned char *metadata_str, unsigned char *fileName);
void initiateGet(struct metaData) ;
struct metaData populateMetaDataFromString_noFileID(unsigned char *input1);
int doesFileExist(struct metaData );
void writeToFileFromData(unsigned char fileName[], int fileNumber);