-
Notifications
You must be signed in to change notification settings - Fork 0
/
CppServer.cpp
371 lines (325 loc) · 11.4 KB
/
CppServer.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
/*
* INCLUDES
*/
// include thrift generated code
#include "APIs.h"
// include Thrift library
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
// include POCO library files
#include "Poco/Foundation.h"
#include "Poco/Util/PropertyFileConfiguration.h"
#include "Poco/Logger.h"
#include "Poco/FileChannel.h"
#include "Poco/ConsoleChannel.h"
#include "Poco/AutoPtr.h"
#include "Poco/SplitterChannel.h"
#include "Poco/PatternFormatter.h"
#include "Poco/FormattingChannel.h"
#include "Poco/Data/Session.h"
#include <Poco/Data/MySQL/MySQLException.h>
#include <Poco/Data/MySQL/Connector.h>
#include <Poco/Data/SessionFactory.h>
// include other libraries
//#include <libmemcached/memcached.h>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <string>
/*
* USING
*/
// using thrift generated code
using namespace ::thriftDemo;
// using namespaces from Thrift
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using namespace apache::thrift::concurrency;
// using namespaces from POCO
using Poco::AutoPtr;
using Poco::Util::PropertyFileConfiguration;
using Poco::Logger;
using Poco::FileChannel;
using Poco::ConsoleChannel;
using Poco::SplitterChannel;
using Poco::PatternFormatter;
using Poco::FormattingChannel;
using namespace Poco::Data;
using namespace Poco::Data::Keywords;
using Poco::Data::Session;
using Poco::Data::Statement;
using Poco::Data::MySQL::ConnectionException;
using Poco::Data::MySQL::StatementException;
// using other namespaces
using namespace std;
using boost::shared_ptr;
/*
* GLOBAL VARS
*/
int port = 0;
int isApplyCache = 0;
std::string mySQLHost = "127.0.0.1";
int mySQLPort = 0;
std::string mySQLDb = "";
std::string mySQLUsername = "";
std::string mySQLPassword = "";
Session* mySQLsession;
struct ViewCountInfo {
std::string username;
int counter;
};
class Utilities {
public:
static std::string convertToString(int number) {
ostringstream convert; // stream used for the conversion
convert << number; // insert the textual representation of 'Number' in the characters in the stream
std::string res = convert.str(); // set 'Result' to the contents of the stream
return res;
}
};
class APIsHandler : virtual public APIsIf {
private:
int getRequest(const std::string& _username) {
int res = -1;
Session localSession = getSession();
Statement select(localSession);
// commented out because of thread conflicts
// Statement select(*mySQLsession);
std::string match("'" + _username + "'");
select << "SELECT counter FROM view_count_info WHERE username = " + match + " LIMIT 1;", into(res);
select.execute();
return res;
}
void putRequest(const std::string& _username, const int32_t _newValue) {
Session localSession = getSession();
// Statement update(*mySQLsession);
Statement update(localSession);
std::string match("'" + _username + "'");
update << "UPDATE view_count_info SET counter=" + Utilities::convertToString(_newValue) + " WHERE username = " + match;
update.execute();
}
Session getSession() {
Poco::Data::MySQL::Connector::registerConnector();
std::string sessionStr = "host=";
sessionStr += mySQLHost;
sessionStr += ";port=";
sessionStr += Utilities::convertToString(mySQLPort);
sessionStr += ";db=";
sessionStr += mySQLDb;
sessionStr += ";user=";
sessionStr += mySQLUsername;
sessionStr += ";password=";
sessionStr += mySQLPassword;
sessionStr += ";compress=true;auto-reconnect=true";
Session session("MySQL", sessionStr);
return session;
}
public:
APIsHandler() {
// Your initialization goes here
}
bool put(const std::string& _username, const int32_t _newValue) {
Logger::root().information("handle PUT request");
// check if existed or not
int isExisted = getRequest(_username);
if (isExisted < 0) {
// the username is not existed yet
return false;
} else {
putRequest(_username, _newValue);
}
return true;
}
bool increase(const std::string& _username) {
Logger::root().information("handle INCREASE request");
// check if existed or not
int isExisted = getRequest(_username);
if (isExisted < 0) {
// the username is not existed yet
return false;
} else {
putRequest(_username, isExisted + 1);
}
return true;
}
int32_t get(const std::string& _username) {
Logger::root().information("handle GET request");
cout << _username << endl;
int result = getRequest(_username);
//Logger::root().information("GET: username = " + _username + " ,counter = " + result);
cout << result << endl;
return result;
}
bool ping() {
Logger::root().information("handle PING request");
return true;
}
};
void getPropertiesInfo() {
AutoPtr<PropertyFileConfiguration> pConf;
pConf = new PropertyFileConfiguration("viewcount.properties");
cout << "=== The following are information get from the viewcount.properties file: ===" << endl;
int serverPort = pConf->getInt("SERVER_PORT");
cout << "serverPort: " << serverPort << endl;
port = serverPort;
int applyCache = pConf->getInt("APPLY_CACHE");
cout << "applyCache: " << applyCache << endl;
isApplyCache = applyCache;
string authorName = pConf->getString("AUTHOR_NAME");
cout << "authorName: " << authorName << endl;
mySQLHost = pConf->getString("MYSQL_HOST");
cout << "mySQLHost: " << mySQLHost << endl;
mySQLPort = pConf->getInt("MYSQL_PORT");
cout << "mySQLPort: " << mySQLPort << endl;
mySQLDb = pConf->getString("MYSQL_DB");
cout << "mySQLDb: " << mySQLDb << endl;
mySQLUsername = pConf->getString("MYSQL_USERNAME");
cout << "mySQLUsername: " << mySQLUsername << endl;
mySQLPassword = pConf->getString("MYSQL_PASSWORD");
cout << "mySQLPassword: " << mySQLPassword << endl;
cout << "==========" << endl;
}
void initLogger() {
AutoPtr<FileChannel> pFileChannel(new FileChannel);
pFileChannel->setProperty("path", "viewCount.log");
AutoPtr<ConsoleChannel> pConsoleChannel(new ConsoleChannel);
AutoPtr<SplitterChannel> pSplitter(new SplitterChannel);
// add pattern formatter, more details go in to the messages
AutoPtr<PatternFormatter> pPatternFormatter(new PatternFormatter);
pPatternFormatter->setProperty("pattern", "%Y-%m-%d %H:%M:%S %s: %t");
AutoPtr<FormattingChannel> pFCFileChannel(new FormattingChannel(pPatternFormatter, pFileChannel));
AutoPtr<FormattingChannel> pFCConsoleChannel(new FormattingChannel(pPatternFormatter, pConsoleChannel));
pSplitter->addChannel(pFCConsoleChannel);
pSplitter->addChannel(pFCFileChannel);
Logger::root().setChannel(pSplitter);
}
void insertTestData(Session* session) {
// insert some rows
ViewCountInfo vcInfo ={
"A",
1
};
Statement insert(*session);
insert << "INSERT INTO view_count_info VALUES(?, ?)",
use(vcInfo.username),
use(vcInfo.counter);
insert.execute();
vcInfo.username = "B";
vcInfo.counter = 5;
insert.execute();
vcInfo.username = "C";
vcInfo.counter = 20;
insert.execute();
vcInfo.username = "D";
vcInfo.counter = 50;
insert.execute();
}
void initDb() {
// register MySQL connector
Poco::Data::MySQL::Connector::registerConnector();
// create a session
std::string sessionStr = "host=";
sessionStr += mySQLHost;
sessionStr += ";port=";
sessionStr += Utilities::convertToString(mySQLPort);
sessionStr += ";db=";
sessionStr += mySQLDb;
sessionStr += ";user=";
sessionStr += mySQLUsername;
sessionStr += ";password=";
sessionStr += mySQLPassword;
sessionStr += ";compress=true;auto-reconnect=true";
mySQLsession = new Session("MySQL", sessionStr);
// drop sample table, if it exists
*mySQLsession << "DROP TABLE IF EXISTS view_count_info", now;
// (re)create table
*mySQLsession << "CREATE TABLE view_count_info (username VARCHAR(30), counter INTEGER(5))", now;
insertTestData(mySQLsession);
}
void initCaching(){
if (isApplyCache){
cout << "Using cache" << endl;
}
else cout << "Not using cache" << endl;
//connect server
// memcached_st *memc;
// memcached_return rc;
// memcached_server_st *server;
// time_t expiration;
// uint32_t flags;
//
// memc = memcached_create(NULL);
// server = memcached_server_list_append(NULL, "localhost", 11211, &rc);
// rc = memcached_server_push(memc, server);
// memcached_server_list_free(server);
//
// string key = "username";
// string value = "hehe";
// size_t value_length = value.length();
// size_t key_length = key.length();
//
// //Save data
// rc = memcached_set(memc, key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags);
// if (rc == MEMCACHED_SUCCESS) {
// cout << "Save data:" << value << " sucessful!" << endl;
// }
//
// //Get data
// char* result = memcached_get(memc, key.c_str(), key_length, &value_length, &flags, &rc);
// if (rc == MEMCACHED_SUCCESS) {
// cout << "Get value:" << result << " sucessful!" << endl;
// }
//
// //Delete data
// rc = memcached_delete(memc, key.c_str(), key_length, expiration);
// if (rc == MEMCACHED_SUCCESS) {
// cout << "Delete key:" << key << " sucessful!" << endl;
// }
//
// //free
// memcached_free(memc);
}
int main(int argc, char **argv) {
// print out viewcount.properties file information
getPropertiesInfo();
// init POCO's logger settings
initLogger();
// initialize the system database
initDb();
// initialize the caching database
initCaching();
// start the server
boost::shared_ptr<APIsHandler> handler(new APIsHandler());
boost::shared_ptr<TProcessor> processor(new APIsProcessor(handler));
boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
//TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
// TThreadPoolServer
const int workerCount = 4;
boost::shared_ptr<ThreadManager> threadManager =
ThreadManager::newSimpleThreadManager(workerCount);
boost::shared_ptr<PosixThreadFactory> threadFactory =
boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TThreadPoolServer server(processor,
serverTransport,
transportFactory,
protocolFactory,
threadManager);
Logger::root().information("> Server is running");
server.serve();
return 0;
}