Commit f134760d by 李维杰

增加新请求

parent ff491e70
...@@ -10,6 +10,8 @@ namespace offcn ...@@ -10,6 +10,8 @@ namespace offcn
CmdProcesserClient::CmdProcesserClient() CmdProcesserClient::CmdProcesserClient()
:mqtt_wrapper_(NULL) :mqtt_wrapper_(NULL)
{ {
local_id_ = kLocalID;
mqtt_wrapper_ = new offcn::MqttWrapper(this); mqtt_wrapper_ = new offcn::MqttWrapper(this);
} }
CmdProcesserClient::~CmdProcesserClient() CmdProcesserClient::~CmdProcesserClient()
...@@ -21,13 +23,22 @@ namespace offcn ...@@ -21,13 +23,22 @@ namespace offcn
} }
} }
void CmdProcesserClient::SetLocalID(std::string id)
{
local_id_ = id;
}
bool CmdProcesserClient::Connect() bool CmdProcesserClient::Connect()
{ {
return mqtt_wrapper_->ConnectMqttServer(kLocalID); return mqtt_wrapper_->ConnectMqttServer(local_id_);
} }
bool CmdProcesserClient::SubscribeTopic() bool CmdProcesserClient::SubscribeTopic()
{ {
return mqtt_wrapper_->SubscribeTopic(kLocalID); return mqtt_wrapper_->SubscribeTopic(local_id_);
}
bool CmdProcesserClient::SendLocalCandidate(std::string tid, std::string type, std::string candidate)
{
std::string req = MqttRequest::Candidate(local_id_, tid, type, candidate);
return mqtt_wrapper_->SendRequest(tid, req);
} }
/** /**
...@@ -47,7 +58,7 @@ namespace offcn ...@@ -47,7 +58,7 @@ namespace offcn
printf("subscribe topic success!\n"); printf("subscribe topic success!\n");
std::string topic = kGlobalTopic; std::string topic = kGlobalTopic;
std::string req = MqttRequest::JoinRequest(kLocalID);; std::string req = MqttRequest::JoinRequest(local_id_);;
mqtt_wrapper_->SendRequest(topic, req); mqtt_wrapper_->SendRequest(topic, req);
} }
...@@ -57,6 +68,22 @@ namespace offcn ...@@ -57,6 +68,22 @@ namespace offcn
} }
void CmdProcesserClient::OnServerMessageArrived(std::string topic, std::string message) void CmdProcesserClient::OnServerMessageArrived(std::string topic, std::string message)
{ {
std::string path;
ReqValue values;
if (!MqttRequest::ParseResponse(message, path, values))
{
printf("Error : Parse request error\n");
return;
}
if (path.compare("join") == 0)
{
std::string ids = values["ids"];
std::string result = values["result"];
if (ids.length() > 0)
{
publisher_list_.insert(std::pair<std::string, std::string>(ids, ids));
}
}
} }
} }
\ No newline at end of file
#pragma once #pragma once
#include "mqtt_wrapper.h" #include "mqtt_wrapper.h"
#include <map>
namespace offcn namespace offcn
{ {
...@@ -11,8 +12,13 @@ namespace offcn ...@@ -11,8 +12,13 @@ namespace offcn
~CmdProcesserClient(); ~CmdProcesserClient();
public: public:
void SetLocalID(std::string id);
bool Connect(); bool Connect();
bool SubscribeTopic(); bool SubscribeTopic();
/**
* @param type offer/answer
*/
bool SendLocalCandidate(std::string tid, std::string type, std::string candidate);
public: public:
/** /**
...@@ -26,5 +32,10 @@ namespace offcn ...@@ -26,5 +32,10 @@ namespace offcn
private: private:
MqttWrapper *mqtt_wrapper_; MqttWrapper *mqtt_wrapper_;
private:
std::map<std::string, std::string> publisher_list_;
std::map<std::string, std::string> subscriber_list_;
private:
std::string local_id_;
}; };
} }
\ No newline at end of file
...@@ -115,6 +115,23 @@ namespace offcn ...@@ -115,6 +115,23 @@ namespace offcn
return true; return true;
} }
bool MqttRequest::ParseResponse(std::string &response, std::string &path, ReqValue &values)
{
Json::Value root;
if (!GetJsonValue(response, root)) return false;
if (!root["path"] || !root["fid"]) return false;
path = root["path"].asString();
std::string fid = root["fid"].asString();
if (path.compare("join") == 0)
{
values["ids"] = root["ids"].asString();
values["result"] = root["result"].asString();
}
return true;
}
std::string MqttRequest::JoinResponse(std::string ids, std::string result) std::string MqttRequest::JoinResponse(std::string ids, std::string result)
{ {
......
...@@ -15,6 +15,7 @@ namespace offcn ...@@ -15,6 +15,7 @@ namespace offcn
static std::string Candidate(std::string local_id, std::string remote_id, std::string type, std::string candidate); static std::string Candidate(std::string local_id, std::string remote_id, std::string type, std::string candidate);
static bool ParseRequest(std::string &req, std::string &path, ReqValue &values); static bool ParseRequest(std::string &req, std::string &path, ReqValue &values);
static bool ParseResponse(std::string &response, std::string &path, ReqValue &values);
static std::string JoinResponse(std::string ids, std::string result); static std::string JoinResponse(std::string ids, std::string result);
static std::string UpdateResponse(std::string result); static std::string UpdateResponse(std::string result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment