feat: add heartbeat capability

This commit is contained in:
Mudit Vats
2021-03-23 14:00:49 -07:00
parent 5b5a480943
commit a8e1bc89f0
5 changed files with 80 additions and 1 deletions

View File

@@ -98,6 +98,8 @@ add_executable (rpc
commands.cpp commands.cpp
activation.h activation.h
activation.cpp activation.cpp
heartbeat.h
heartbeat.cpp
lms.h lms.h
lms.cpp lms.cpp
main.cpp main.cpp

View File

@@ -8,7 +8,7 @@
#include <string> #include <string>
#define PROTOCOL_VERSION "3.0.0" #define PROTOCOL_VERSION "4.0.0"
#ifdef _WIN32 #ifdef _WIN32
#define convertstring to_utf16string #define convertstring to_utf16string

48
heartbeat.cpp Normal file
View File

@@ -0,0 +1,48 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#include "heartbeat.h"
#include <cpprest/ws_client.h>
#include <cpprest/json.h>
#include <cpprest/streams.h>
#include <iostream>
#include <string>
#include "activation.h"
#include "version.h"
#include "commands.h"
#include "network.h"
#include "utils.h"
bool heartbeat_create_response(std::string& response)
{
web::json::value msg;
utility::string_t tmp = utility::conversions::convertstring("heartbeat_response");
msg[U("method")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("");
msg[U("apiKey")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROJECT_VER);
msg[U("appVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring(PROTOCOL_VERSION);
msg[U("protocolVersion")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("success");
msg[U("status")] = web::json::value::string(tmp);
tmp = utility::conversions::convertstring("");
msg[U("message")] = web::json::value::string(tmp);
// set empty payload
tmp = utility::conversions::convertstring("");
msg[U("payload")] = web::json::value::string(tmp);
// serialize the entire message
response = utility::conversions::to_utf8string(msg.serialize());
return true;
}

13
heartbeat.h Normal file
View File

@@ -0,0 +1,13 @@
/*********************************************************************
* Copyright (c) Intel Corporation 2019 - 2020
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
#ifndef __HEARTBEAT_H__
#define __HEARTBEAT_H__
#include <string>
bool heartbeat_create_response(std::string& response);
#endif

View File

@@ -11,6 +11,7 @@
#include "lms.h" #include "lms.h"
#include "commands.h" #include "commands.h"
#include "activation.h" #include "activation.h"
#include "heartbeat.h"
#include "utils.h" #include "utils.h"
#include "usage.h" #include "usage.h"
#include "args.h" #include "args.h"
@@ -246,6 +247,21 @@ int main(int argc, char* argv[])
return; return;
} }
if (msgMethod.compare("heartbeat_request") == 0)
{
// create the response
std::string response;
if (!heartbeat_create_response(response)) return;
// send it
web::websockets::client::websocket_outgoing_message send_websocket_msg;
std::string send_websocket_buffer(response);
send_websocket_msg.set_utf8_message(send_websocket_buffer);
client.send(send_websocket_msg).wait();
return;
}
// process any messages we can // process any messages we can
// - if success, done // - if success, done
// - if error, get out // - if error, get out