summaryrefslogtreecommitdiff
path: root/src/3rd_party-static/MessageBroker/src/lib_messagebroker/CMessageBroker.cpp
blob: 07f01ea128a79b623c40a9f04e2d91580893fbee (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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/**
 * \file CMessageBroker.cpp
 * \brief CMessageBroker singletone class implementation.
 * \author AKara
 */

#include <stdio.h>
#include <vector>

#include <string>

#include "CMessageBroker.hpp"
#include "CMessageBrokerRegistry.hpp"

#include "system.h"

#include "json/json.h"

#include "libMBDebugHelper.h"

namespace NsMessageBroker {
/**
 * \class CMessage
 * \brief CMessage class implementation.
 */
class CMessage {
  public:
    /**
     * \brief Constructor.
     */
    CMessage(int aSenderFp, Json::Value aMessage) {
      mSenderFd = aSenderFp;
      mMessage = aMessage;
    }

    /**
     * \brief Destructor.
     */
    ~CMessage() {
    }

    /**
     * \brief getter for Json::Value message.
     * \return Json::Value message.
     */
    Json::Value getMessage() const {
      return mMessage;
    }

    /**
     * \brief getter for sender FileDescriptor.
     * \return sender FileDescriptor.
     */
    int getSenderFd() const {
      return mSenderFd;
    }
  private:
    /**
     * \brief sender FileDescriptor.
     */
    int mSenderFd;

    /**
     * \brief Json::Value message.
     */
    Json::Value mMessage;
};


class CMessageBroker_Private {
  public:
    /**
     * \brief Constructor.
     */
    CMessageBroker_Private();

    /**
     * \brief Check if que empty (Thread safe).
     * \return True when empty.
     */
    bool isEventQueueEmpty();

    /**
     * \brief Pop message from que (Thread safe).
     * \return Pointer to CMessage.
     */
    CMessage* popMessage();

    /**
     * \brief Push message to que (Thread safe).
     * \param pMessage pointer to new CMessage object.
     */
    void pushMessage(CMessage* pMessage);

    /**
     * \brief gets  destination component name.
     * \param pMessage JSON message.
     * \return string destination component name.
     */
    std::string getDestinationComponentName(CMessage* pMessage);

    /**
     * \brief gets  method name.
     * \param pMessage JSON message.
     * \return string method name.
     */
    std::string getMethodName(CMessage* pMessage);

    /**
     * \brief checks is message notification or not.
     * \param pMessage JSON message.
     * \return true if notification.
     */
    bool isNotification(CMessage* pMessage);

    /**
     * \brief checks is message response or not.
     * \param pMessage JSON message.
     * \return true if response.
     */
    bool isResponse(CMessage* pMessage);

    /**
     * \brief checks message.
     * \param pMessage JSON message.
     * \param error JSON message to fill in case of any errors.
     * \return true if message is good.
     */
    bool checkMessage(CMessage* pMessage, Json::Value& error);

    /**
     * \brief Process internal MessageBrocker message
     *
     * \brief Register controller in MessageBroker.
     *  Use following JSON command to register new component:
     * \code
     *  {"jsonrpc": "2.0", "method": "MB.registerComponent", "params": "<ComponentName>"}
     * \endcode
     *
     * \brief Unregister controller in MessageBroker.
     *  Use following JSON command to unregister component:
     * \code
     *  {"jsonrpc": "2.0", "method": "MB.unregisterComponent", "params": "<ComponentName>"}
     * \endcode
     *
     * \brief Subscribe controller on property change.
     *  Use following JSON command to subscribe to notifications:
     * \code
     *  {"jsonrpc": "2.0", "method": "MB.subscribeTo", "params": "<ComponentName>.<NotificationName>"}
     * \endcode
     *
     * \brief Unsubscribe controller from property change.
     *  Use following JSON command to unsubscribe from notifications:
     * \code
     *  {"jsonrpc": "2.0", "method": "MB.unsubscribeFrom", "params": "<ComponentName>.<NotificationName>"}
     * \endcode
     *
     * \param pMessage JSON message.
     */
    void processInternalMessage(CMessage* pMessage);

    /**
     * \brief process external message.
     * \param pMessage JSON message.
     */
    void processExternalMessage(CMessage* pMessage);

    /**
     * \brief process response.
     * \param pMessage JSON message.
     */
    void processResponse(CMessage* pMessage);

    /**
     * \brief Process notification message.
     * \brief Notify subscribers about property change.
     * expected notification format example:
     * \code
     * {"jsonrpc": "2.0", "method": "<ComponentName>.<NotificationName>", "params": <list of params>}
     * \endcode
     * \param pMessage JSON message.
     */
    void processNotification(CMessage* pMessage);

    /**
     * \brief send error message.
     * \param pMessage JSON message.
     */
    void processError(CMessage* pMessage);

    /**
     * \brief send Json message.
     * \param fd FileDescriptor of socket.
     * \param message JSON message.
     */
    void sendJsonMessage(int fd, Json::Value message);

    /**
     * \brief push message to wait response que.
     * \param pMessage JSON message.
     */
    void pushMessageToWaitQue(CMessage* pMessage);

    /**
     * \brief Returns start position for Id's generator of controller.
     * \return start position for Id's generator of controller (1000 id's).
     */
    int getNextControllerIdDiapason() {
      return 1000 * mControllersIdCounter++;
    }

    /**
     * \brief pop message from wait response que.
     * \param pMessage JSON message.
     */
    int popMessageFromWaitQue(CMessage* pMessage);

    /**
     * \brief Que of messages.
     */
    std::deque<CMessage*> mMessagesQueue;

    /**
     * \brief Counter of messages Id's diapason for the next controllers
     * From mControllersIdCounter*1000 to mControllersIdCounter*1000+999.
     */
    int mControllersIdCounter;

    /**
     * \brief Que of messages which are waiting the response in format: MessageId:SenderFd.
     */
    std::map<int, int> mWaitResponseQueue;

    /**
     * \brief Pointer to sender.
     */
    CSender* mpSender;

    /**
     * \brief Pointer to registry.
     */
    CMessageBrokerRegistry* mpRegistry;

    /**
     * \brief JSON reader.
     */
    Json::Reader m_reader;

    /**
     * \brief JSON writer.
     */
    Json::FastWriter m_writer;

    /**
     * \brief JSON writer for receiver.
     */
    Json::FastWriter m_recieverWriter;

    /**
     * \brief Messages que mutex.
     */
    System::Mutex mMessagesQueueMutex;

    /**
     * \brief Binary semaphore that is used to notify the
     * messaging thread that a new message is available.
     */
    System::BinarySemaphore m_messageQueueSemaphore;
};

CMessageBroker_Private::CMessageBroker_Private() :
  mControllersIdCounter(1),
  mpSender(NULL) {
  mpRegistry = CMessageBrokerRegistry::getInstance();
}


CMessageBroker::CMessageBroker() :
  p(new CMessageBroker_Private()) {
}

CMessageBroker::~CMessageBroker() {
  delete p, p = 0;
}

CMessageBroker* CMessageBroker::getInstance() {
  static CMessageBroker instance;
  return &instance;
}

void CMessageBroker::onMessageReceived(int fd, std::string& aJSONData) {
  DBG_MSG(("CMessageBroker::onMessageReceived()\n"));
  while (!aJSONData.empty()) {
    Json::Value root;
    if (!p->m_reader.parse(aJSONData, root)) {
      DBG_MSG(("Received not JSON string! %s\n", aJSONData.c_str()));
      return;
    }
    if(root["jsonrpc"]!="2.0") {
      DBG_MSG(("\t Json::Reader::parce didn't set up jsonrpc!  jsonrpc = '%s'\n", root["jsonrpc"].asString().c_str()));
      return;
    }
    std::string wmes = p->m_recieverWriter.write(root);
    DBG_MSG(("Parsed JSON string %d : %s\n", wmes.length(),
             wmes.c_str()));
    DBG_MSG(("Buffer is:%s\n", aJSONData.c_str()));
    if (aJSONData.length() > wmes.length()) {
      // wmes string length can differ from buffer substr length
      size_t offset = wmes.length();
      char msg_begin = '{';
      if (aJSONData.at(offset) != msg_begin) {
        offset -= 1; // wmes can contain redudant \n in the tail.
      }
      aJSONData.erase(aJSONData.begin(), aJSONData.begin() + offset);
      DBG_MSG(("Buffer after cut is:%s\n", aJSONData.c_str()));
    } else {
      aJSONData = "";
    }
    p->pushMessage(new CMessage(fd, root));
  }
}

void CMessageBroker::Test() {
  Json::Value root, err;
  std::string ReceivingBuffer =
    "{\"id\":0,\"jsonrpc\":\"2.0\",\"method\":\"MB.registerComponent\",\"params\":{\"componentName\":\"AVA\"}}123{\"id\":0,\"jsonrpc\":\"2.0\",\"method\":\"MB.registerComponent\",\"params\":{\"componentName\":\"AVA\"}}";
  DBG_MSG(("String is:%s\n", ReceivingBuffer.c_str()));
  while (1) {
    if (!p->m_reader.parse(ReceivingBuffer, root)) {
      DBG_MSG_ERROR(("Received not JSON string! %s\n", ReceivingBuffer.c_str()));
      return;
    }
    std::string wmes = p->m_recieverWriter.write(root);
    DBG_MSG(("Parsed JSON string:%s; length: %d\n", wmes.c_str(), wmes.length()));
    DBG_MSG(("Buffer is:%s\n", ReceivingBuffer.c_str()));
    ssize_t beginpos = ReceivingBuffer.find(wmes);
    ReceivingBuffer.erase(0, beginpos + wmes.length());
    DBG_MSG(("Buffer after cut is:%s\n", ReceivingBuffer.c_str()));
    CMessage message(0, root);
    if (p->checkMessage(&message, err)) {
      //here put message to que
    } else {
      DBG_MSG_ERROR(("Wrong message:%s\n", wmes.c_str()));
    }
  }
}

void CMessageBroker::OnSocketClosed(const int fd) {
  DBG_MSG(("CMessageBroker::OnSocketClosed(%d)\n", fd));
  if (p->mpRegistry) {
    p->mpRegistry->removeControllersByDescriptor(fd);
  }
}

void CMessageBroker::startMessageBroker(CSender* pSender) {
  DBG_MSG(("CMessageBroker::startMessageBroker()\n"));
  p->mpSender = pSender;
}

void CMessageBroker::stopMessageBroker() {
  p->mpSender = NULL;
  DBG_MSG(("CMessageBroker::stopMessageBroker()\n"));
}

CMessage* CMessageBroker_Private::popMessage() {
  CMessage* ret = NULL;
  DBG_MSG(("CMessageBroker::popMessage()\n"));
  mMessagesQueueMutex.Lock();
  if (false == mMessagesQueue.empty()) {
    ret = mMessagesQueue.front();
    mMessagesQueue.pop_front();// delete message from que
  } else {
    DBG_MSG(("Que is empty!\n"));
  }
  mMessagesQueueMutex.Unlock();
  return ret;
}

void CMessageBroker_Private::pushMessage(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::pushMessage()\n"));
  mMessagesQueueMutex.Lock();
  if (pMessage) {
    mMessagesQueue.push_back(pMessage);
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
  mMessagesQueueMutex.Unlock();

  m_messageQueueSemaphore.Notify();
}

bool CMessageBroker_Private::isEventQueueEmpty() {
  bool bResult = true;
  mMessagesQueueMutex.Lock();
  bResult = mMessagesQueue.empty();
  mMessagesQueueMutex.Unlock();
  return bResult;
}

std::string CMessageBroker_Private::getDestinationComponentName(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::getDestinationComponentName()\n"));
  std::string ret = "";
  if (pMessage) {
    Json::Value mes = pMessage->getMessage();
    std::string method = mes["method"].asString();
    int pos = method.find(".");
    if (-1 != pos) {
      ret = method.substr(0, pos);
    }
    DBG_MSG(("Destination component is: %s\n", ret.c_str()));
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
  return ret;
}

std::string CMessageBroker_Private::getMethodName(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::getMethodName()\n"));
  std::string ret = "";
  if (pMessage) {
    Json::Value mes = pMessage->getMessage();
    std::string method = mes["method"].asString();
    int pos = method.find(".");
    if (-1 != pos) {
      ret = method.substr(pos + 1);
    }
    DBG_MSG(("Method is: %s\n", ret.c_str()));
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
  return ret;
}

bool CMessageBroker_Private::isNotification(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::isNotification()\n"));
  bool ret = false;
  Json::Value mes = pMessage->getMessage();
  if (false == mes.isMember("id")) {
    ret = true;
  }
  DBG_MSG(("Result: %d\n", ret));
  return ret;
}

bool CMessageBroker_Private::isResponse(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::isResponse()\n"));
  bool ret = false;
  Json::Value mes = pMessage->getMessage();
  if ((true == mes.isMember("result")) || (true == mes.isMember("error"))) {
    ret = true;
  }
  DBG_MSG(("Result: %d\n", ret));
  return ret;
}

void CMessageBroker_Private::pushMessageToWaitQue(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::pushMessageToWaitQue()\n"));
  if (pMessage) {
    Json::Value root = pMessage->getMessage();
    mWaitResponseQueue.insert(std::map<int, int>::value_type(root["id"].asInt(), pMessage->getSenderFd()));
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
}

int CMessageBroker_Private::popMessageFromWaitQue(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::popMessageFromWaitQue()\n"));
  int result = -1;
  if (pMessage) {
    Json::Value root = pMessage->getMessage();
    int messageId = root["id"].asInt();
    std::map <int, int>::iterator it;
    it = mWaitResponseQueue.find(messageId);
    if (it != mWaitResponseQueue.end()) {
      result = (*it).second;
      mWaitResponseQueue.erase(it);
    }
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
  DBG_MSG(("Senders Fd: %d\n", result));
  return result;
}

void CMessageBroker_Private::processInternalMessage(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::processInternalMessage()\n"));
  if (pMessage) {
    std::string amethodName = getMethodName(pMessage);
    DBG_MSG(("Method: %s\n", amethodName.c_str()));
    Json::Value root = pMessage->getMessage();
    if ("registerComponent" == amethodName) {
      Json::Value params = root["params"];
      if (params.isMember("componentName") && params["componentName"].isString()) {
        std::string controllerName = params["componentName"].asString();
        if (mpRegistry->addController(pMessage->getSenderFd(), controllerName)) {
          Json::Value response;
          response["id"] = root["id"];
          response["jsonrpc"] = "2.0";
          response["result"] = getNextControllerIdDiapason();
          sendJsonMessage(pMessage->getSenderFd(), response);
        } else {
          Json::Value error, err;
          error["id"] = root["id"];
          error["jsonrpc"] = "2.0";
          err["code"] = CONTROLLER_EXISTS;
          err["message"] = "Controller has been already registered.";
          error["error"] = err;
          processError(new CMessage(pMessage->getSenderFd(), error));
        }
      } else {
        Json::Value error, err;
        error["id"] = root["id"];
        error["jsonrpc"] = "2.0";
        err["code"] = INVALID_REQUEST;
        err["message"] = "Wrong method parameter.";
        error["error"] = err;
        processError(new CMessage(pMessage->getSenderFd(), error));
      }
    } else if ("subscribeTo" == amethodName) {
      Json::Value params = root["params"];
      if (params.isMember("propertyName") && params["propertyName"].isString()) {
        std::string propertyName = params["propertyName"].asString();
        if (mpRegistry->addSubscriber(pMessage->getSenderFd(), propertyName)) {
          Json::Value response;
          response["id"] = root["id"];
          response["jsonrpc"] = "2.0";
          response["result"] = "OK";
          sendJsonMessage(pMessage->getSenderFd(), response);
        } else {
          Json::Value error, err;
          error["id"] = root["id"];
          error["jsonrpc"] = "2.0";
          err["code"] = CONTROLLER_EXISTS;
          err["message"] = "Subscribe has been already registered.";
          error["error"] = err;
          processError(new CMessage(pMessage->getSenderFd(), error));
        }
      } else {
        Json::Value error, err;
        error["id"] = root["id"];
        error["jsonrpc"] = "2.0";
        err["code"] = INVALID_REQUEST;
        err["message"] = "Wrong method parameter.";
        error["error"] = err;
        processError(new CMessage(pMessage->getSenderFd(), error));
      }
    } else if ("unregisterComponent" == amethodName) {
      Json::Value params = root["params"];
      if (params.isMember("componentName") && params["componentName"].isString()) {
        std::string controllerName = params["componentName"].asString();
        mpRegistry->deleteController(controllerName);
        Json::Value response;
        response["id"] = root["id"];
        response["jsonrpc"] = "2.0";
        response["result"] = "OK";
        sendJsonMessage(pMessage->getSenderFd(), response);
      } else {
        Json::Value error, err;
        error["id"] = root["id"];
        error["jsonrpc"] = "2.0";
        err["code"] = INVALID_REQUEST;
        err["message"] = "Wrong method parameter.";
        error["error"] = err;
        processError(new CMessage(pMessage->getSenderFd(), error));
      }
    } else if ("unsubscribeFrom" == amethodName) {
      Json::Value params = root["params"];
      if (params.isMember("propertyName") && params["propertyName"].isString()) {
        std::string propertyName = params["propertyName"].asString();
        mpRegistry->deleteSubscriber(pMessage->getSenderFd(), propertyName);
        Json::Value response;
        response["id"] = root["id"];
        response["jsonrpc"] = "2.0";
        response["result"] = "OK";
        sendJsonMessage(pMessage->getSenderFd(), response);
      } else {
        Json::Value error, err;
        error["id"] = root["id"];
        error["jsonrpc"] = "2.0";
        err["code"] = INVALID_REQUEST;
        err["message"] = "Wrong method parameter.";
        error["error"] = err;
        processError(new CMessage(pMessage->getSenderFd(), error));
      }
    } else {
      DBG_MSG(("Unknown method!\n"));
      Json::Value error;
      Json::Value err;
      error["id"] = root["id"];
      error["jsonrpc"] = "2.0";
      err["code"] = INVALID_REQUEST;
      err["message"] = "Invalid MessageBroker method.";
      error["error"] = err;
      processError(new CMessage(pMessage->getSenderFd(), error));
    }
  } else {
    DBG_MSG_ERROR(("NULL pointer!\n"));
  }
}

void CMessageBroker_Private::processExternalMessage(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::processExternalMessage()\n"));
  if (pMessage) {
    std::string destComponentName = getDestinationComponentName(pMessage);
    int destFd = mpRegistry->getDestinationFd(destComponentName);
    Json::Value root = pMessage->getMessage();
    if (0 < destFd) {
      sendJsonMessage(destFd, root);
      pushMessageToWaitQue(pMessage);
    } else {
      // error, controller not found in the registry
      DBG_MSG(("Unknown method!\n"));
      Json::Value error;
      Json::Value err;
      Json::Value error_data;
      error["id"] = root["id"];
      error["jsonrpc"] = "2.0";
      err["code"] = UNSUPPORTED_RESOURCE;
      err["message"] = "Destination controller not found!";
      error_data["method"] = root["method"];
      err["data"] = error_data;
      error["error"] = err;
      processError(new CMessage(pMessage->getSenderFd(), error));
    }
  } else {
    DBG_MSG_ERROR(("NULL pointer\n"));
  }
}

void CMessageBroker_Private::processResponse(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::processResponse()\n"));
  if (pMessage) {
    int senderFd = popMessageFromWaitQue(pMessage);
    if (-1 != senderFd) {
      sendJsonMessage(senderFd, pMessage->getMessage());
    }
  } else {
    DBG_MSG_ERROR(("NULL pointer\n"));
  }
}

void CMessageBroker_Private::processNotification(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::processNotification()\n"));
  if (pMessage) {
    Json::Value root = pMessage->getMessage();
    std::string methodName = root["method"].asString();
    DBG_MSG(("Property: %s\n", methodName.c_str()));
    std::vector<int> result;
    int subscribersCount = mpRegistry->getSubscribersFd(methodName, result);
    if (0 < subscribersCount) {
      std::vector<int>::iterator it;
      for (it = result.begin(); it != result.end(); it++) {
        sendJsonMessage(*it, root);
      }
    } else {
      DBG_MSG(("No subscribers for this property!\n"));
    }
  } else {
    DBG_MSG_ERROR(("NULL pointer\n"));
  }
}

void CMessageBroker_Private::processError(CMessage* pMessage) {
  DBG_MSG(("CMessageBroker::processError()\n"));
  if (pMessage) {
    sendJsonMessage(pMessage->getSenderFd(), pMessage->getMessage());
    delete pMessage;// delete CMessage object with error description!!!
  } else {
    DBG_MSG_ERROR(("NULL pointer\n"));
  }
}

void CMessageBroker_Private::sendJsonMessage(int fd, Json::Value message) {
  DBG_MSG(("CMessageBroker::sendJsonMessage(%d)\n", fd));
  if (mpSender) {
    std::string mes = m_writer.write(message);
    int retVal = mpSender->Send(fd, mes);
    if (retVal == -1) {
      DBG_MSG_ERROR(("Message hasn't been sent!\n"));
      return;
    }
    DBG_MSG(("Length:%d, Sent: %d bytes\n", mes.length(), retVal));
  } else {
    DBG_MSG_ERROR(("mpSender NULL pointer\n"));
  }
}

void* CMessageBroker::MethodForThread(void* arg) {
  arg = arg; // to avoid compiler warnings
  while (1) {
    while (!p->isEventQueueEmpty()) {
      CMessage* message = p->popMessage();
      if (message) {
        Json::Value error;
        if (p->checkMessage(message, error)) {
          if (p->isNotification(message)) {
            DBG_MSG(("Message is notification!\n"));
            p->processNotification(message);
          } else if (p->isResponse(message)) {
            DBG_MSG(("Message is response!\n"));
            p->processResponse(message);
          } else {
            if ("MB" == p->getDestinationComponentName(message)) {
              DBG_MSG(("Internal MessageBroker method!\n"));
              p->processInternalMessage(message);
            } else {
              DBG_MSG(("Not MessageBroker method!\n"));
              p->processExternalMessage(message);
            }
          }
        } else {
          DBG_MSG_ERROR(("Message contains wrong data!\n"));
          CMessage* errMessage = new CMessage(message->getSenderFd(), error);
          if (NULL != errMessage) {
            p->processError(errMessage);
          } else {
            DBG_MSG_ERROR(("NULL pointer!\n"));
          }
        }
        delete message;// delete message object
      }
    }
    p->m_messageQueueSemaphore.Wait();
  }

  return NULL;
}

bool CMessageBroker_Private::checkMessage(CMessage* pMessage, Json::Value& error) {
  DBG_MSG(("CMessageBroker::checkMessage()\n"));
  Json::Value root;
  root = pMessage->getMessage();
  Json::Value err;

  /* check the JSON-RPC version => 2.0 */
  if (!root.isObject() || !root.isMember("jsonrpc") || root["jsonrpc"] != "2.0") {
    error["id"] = Json::Value::null;
    error["jsonrpc"] = "2.0";
    err["code"] = INVALID_REQUEST;
    err["message"] = "Invalid JSON RPC version.";
    error["error"] = err;
    return false;
  }

  /* Check the id of message */
  if (root.isMember("id") && (root["id"].isArray() || root["id"].isObject() || root["id"].isString())) {
    error["id"] = Json::Value::null;
    error["jsonrpc"] = "2.0";
    err["code"] = INVALID_REQUEST;
    err["message"] = "Invalid ID of message.";
    error["error"] = err;
    return false;
  }

  /* extract "method" attribute */
  if (root.isMember("method")) {
    if (!root["method"].isString()) {
      error["id"] = Json::Value::null;
      error["jsonrpc"] = "2.0";
      err["code"] = INVALID_REQUEST;
      err["message"] = "Invalid JSONRPC method.";
      error["error"] = err;
      return false;
    }
    /* Check the params is  an object*/
    if (root.isMember("params") && !root["params"].isObject()) {
      error["id"] = Json::Value::null;
      error["jsonrpc"] = "2.0";
      err["code"] = INVALID_REQUEST;
      err["message"] = "Invalid JSONRPC params.";
      error["error"] = err;
      return false;
    }
  } else if (!(root.isMember("result") || root.isMember("error"))) {
    error["id"] = Json::Value::null;
    error["jsonrpc"] = "2.0";
    err["code"] = INVALID_REQUEST;
    err["message"] = "Unknwn message type.";
    error["error"] = err;
    return false;
  }
  return true;
}
} /* namespace NsMessageBroker */