-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppMessage.cpp
398 lines (330 loc) · 7.93 KB
/
AppMessage.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* AppMessage.cpp
*
* Created on: Nov 23, 2016
* Author: akash
*/
#include<iostream>
#include<cstring>
#include<string>
# include<cstring>
# include <fstream>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
# include <cmath>
#include "config.h"
#include "unp.h"
using namespace std;
int bin2int(char * );
string getBinary(int num,int len);
int char2int(char * );
struct AppMessage{
char data[51];
char size[7]; // size show the no of bytes in the data array
char msgtype;// this will show the msg Type i.e. 0:Request(client) or 1:Response(Server)
char rqstType[3];//this will tell further about the request message i.e. 00:Show files
// 01:download file, 10: upload file
char key[6]; // this is the key used for encryption and decryption
char status[3];//this will show the status of file i.e. 00: End of file, 01: Error in file
// 10:FileNotfound, 11:File continuing
AppMessage(){
memset(data,'\0',51);
memset(size,'\0',6);
msgtype='\0';
memset(rqstType,'\0',3);
memset(key,'\0',6);
memset(status,'\0',3);
}
bool setRqstType(int number){
string str=getBinary(number,2);
for (int i=0;i<2;i++){
rqstType[i]=str[i];
}
size[2]='\0';
return true;
}
void setMsgType(int number){
if (number==0)
msgtype='0';
else
msgtype='1';
}
bool setSize(int number){
string str=getBinary(number,6);
for (int i=0;i<6;i++){
size[i]=str[i];
}
size[6]='\0';
return true;
}
void setStatus(int number){
string str=getBinary(number,2);
for (int i=0;i<2;i++){
status[i]=str[i];
}
status[2]='\0';
}
void setKey(int number){
string str=getBinary(number,5);
for (int i=0;i<5;i++){
key[i]=str[i];
}
key[5]='\0';
}
};
class AppHandler{
private:
public:
string fileData;
char * fileName;
int fileSize;
int sentByte;
int tempSentByte;
int msgType;
int packetSize;
public:
AppHandler(){
//flush
sentByte=0;
tempSentByte=0;
fileSize=0;
msgType=-1;
packetSize=0;
}
void flush(AppMessage& appmessage){
memset(appmessage.data,'\0',51);
memset(appmessage.size,'\0',6);
appmessage.msgtype='\0';
memset(appmessage.rqstType,'\0',3);
memset(appmessage.key,'\0',6);
memset(appmessage.status,'\0',3);
}
void getClientShow(AppMessage& appmessage){
flush(appmessage);
appmessage.msgtype='0';
appmessage.rqstType[0]='0';
appmessage.rqstType[1]='0';
appmessage.rqstType[2]='\0';
}
void getClientDownload(AppMessage& appMessage,string fileName){
flush(appMessage);
appMessage.msgtype='0';
appMessage.rqstType[0]='0';
appMessage.rqstType[1]='1';
appMessage.rqstType[2]='\0';
int i=0;
for (i=0;i<fileName.length() ;i++){
appMessage.data[i]=fileName[i];
}
appMessage.data[i]='\0';
}
void DisplayAppMsg(AppMessage& appmessage){
cout<<"Data : "<< appmessage.data<<endl;
cout<<"Key : "<<appmessage.key<<endl;
cout<<"Message Type : "<<appmessage.msgtype<<endl;
cout<<"Message Rqst Type : "<<appmessage.rqstType<<endl;
cout<<"Size : "<<appmessage.size<<endl;
cout<<"Status : "<<appmessage.status<<endl;
}
bool setRequestAtServer(AppMessage& appmessage){
if ( !strcmp(appmessage.rqstType,"00") ){
msgType=1;
serverShowFiles();
return true;
}
else if ( !strcmp(appmessage.rqstType,"01") ){
msgType=1;
cout<<"File Name to read : "<<appmessage.data<<endl;
sleep(2);
this->fileName=new char [strlen(appmessage.data)+1];
for (int i=0;i<strlen(appmessage.data);i++){
this->fileName[i]=appmessage.data[i];
}
this->fileName[strlen(appmessage.data)]='\0';
return readFile(appmessage.data);
}
else if (! strcmp(appmessage.rqstType,"10")){
msgType=1;
}
return false;
}
void setPacketSize(int pktSize){
packetSize=pktSize;
}
void serverShowFiles(){
char dirArray[256];
struct dirent *dirp;
DIR * filedesc;
getcwd(dirArray, 600);
string output;
if ((filedesc = opendir(dirArray)) == NULL) {
cout<<"Files can't be open\n";
return ;
}
string s1="";
string s2=".txt";
string s3=".txt~";
for ( ;(dirp = readdir(filedesc)) != NULL; ) {
s1=dirp->d_name;
if(!strcmp(dirp->d_name,".") )
continue;
else if (!strcmp(dirp->d_name,".."))
continue;
else if(strstr(s1.c_str(),s3.c_str()))
continue;
else if(strstr(s1.c_str(),s2.c_str())){
output+=dirp->d_name;
//output+=".txt";
output+="$";
}
s1.clear();
}
cout<<output<<endl;
fileData=output;
fileSize=output.length();
}
bool readFile(char * fileName){
fileData.clear();
fileSize=0;
ifstream ifile(fileName);
if (!ifile){
cout<<"File Does not exist\n";
return false;
}
else{
string line;
while(ifile){
getline(ifile,line);
fileData+=line;
}
fileSize=fileData.length();
}
ifile.close();
return true;
}
bool setAppMessage(AppMessage& appMessage){
cout<<"Sent bye : "<<sentByte<<endl;
cout<<"fliseize : "<<fileSize<<endl;
cout<<"TempSentByte : "<<tempSentByte<<endl;
sleep(5);
for (int i=0;i<packetSize;i++){
if (sentByte+tempSentByte+i<fileSize){
appMessage.data[i]=fileData[sentByte+tempSentByte+i];
cout<<"Data at index : "<<i<<" is "<<appMessage.data[i]<<endl;
sleep(1);
}
else{
cout<<"nothing to send :(\n";
sleep(2);
appMessage.setStatus(0);
appMessage.setSize(i);
// tempSentByte+=i;
// sentByte+=i;
return true;
}
}
appMessage.setStatus(3);
appMessage.setSize(strlen(appMessage.data));
//tempSentByte+=packetSize;
//sentByte+=packetSize;
return false;
}
bool setDataDatagram(AppMessage& appMessage,int & ackNo, char * seqNo){
if (!strcmp(appMessage.status,"00")){
fileData+=appMessage.data;
fileSize=fileData.length();
char * data=new char[fileData.length()+1];
for (int i=0;i<fileData.length();i++){
data[i]=fileData[i];
}
data[fileData.length()]='\0';
char * arr=strtok(data,"$");
cout<<"***********************************************************";
cout<<"\nFile Names \n\n";
cout<<"***********************************************************\n";
while(arr){
cout<<arr<<endl;
arr=strtok(NULL,"$");
}
sleep(2);
fileData.clear();
fileSize=0;
cout<<"End of File\n";
return true;
}
if (bin2int(seqNo) >= fileSize){
fileData+=appMessage.data;
fileSize=fileData.length();
ackNo=char2int(appMessage.size);
}
cout<<"Seq No : "<<bin2int(seqNo)<<endl;
cout<<" file Size : "<<fileSize<<endl;
cout<<"Data : "<<fileData<<endl;
sleep(5);
return false;
}
bool setDataDatagram2(AppMessage& appMessage,int & ackNo,char * seqNo){
if (!strcmp(appMessage.status,"00")){
fileData+=appMessage.data;
fileSize=fileData.length();
ofstream ofile("output.txt");
if (!ofile){
cout<<"File can't create\n";
}
else{
ofile<<fileData;
}
ofile.close();
sleep(2);
fileData.clear();
fileSize=0;
cout<<"End of File\n";
return true;
}
if ( bin2int(seqNo) >=fileSize ){
fileData+=appMessage.data;
fileSize=fileData.length();
ackNo=char2int(appMessage.size);
}
cout<<"Seq No : "<<bin2int(seqNo)<<endl;
cout<<" file Size : "<<fileSize<<endl;
cout<<"Data : "<<fileData<<endl;
sleep(5);
return false;
}
};
string getBinary(int num, int len){
char * arr= new char [len+1];
for (int i=0;i<len;i++){
if (num>0){
if (num%2==0)
arr[len-i-1]='0';
else
arr[len-i-1]='1';
num=num/2;
}
else{
arr[len-i-1]='0';
}
}
arr[len]='\0';
return arr;
}
int char2int(char * arr){
int length=strlen(arr);
int retVal=0;
for (int i=1;i<=length;i++){
retVal*=10;
retVal+=static_cast<int>(arr[length-i]%48);
}
return retVal;
}
int bin2int(char * arr){
int length=strlen(arr);
int retVal=0;
for (int i=1;i<=length;i++){
retVal+=(static_cast<int>(arr[length-i]%48))*pow(2,i-1);
}
return retVal;
}