-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
190 lines (166 loc) · 6.89 KB
/
user.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
User :: User(string username, string password){
myInfo.username = username;
myInfo.password password;
FormMessage_signup();
rpcId = 0;
}
void updateStatus(){
string messageS = "0";
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT , rpcId, Operation::login, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
//will I do anything with replyMessage?
}
//acting as a client
void Peer:: login (){
string messageS = myInfo.username + "," + myInfo.password;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT , rpcId, Operation::login, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
//will I do anything with replyMessage?
}
void Peer:: signup(){
string messageS = myInfo.username + "," + myInfo.password;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT , rpcId, Operation::signup, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
//will I do anything with replyMessage?
}
void Peer:: logout(){
string messageS = myInfo.username + "," + myInfo.password;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT , rpcId, Operation::logout, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
//will I do anything with replyMessage?
}
void Peer:: Client_uploadImage(string imageName){
String messageS = myInfo.username +","+ imageName;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT, rpcId, Operation::uploadImage, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
//will I do anything with replyMessage?
}
void Peer:: Client_viewImage(string ownerUsername, string imageName){
String messageS = ownerUsername;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT, rpcId, Operation::getPortnIP, messageS.size(), messageC);
sendMessage(message, replyMessage, False);
vector<string> args;
args = replyMessage.getMessageArgs();
messageS = myInfo.username + "," + imageName;
messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, args[0], stoi(args[1]), rpcId, Operation::viewImage, messageS.size(), messageC);
sendMessage(message, replyMessage, False);
//TODO
//View image in reply message
}
void Peer:: Client_requestImage(){
String messageS = ownerUsername;
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT, rpcId, Operation::getPortnIP, messageS.size(), messageC);
sendMessage(message, replyMessage, False);
vector<string> args;
args = replyMessage.getMessageArgs();
messageS = imageName;
messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, args[0], stoi(args[1]), rpcId, Operation::requestImage, messageS.size(), messageC);
sendMessage(message, replyMessage, False);
//will I do anything with replyMessage?
}
void getAllImages(){
String messageS = "-1";
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * replyMessage;
rpcId++;
Message * message(MessageType:: Request, 0, 0, udpClientSocket.myAddress, DIRECTORY_IP, DIRECTORY_PORT, rpcId, Operation::getAllImages, messageS.size(), messageC);
sendMessage(message, replyMessage ,False);
vector<string> args;
args = replyMessage.getMessageArgs();
for(int i=0; i<args.size(); i++){
allImages.ownerUsername = args[i++];
allImages.imageId = args[i];
}
}
//acting as a server
void Peer:: Server_viewImage(Message * request, UDPSocket socket){
vector<string> args;
args = request.getMessageArgs();
int imgIdx;
for (imgIdx=0; imgIdx<myImages.size(); imgIdx++){
if(myImages[imgIdx].imageId == args[1]){
break;
}
}
vector <struct userProperties> properties;
int userIdx;
for (userIdx=0; userIdx<properties.size(); userIdx++){
if(properties[userIdx].username == args[0]){
break;
}
}
if (properties[userIdx].views > 0)
properties[userIdx].views--;
myImages[imgIdx].setProperties(properties);
String messageS = myImages[imgIdx].extractImage();
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * message(MessageType:: Reply, 0, 0, socket.myAddress, request.getSourceIP(), request.getSourcePort(), rpcId, request.getOperation(), messageS.size(), messageC);
sendReply(message);
}
void Peer:: Server_requestImage(){
vector<string> args;
args = request.getMessageArgs();
int imgIdx;
for (imgIdx=0; imgIdx<myImages.size(); imgIdx++){
if(myImages[imgIdx].imageId == args[1]){
break;
}
}
vector <struct userProperties> properties;
int userIdx;
for (userIdx=0; userIdx<properties.size(); userIdx++){
if(properties[userIdx].username == args[0]){
break;
}
}
if (properties[userIdx].views > 0)
properties[userIdx].views = 1;
else{
struct userProperties userProp;
userProp.username = args[0];
userProp.views = stoi(args[1]);
properties.push_back(userProp);
}
myImages[imgIdx].setProperties(properties);
String messageS = "1";
char * messageC = new char[messageS.size()];
strcpy(messageC, messageS.c_str());
Message * message(MessageType:: Ack, 0, 0, socket.myAddress, request.getSourceIP(), request.getSourcePort(), rpcId, request.getOperation(), messageS.size(), messageC);
sendReply(message);
}