Browse Source

[2930] Sending notifications, C++ version

Michal 'vorner' Vaner 12 years ago
parent
commit
f3cd2b4939

+ 2 - 0
src/lib/cc/proto_defs.cc

@@ -43,6 +43,8 @@ const char* const CC_COMMAND_STOP = "stop";
 // The wildcards of some headers
 const char* const CC_TO_WILDCARD = "*";
 const char* const CC_INSTANCE_WILDCARD = "*";
+// Prefixes for groups
+const char* const CC_GROUP_NOTIFICATION_PREFIX = "notifications/";
 // Reply codes
 const int CC_REPLY_NO_RECPT = -1;
 const int CC_REPLY_SUCCESS = 0;

+ 16 - 0
src/lib/config/ccsession.cc

@@ -884,5 +884,21 @@ ModuleCCSession::rpcCall(const std::string &command, const std::string &group,
     }
 }
 
+void
+ModuleCCSession::notify(const std::string &group, const std::string &name,
+                        const ConstElementPtr &params)
+{
+    const ElementPtr message(Element::createMap());
+    const ElementPtr notification(Element::createList());
+    notification->add(Element::create(name));
+    if (params) {
+        notification->add(params);
+    }
+    message->set("notification", notification);
+    groupSendMsg(message, isc::cc::CC_GROUP_NOTIFICATION_PREFIX + group,
+                 isc::cc::CC_INSTANCE_WILDCARD,
+                 isc::cc::CC_TO_WILDCARD, false);
+}
+
 }
 }

+ 22 - 0
src/lib/config/tests/ccsession_unittests.cc

@@ -117,6 +117,28 @@ TEST_F(CCSessionTest, rpcNoRecpt) {
                  RPCRecipientMissing);
 }
 
+// Test sending a notification
+TEST_F(CCSessionTest, notify) {
+    ModuleCCSession mccs(ccspecfile("spec1.spec"), session, NULL, NULL, false,
+                         false);
+    mccs.notify("group", "event", el("{\"param\": true}"));
+    const ConstElementPtr notification(el(
+        "["
+        "   \"notifications/group\","
+        "   \"*\","
+        "   {"
+        "       \"notification\": ["
+        "           \"event\", {"
+        "               \"param\": true"
+        "           }"
+        "       ]"
+        "   },"
+        "   -1"
+        "]"));
+    EXPECT_TRUE(notification->equals(*session.getMsgQueue()->get(1))) <<
+            session.getMsgQueue()->get(1)->toWire();
+}
+
 TEST_F(CCSessionTest, createAnswer) {
     ConstElementPtr answer;
     answer = createAnswer();