-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg_q.cpp
More file actions
108 lines (96 loc) · 3.16 KB
/
msg_q.cpp
File metadata and controls
108 lines (96 loc) · 3.16 KB
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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef struct {
int clientId;
int window;
} client_s;
typedef struct {
int clientId;
int msgId;
int msgAge;
} msg_s;
map<int,client_s *> client_list;
map<int,vector<msg_s *>*> msg_list;
map<int,map<int,msg_s *>> msg_map;
bool CompareClientFn(msg_s* l1,msg_s* l2) {
return l1->msgId > l2->msgId;
}
void send_message(int clientId, int msgId, int msgAge) {
cout<<clientId<<"\t"<<msgId<<"\t"<<msgAge<<"\n";
}
vector<msg_s* >* getVector() {
vector<msg_s *>* v= new vector<msg_s *>;
return v;
}
void register_client(int clientId, int window) {
client_s* client = (client_s *)malloc(sizeof(client_s));
client->clientId = clientId;
client->window = window;
client_list.insert(pair<int, client_s *>(clientId,client));
msg_list.insert(pair<int,vector<msg_s *>*>(clientId,getVector()));
}
void adjust_window(int clientId, int window) {
client_s* client = client_list[clientId];
client->window = window;
map<int,vector<msg_s *>*> :: iterator it;
it = msg_list.find(clientId);
vector <msg_s *>* temp1 = it->second;
while(client->window > 0 && temp1->size() > 0) {
msg_s* temp = it->second->front();
pop_heap (temp1->begin(),temp1->end(),CompareClientFn);
it->second->pop_back();
map<int,map<int, msg_s *>>:: iterator itr = msg_map.find(clientId);
map<int, msg_s *>::iterator it_inner = itr->second.find(temp->msgId);
itr->second.erase(it_inner);
send_message(temp->clientId,temp->msgId,temp->msgAge);
client->window--;
}
}
void new_message(int clientId, int msgId, int msgAge) {
map<int, client_s *>:: iterator itr;
itr = client_list.find(clientId);
if(itr != client_list.end()){
if(itr->second->window > 0) {
send_message(clientId,msgId,msgAge);
itr->second--;
}
else {
map<int,map<int, msg_s *>>:: iterator itr = msg_map.find(clientId);
map<int, msg_s *>::iterator it_inner = itr->second.find(msgId);
if(it_inner != itr->second.end()) {
it_inner->second->msgAge=msgAge;
}
else {
msg_s* temp = (msg_s *)malloc(sizeof(msg_s));
temp->clientId = clientId;
temp->msgId = msgId;
temp->msgAge = msgAge;
map<int, vector<msg_s *>*>::iterator it;
it = msg_list.find(clientId);
it->second->push_back(temp);
push_heap(it->second->begin(),it->second->end(),CompareClientFn);
msg_map[clientId][msgId] = temp;
}
}
} else {
cout<<"The client has not registered"<<endl;
}
}
int main() {
register_client(10,1);
new_message(10,1,1);
new_message(10,2,1);
new_message(10,3,2);
adjust_window(10,3);
new_message(10,1,2);
new_message(10,5,2);
new_message(10,4,1);
new_message(10,5,3);
adjust_window(10,3);
new_message(20,2,2);
register_client(20,2);
new_message(20,1,2);
}