proto_2_json.cc 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <google/protobuf/message.h>
  2. #include <google/protobuf/util/json_util.h>
  3. #include "proto/alarm/v1/config.pb.h"
  4. #include "proto/alarm/v1/stream.pb.h"
  5. #include "proto/aclk/v1/lib.pb.h"
  6. #include "proto/agent/v1/connection.pb.h"
  7. #include "proto/agent/v1/disconnect.pb.h"
  8. #include "proto/nodeinstance/connection/v1/connection.pb.h"
  9. #include "proto/nodeinstance/create/v1/creation.pb.h"
  10. #include "proto/nodeinstance/info/v1/info.pb.h"
  11. #include "proto/context/v1/stream.pb.h"
  12. #include "proto/context/v1/context.pb.h"
  13. #include "proto/agent/v1/cmds.pb.h"
  14. #include "libnetdata/libnetdata.h"
  15. #include "proto_2_json.h"
  16. using namespace google::protobuf::util;
  17. static google::protobuf::Message *msg_name_to_protomsg(const char *msgname)
  18. {
  19. //tx side
  20. if (!strcmp(msgname, "UpdateAgentConnection"))
  21. return new agent::v1::UpdateAgentConnection;
  22. if (!strcmp(msgname, "UpdateNodeInstanceConnection"))
  23. return new nodeinstance::v1::UpdateNodeInstanceConnection;
  24. if (!strcmp(msgname, "CreateNodeInstance"))
  25. return new nodeinstance::create::v1::CreateNodeInstance;
  26. if (!strcmp(msgname, "UpdateNodeInfo"))
  27. return new nodeinstance::info::v1::UpdateNodeInfo;
  28. if (!strcmp(msgname, "AlarmCheckpoint"))
  29. return new alarms::v1::AlarmCheckpoint;
  30. if (!strcmp(msgname, "ProvideAlarmConfiguration"))
  31. return new alarms::v1::ProvideAlarmConfiguration;
  32. if (!strcmp(msgname, "AlarmSnapshot"))
  33. return new alarms::v1::AlarmSnapshot;
  34. if (!strcmp(msgname, "AlarmLogEntry"))
  35. return new alarms::v1::AlarmLogEntry;
  36. if (!strcmp(msgname, "UpdateNodeCollectors"))
  37. return new nodeinstance::info::v1::UpdateNodeCollectors;
  38. if (!strcmp(msgname, "ContextsUpdated"))
  39. return new context::v1::ContextsUpdated;
  40. if (!strcmp(msgname, "ContextsSnapshot"))
  41. return new context::v1::ContextsSnapshot;
  42. //rx side
  43. if (!strcmp(msgname, "CreateNodeInstanceResult"))
  44. return new nodeinstance::create::v1::CreateNodeInstanceResult;
  45. if (!strcmp(msgname, "SendNodeInstances"))
  46. return new agent::v1::SendNodeInstances;
  47. if (!strcmp(msgname, "StartAlarmStreaming"))
  48. return new alarms::v1::StartAlarmStreaming;
  49. if (!strcmp(msgname, "SendAlarmCheckpoint"))
  50. return new alarms::v1::SendAlarmCheckpoint;
  51. if (!strcmp(msgname, "SendAlarmConfiguration"))
  52. return new alarms::v1::SendAlarmConfiguration;
  53. if (!strcmp(msgname, "SendAlarmSnapshot"))
  54. return new alarms::v1::SendAlarmSnapshot;
  55. if (!strcmp(msgname, "DisconnectReq"))
  56. return new agent::v1::DisconnectReq;
  57. if (!strcmp(msgname, "ContextsCheckpoint"))
  58. return new context::v1::ContextsCheckpoint;
  59. if (!strcmp(msgname, "StopStreamingContexts"))
  60. return new context::v1::StopStreamingContexts;
  61. if (!strcmp(msgname, "CancelPendingRequest"))
  62. return new agent::v1::CancelPendingRequest;
  63. return NULL;
  64. }
  65. char *protomsg_to_json(const void *protobin, size_t len, const char *msgname)
  66. {
  67. google::protobuf::Message *msg = msg_name_to_protomsg(msgname);
  68. if (msg == NULL)
  69. return strdupz("Don't know this message type by name.");
  70. if (!msg->ParseFromArray(protobin, len))
  71. return strdupz("Can't parse this message. Malformed or wrong parser used.");
  72. JsonPrintOptions options;
  73. std::string output;
  74. google::protobuf::util::MessageToJsonString(*msg, &output, options);
  75. delete msg;
  76. return strdupz(output.c_str());
  77. }