-
Notifications
You must be signed in to change notification settings - Fork 0
/
NyanSync_server.cpp
90 lines (69 loc) · 2.09 KB
/
NyanSync_server.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
#include "NyanSync.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TNonblockingServer.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thread>
#include "Nyancat.cpp"
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::concurrency;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
using namespace ::nyan;
const int FIX_FRAME = 22;
int server_counter = 0;
int frame_counter = 0;
int initialized = 0;
class NyanSyncHandler : virtual public NyanSyncIf {
public:
NyanSyncHandler() {
// Your initialization goes here
}
void sync(SyncPacket& _return) {
if (initialized == 0) {
//init counter
server_counter = 1;
frame_counter = 0;
initialized = 1;
}
_return.server = server_counter;
_return.frame = frame_counter;
printf("%d\n", server_counter);
printf("%d\n", frame_counter);
printf("sync\n");
}
};
int counterThread() {
while (1) {
frame_counter++;
if (frame_counter > FIX_FRAME) {
server_counter++;
frame_counter = 0;
}
usleep(FRAMESPEED);
}
return 0;
}
int serverThread(int port) {
printf("creating server with port: %d", port);
shared_ptr<NyanSyncHandler> handler(new NyanSyncHandler());
shared_ptr<TProcessor> processor(new NyanSyncProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(15);
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
TNonblockingServer server(processor, protocolFactory, port, threadManager);
server.serve();
return 0;
}
int main(int argc, char **argv) {
std::thread cnt(counterThread);
std::thread svr1(serverThread, 9090);
svr1.join();
cnt.join();
return 0;
}