summaryrefslogtreecommitdiff
path: root/src/3rd_party-static/MessageBroker/src/example/MessageBrokerServer.cpp
blob: 405b3fcbd56285b899838fe83741cdfb746e6258 (plain)
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
/**
 * \file MessageBrokerServer.cpp
 * \brief MessageBrokerServer sources
 * \author AKara
 */

#include <cstdio>
#include <cstdlib>
#include <csignal>
#include <iostream>
#include <ctime>

#include "system.h"

#include "MBDebugHelper.h"

#include "mb_tcpserver.hpp"
#include "mb_tcpclient.hpp"
#include "CMessageBroker.hpp"

#include "MessageBrokerControllerAVA.hpp"
#include "MessageBrokerControllerPhone.hpp"
#include "MessageBrokerControllerBackend.hpp"

/**
 * \brief Signal management.
 * \param code signal code
 */
 static void signal_handler(int code)
 {
   switch(code)
   {
      case SIGINT:
      case SIGTERM:
      break;
      default:
      break;
   }
}

/**
 * \brief stores start time of test operation.
 */
int start;

/**
 * \brief Entry point of the program.
 * \param argc number of argument
 * \param argv array of arguments
 * \return EXIT_SUCCESS or EXIT_FAILURE
 */
int main(int argc, char** argv)
{
   NsMessageBroker::CMessageBroker* mpMessageBroker = NsMessageBroker::CMessageBroker::getInstance();
   if (!mpMessageBroker)
   {
      DBG_MSG_ERROR(("NULL pointer\n"));
      exit(EXIT_FAILURE);
   }

   
   NsMessageBroker::TcpServer server(std::string("127.0.0.1"), 8086, mpMessageBroker);
   
   DBG_MSG(("Start MessageBroker component\n"));
   mpMessageBroker->startMessageBroker(&server);

   NsMessageBroker::CMessageBrokerControllerAVA tcpControllerAVA(std::string("127.0.0.1"), 8086);
   NsMessageBroker::CMessageBrokerControllerPhone tcpControllerPhone(std::string("127.0.0.1"), 8086);
   NsMessageBroker::CMessageBrokerControllerBackend tcpControllerBackend(std::string("127.0.0.1"), 8086);
  
   /* avoid compilation warnings */
   argc = argc;
   argv = argv;

   if(!networking::init())
   {
      DBG_MSG_ERROR(("Networking initialization failed!\n"));
      
   }

   if(signal(SIGTERM, signal_handler) == SIG_ERR)
   {
      DBG_MSG_ERROR(("Error signal SIGTERM will not be handled!\n"));
   }

   if(signal(SIGINT, signal_handler) == SIG_ERR)
   {
      DBG_MSG_ERROR(("Error signal SIGINT will not be handled!\n"));
   }

   if(!server.Bind())
   {
      DBG_MSG_ERROR(("Bind failed!\n"));
      exit(EXIT_FAILURE);
   } else
   {
      DBG_MSG(("Bind successful!\n"));
   }

   if(!server.Listen())
   {
      DBG_MSG_ERROR(("Listen failed!\n"));
      exit(EXIT_FAILURE);
   } else
   {
      DBG_MSG(("Listen successful!\n"));
   }

   if(!tcpControllerAVA.Connect())
   {
      DBG_MSG_ERROR(("Cannot connect to remote peer!\n"));
      exit(EXIT_FAILURE);
   } else
   {
      DBG_MSG(("ClientAVA connected to the server! SocketID = %d\n", tcpControllerAVA.GetSocket()));
   }

   if(!tcpControllerPhone.Connect())
   {
      DBG_MSG_ERROR(("Cannot connect to remote peer!\n"));
      exit(EXIT_FAILURE);
   } else
   {
      DBG_MSG(("ClientPhone connected to the server! SocketID = %d\n",tcpControllerPhone.GetSocket()));
   }

   if(!tcpControllerBackend.Connect())
   {
      DBG_MSG_ERROR(("Cannot connect to remote peer!\n"));
      exit(EXIT_FAILURE);
   } else
   {
      DBG_MSG(("ClientBackend connected to the server! SocketID = %d\n",tcpControllerBackend.GetSocket()));
   }

   DBG_MSG(("Start CMessageBroker thread!\n"));
   System::Thread th1(new System::ThreadArgImpl<NsMessageBroker::CMessageBroker>(*mpMessageBroker, &NsMessageBroker::CMessageBroker::MethodForThread, NULL));
   th1.Start(false);

   DBG_MSG(("Start MessageBroker TCP server thread!\n"));
   System::Thread th2(new System::ThreadArgImpl<NsMessageBroker::TcpServer>(server, &NsMessageBroker::TcpServer::MethodForThread, NULL));
   th2.Start(false);

   DBG_MSG(("Start tcpControllerAVA receiver thread!\n"));
   System::Thread th3(new System::ThreadArgImpl<NsMessageBroker::CMessageBrokerControllerAVA>(tcpControllerAVA, &NsMessageBroker::CMessageBrokerControllerAVA::MethodForReceiverThread, NULL));
   th3.Start(false);

   DBG_MSG(("Start tcpControllerPhone receiver thread!\n"));
   System::Thread th4(new System::ThreadArgImpl<NsMessageBroker::CMessageBrokerControllerPhone>(tcpControllerPhone, &NsMessageBroker::CMessageBrokerControllerPhone::MethodForReceiverThread, NULL));
   th4.Start(false);

   DBG_MSG(("Start tcpControllerBackend receiver thread!\n"));
   System::Thread th5(new System::ThreadArgImpl<NsMessageBroker::CMessageBrokerControllerBackend>(tcpControllerBackend, &NsMessageBroker::CMessageBrokerControllerBackend::MethodForReceiverThread, NULL));
   th5.Start(false);

   bool loop = true;
   while(loop)
   {
      DBG_MSG(("Enter command code:\n"));
      int i;
      std::cin >> i;
      switch(i)
      {
         case 0:
         {
            DBG_MSG(("Exit!\n"));
            mpMessageBroker->stopMessageBroker();
            th1.Stop();
            th2.Stop();
            th3.Stop();
            th4.Stop();
            server.Close();
            loop = false;
            break;
         }
         case 1:// register component
         {
            DBG_MSG(("tcpControllerAVA.register()\n"));
            tcpControllerAVA.registerController(0);
            DBG_MSG(("tcpControllerPhone.register()\n"));
            tcpControllerPhone.registerController(1);
            DBG_MSG(("tcpControllerBackend.register()\n"));
            tcpControllerBackend.registerController(2);
            break;
         }
         case 2:// external message
         {
            DBG_MSG(("tcpControllerAVA.Phone.Call()\n"));
            start = GetTickCount();
            tcpControllerAVA.makeCall("+380677639550");
            break;
         }
         case 3://subscribe
         {
            DBG_MSG(("tcpControllerAVA.subscribeTo()\n"));
            tcpControllerAVA.subscribeTo("Phone.onContactsUpdated");
            break;
         }
         case 4://notify
         {
            DBG_MSG(("tcpControllerPhone.onContactsUpdated()\n"));
            tcpControllerPhone.onContactsUpdated();
            break;
         }
         case 5: //unsubscribe
         {
            DBG_MSG(("tcpControllerAVA.unsubscribeFrom()\n"));
            tcpControllerAVA.unsubscribeFrom("Phone.onContactsUpdated");
            break;
         }
         case 6: //unregister
         {
            DBG_MSG(("tcpControllerPhone.unregister()\n"));
            tcpControllerPhone.unregisterController();
            break;
         }
         case 7: //stress test
         {
            
            DBG_MSG(("tcpControllerAVA.Phone.Call() 500 times\n"));
            start = GetTickCount();
            for (int c =0; c<1000; c++)
            {
               tcpControllerAVA.makeCall("+380677639550");
            }
            int stop = GetTickCount();
            int diff = stop - start;
            printf("Requests execution time is %d ms!\n", diff);
            break;
         }
         case 8: //parser test
         {
            DBG_MSG(("Parser test\n"));
            mpMessageBroker->Test();
            break;
         }
         default:
         {
            DBG_MSG(("Entered: %d\n", i));
            break;
         }
      }
   }
   return EXIT_SUCCESS;
}