Commit 797008cd authored by legoc's avatar legoc
Browse files

(split) Re-added some strings classes to prepare JSON argument info

parent 9fad2883
...@@ -28,8 +28,10 @@ std::vector<std::string> split(const std::string& str, char c); ...@@ -28,8 +28,10 @@ std::vector<std::string> split(const std::string& str, char c);
class Endpoint { class Endpoint {
public: public:
Endpoint(const std::string& protocol, const std::string& address, int port);
Endpoint(const std::string& address, int port); Endpoint(const std::string& address, int port);
const std::string& getProtocol() const;
const std::string& getAddress() const; const std::string& getAddress() const;
int getPort() const; int getPort() const;
...@@ -38,10 +40,45 @@ public: ...@@ -38,10 +40,45 @@ public:
std::string toString() const; std::string toString() const;
private: private:
std::string m_protocol;
std::string m_address; std::string m_address;
int m_port; int m_port;
}; };
class ApplicationIdentity {
public:
ApplicationIdentity(const std::string& name, int id, const Endpoint& endpoint);
ApplicationIdentity(const std::string& name, const Endpoint& endpoint);
const std::string& getName() const;
const std::optional<int>& getId() const;
const Endpoint& getEndpoint() const;
std::string toJSONString() const;
private:
std::string m_name;
std::optional<int> m_id;
Endpoint m_endpoint;
};
class ApplicationWithStarterIdentity {
public:
ApplicationWithStarterIdentity(const ApplicationIdentity& application, const ApplicationIdentity& starter);
ApplicationWithStarterIdentity(const ApplicationIdentity& application);
const ApplicationIdentity& getApplication() const;
const std::optional<ApplicationIdentity>& getStarter() const;
std::string toJSONString() const;
private:
ApplicationIdentity m_application;
std::optional<ApplicationIdentity> m_starter;
};
} }
#endif #endif
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
#include "Strings.h" #include "Strings.h"
#include "JSON.h"
#include "message/Message.h"
#include "BadFormatException.h" #include "BadFormatException.h"
#include <regex> #include <regex>
...@@ -38,11 +40,22 @@ std::vector<std::string> split(const std::string& str, char c) { ...@@ -38,11 +40,22 @@ std::vector<std::string> split(const std::string& str, char c) {
return result; return result;
} }
Endpoint::Endpoint(const std::string& protocol, const std::string& address, int port) {
m_protocol = protocol;
m_address = address;
m_port = port;
}
Endpoint::Endpoint(const std::string& address, int port) { Endpoint::Endpoint(const std::string& address, int port) {
m_protocol = "tcp";
m_address = address; m_address = address;
m_port = port; m_port = port;
} }
const std::string& Endpoint::getProtocol() const {
return m_protocol;
}
const std::string& Endpoint::getAddress() const { const std::string& Endpoint::getAddress() const {
return m_address; return m_address;
} }
...@@ -53,32 +66,130 @@ int Endpoint::getPort() const { ...@@ -53,32 +66,130 @@ int Endpoint::getPort() const {
Endpoint Endpoint::parse(const std::string& str) { Endpoint Endpoint::parse(const std::string& str) {
if (str.substr(0, 6) != "tcp://") { vector<string> tokens = split(str, ':');
throw new BadFormatException("Bad format for endpoint " + str);
if (tokens.size() != 3) {
throw BadFormatException("Bad format for endpoint " + str);
} }
string substr = str.substr(6); string protocol = tokens[0];
vector<string> tokens = split(substr, ':'); string substr = tokens[1];
if (tokens.size() != 2) { string address = substr.substr(2);
throw new BadFormatException("Bad format for endpoint " + str);
try {
address = substr.substr(2);
}
catch (...) {
throw BadFormatException("Bad format for endpoint " + str);
} }
string address = tokens[0];
int port = 0; int port = 0;
try { try {
port = stoi(tokens[1]); port = stoi(tokens[2]);
} }
catch (...) { catch (...) {
throw new BadFormatException("Bad format for endpoint " + str); throw BadFormatException("Bad format for endpoint " + str);
} }
return Endpoint(address, port); return Endpoint(protocol, address, port);
} }
std::string Endpoint::toString() const { std::string Endpoint::toString() const {
return string("tcp://") + m_address + ":" + to_string(m_port); return m_protocol + "://" + m_address + ":" + to_string(m_port);
}
ApplicationIdentity::ApplicationIdentity(const std::string& name, int id, const Endpoint& endpoint) :
m_name(name),
m_id(id),
m_endpoint(endpoint) {
}
ApplicationIdentity::ApplicationIdentity(const std::string& name, const Endpoint& endpoint) :
m_name(name),
m_endpoint(endpoint) {
}
const std::string& ApplicationIdentity::getName() const {
return m_name;
}
const std::optional<int>& ApplicationIdentity::getId() const {
return m_id;
}
const Endpoint& ApplicationIdentity::getEndpoint() const {
return m_endpoint;
}
std::string ApplicationIdentity::toJSONString() const {
json::StringObject jsonObject;
jsonObject.pushKey(message::ApplicationIdentity::NAME);
jsonObject.pushString(m_name);
if (m_id.has_value()) {
jsonObject.pushKey(message::ApplicationIdentity::ID);
jsonObject.pushInt(m_id.value());
}
jsonObject.pushKey(message::ApplicationIdentity::SERVER);
jsonObject.pushString(m_endpoint.toString());
return jsonObject.toString();
}
ApplicationWithStarterIdentity::ApplicationWithStarterIdentity(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
m_application(application),
m_starter(starter) {
}
ApplicationWithStarterIdentity::ApplicationWithStarterIdentity(const ApplicationIdentity& application) :
m_application(application) {
}
const ApplicationIdentity& ApplicationWithStarterIdentity::getApplication() const {
return m_application;
}
const std::optional<ApplicationIdentity>& ApplicationWithStarterIdentity::getStarter() const {
return m_starter;
}
std::string ApplicationWithStarterIdentity::toJSONString() const {
json::StringObject jsonObject;
jsonObject.pushKey(message::ApplicationIdentity::NAME);
jsonObject.pushString(m_application.getName());
if (m_application.getId().has_value()) {
jsonObject.pushKey(message::ApplicationIdentity::ID);
jsonObject.pushInt(m_application.getId().value());
}
jsonObject.pushKey(message::ApplicationIdentity::SERVER);
jsonObject.pushString(m_application.getEndpoint().toString());
if (m_starter.has_value()) {
jsonObject.pushKey(message::ApplicationIdentity::STARTER);
jsonObject.startObject();
jsonObject.pushKey(message::ApplicationIdentity::NAME);
jsonObject.pushString(m_starter.value().getName());
jsonObject.pushKey(message::ApplicationIdentity::ID);
jsonObject.pushInt(m_starter.value().getId().value());
jsonObject.pushKey(message::ApplicationIdentity::SERVER);
jsonObject.pushString(m_starter.value().getEndpoint().toString());
jsonObject.endObject();
}
return jsonObject.toString();
} }
} }
......
...@@ -76,7 +76,7 @@ namespace message { ...@@ -76,7 +76,7 @@ namespace message {
constexpr const char* KEYVALUE = "KEYVALUE"; constexpr const char* KEYVALUE = "KEYVALUE";
} }
namespace ArgumentInfo { namespace ApplicationIdentity {
constexpr const char* NAME = "name"; // string constexpr const char* NAME = "name"; // string
constexpr const char* ID = "id"; // int32 constexpr const char* ID = "id"; // int32
constexpr const char* SERVER = "server"; // string constexpr const char* SERVER = "server"; // string
......
...@@ -7,3 +7,21 @@ target_link_libraries(testendpoint PUBLIC ...@@ -7,3 +7,21 @@ target_link_libraries(testendpoint PUBLIC
) )
add_test(endpoint testendpoint) add_test(endpoint testendpoint)
add_executable(testapplicationidentity TestApplicationIdentity.cpp)
target_link_libraries(testapplicationidentity PUBLIC
cameo
)
add_test(applicationidentity testapplicationidentity)
add_executable(testapplicationwithstarteridentity TestApplicationWithStarterIdentity.cpp)
target_link_libraries(testapplicationwithstarteridentity PUBLIC
cameo
)
add_test(applicationwithstarteridentity testapplicationwithstarteridentity)
#include "Test.h"
#include "../include/Strings.h"
#include "../include/JSON.h"
#include "../src/message/Message.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
Endpoint endpoint("gamma75", 9999);
ApplicationIdentity identity("my-app", 31, endpoint);
string jsonString = identity.toJSONString();
json::Object jsonIdentity;
json::parse(jsonIdentity, jsonString);
CAMEO_ASSERT_TRUE(string("my-app") == jsonIdentity[message::ApplicationIdentity::NAME].GetString());
CAMEO_ASSERT_EQUAL(31, jsonIdentity[message::ApplicationIdentity::ID].GetInt());
CAMEO_ASSERT_TRUE(string("tcp://gamma75:9999") == jsonIdentity[message::ApplicationIdentity::SERVER].GetString());
return 0;
}
#include "Test.h"
#include "../include/Strings.h"
#include "../include/JSON.h"
#include "../src/message/Message.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
Endpoint myEndpoint("gamma75", 9000);
ApplicationIdentity application("my-app", 31, myEndpoint);
Endpoint yourEndpoint("gamma57", 7000);
ApplicationIdentity starter("your-app", 76, yourEndpoint);
ApplicationWithStarterIdentity identity(application, starter);
string jsonString = identity.toJSONString();
json::Object jsonIdentity;
json::parse(jsonIdentity, jsonString);
CAMEO_ASSERT_TRUE(string("my-app") == jsonIdentity[message::ApplicationIdentity::NAME].GetString());
CAMEO_ASSERT_EQUAL(31, jsonIdentity[message::ApplicationIdentity::ID].GetInt());
CAMEO_ASSERT_TRUE(string("tcp://gamma75:9000") == jsonIdentity[message::ApplicationIdentity::SERVER].GetString());
CAMEO_ASSERT_TRUE(jsonIdentity.HasMember(message::ApplicationIdentity::STARTER));
CAMEO_ASSERT_TRUE(string("your-app") == jsonIdentity[message::ApplicationIdentity::STARTER][message::ApplicationIdentity::NAME].GetString());
CAMEO_ASSERT_EQUAL(76, jsonIdentity[message::ApplicationIdentity::STARTER][message::ApplicationIdentity::ID].GetInt());
CAMEO_ASSERT_TRUE(string("tcp://gamma57:7000") == jsonIdentity[message::ApplicationIdentity::STARTER][message::ApplicationIdentity::SERVER].GetString());
ApplicationWithStarterIdentity identity2(application);
jsonString = identity2.toJSONString();
json::parse(jsonIdentity, jsonString);
CAMEO_ASSERT_TRUE(!jsonIdentity.HasMember(message::ApplicationIdentity::STARTER));
return 0;
}
...@@ -13,17 +13,25 @@ int main(int argc, char *argv[]) { ...@@ -13,17 +13,25 @@ int main(int argc, char *argv[]) {
endpoint = Endpoint::parse("tcp://gamma75:9999"); endpoint = Endpoint::parse("tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("tcp" == endpoint.getProtocol());
CAMEO_ASSERT_TRUE("gamma75" == endpoint.getAddress());
CAMEO_ASSERT_EQUAL(9999, endpoint.getPort());
endpoint = Endpoint::parse("ws://gamma75:9999");
CAMEO_ASSERT_TRUE("ws" == endpoint.getProtocol());
CAMEO_ASSERT_TRUE("gamma75" == endpoint.getAddress()); CAMEO_ASSERT_TRUE("gamma75" == endpoint.getAddress());
CAMEO_ASSERT_EQUAL(9999, endpoint.getPort()); CAMEO_ASSERT_EQUAL(9999, endpoint.getPort());
endpoint = Endpoint::parse("tcp://175.29.285.15:9999"); endpoint = Endpoint::parse("tcp://175.29.285.15:9999");
CAMEO_ASSERT_TRUE("tcp" == endpoint.getProtocol());
CAMEO_ASSERT_TRUE("175.29.285.15" == endpoint.getAddress()); CAMEO_ASSERT_TRUE("175.29.285.15" == endpoint.getAddress());
CAMEO_ASSERT_EQUAL(9999, endpoint.getPort()); CAMEO_ASSERT_EQUAL(9999, endpoint.getPort());
bool error = false; bool error = false;
try { try {
Endpoint::parse("tcp:/gamma75:9999"); Endpoint::parse("gamma75:9999");
} }
catch (...) { catch (...) {
error = true; error = true;
......
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