-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestcase.cpp
142 lines (112 loc) · 4.62 KB
/
testcase.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
#include <string>
#include <iostream>
#include <queue>
#include <thread>
#include <mutex>
#include <memory>
#include <map>
#include <condition_variable>
#include "libmumbot/proto/MumBot.pb.h"
#include "libmumbot/proto/MumBot.grpc.pb.h"
//GRPC includes
#include <grpc/grpc.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc++/security/server_credentials.h>
using namespace libmumbot;
class MumBotRPCWorkQueueMgr {
private:
class TextMessageQueue {
public:
std::queue<std::string> queue;
std::mutex cv_mtx;
std::condition_variable cv;
};
std::mutex txtMessageQueuesMutex_; //easiest solution for now
std::map<int,std::unique_ptr<TextMessageQueue>> textMessageQueues_;
int maxTextMessageQueueIndex_ = 0;
public:
int createTextMessageQueue() { //returns index of queue to watch
std::cout << "Calling createTextMessageQueue\n";
std::lock_guard<std::mutex> lock(txtMessageQueuesMutex_); //easiest solution for now
std::cout << "Calling createTextMessageQueue - 2\n";
auto ptr = std::make_unique<TextMessageQueue>();
textMessageQueues_[maxTextMessageQueueIndex_] = std::move(ptr);
maxTextMessageQueueIndex_++;
return maxTextMessageQueueIndex_ - 1;
}
void removeTextMessageQueue(int index) { //deletes queue
std::cout << "Calling removeTextMessageQueue\n";
std::lock_guard<std::mutex> lock(txtMessageQueuesMutex_); //easiest solution for now
textMessageQueues_.erase(index);
}
std::string getNextTextMessage(int queueid) { //pop next text message in a specific queue
std::cout << "Calling getNextTextMessage\n";
std::unique_lock<std::mutex> tlock(txtMessageQueuesMutex_);
try {
std::unique_ptr<TextMessageQueue> &ptr = textMessageQueues_.at(queueid);
tlock.unlock();
std::unique_lock<std::mutex> qlock(ptr->cv_mtx);
while (ptr->queue.empty()) {
ptr->cv.wait(qlock); //will unlock ptr->mtx and wait for notification
}
std::cout << "lock 3\n";
//qlock.lock();
std::string msg = ptr->queue.front();
ptr->queue.pop();
return msg;
}
catch (std::exception &e) {
throw std::out_of_range("MumBotRPCWorkQueueMgr::getNextTextMessage queueid does not exist " + std::to_string(queueid));
}
return "";
}
void pushNextTextMessage(std::string msg) { //adds to all queues
std::cout << "Calling pushNextTextMessage\n";
std::lock_guard<std::mutex> lock(txtMessageQueuesMutex_); //easiest solution for now
for (const auto &i : textMessageQueues_) { //push to all queues
std::lock_guard<std::mutex>(i.second->cv_mtx);
i.second->queue.push(msg);
i.second->cv.notify_all();
std::cout << "Notify all\n";
}
}
};
class SegfaultyClass: public protobuf::MumBotRPC::Service {
private:
MumBotRPCWorkQueueMgr RPCWorkQueueMgr_;
public:
grpc::Status SubscribeToTextMessages(::grpc::ServerContext* context, const protobuf::TextMessageRequest* request, ::grpc::ServerWriter<protobuf::TextMessage>* writer) {
std::cout << "Client subscribed to text messages\n";
uint32_t myindex = RPCWorkQueueMgr_.createTextMessageQueue();
for (;;) {
std::string msg = RPCWorkQueueMgr_.getNextTextMessage(myindex); //will wait
std::cout << "Got a message:" << msg << "\n";
if (context->IsCancelled()) break;
protobuf::TextMessage tmsg;
tmsg.set_msg(msg);
writer->Write(tmsg);
}
std::cout << "Client connection dropped or cancelled\n";
RPCWorkQueueMgr_.removeTextMessageQueue(myindex);
return grpc::Status::OK;
}
grpc::Status Say(::grpc::ServerContext* context, const protobuf::TextMessage* request, protobuf::TextMessageResponse* response) {
std::cout << "Message received:" << request->msg() << "\n";
return grpc::Status::OK;
}
void startRPCService() {
grpc::ServerBuilder builder;
builder.AddListeningPort("0.0.0.0:50080",grpc::InsecureServerCredentials());
builder.RegisterService(this);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
server->Wait();
std::cout << "Quitting!!!!\n";
}
};
int main() {
SegfaultyClass seggyMcFaultFault;
seggyMcFaultFault.startRPCService();
return 0;
}