Commit c49ff939 authored by legoc's avatar legoc
Browse files

(split) Removed Some strings classes and preparing to JSON argument info

parent 3f5fe061
...@@ -23,10 +23,6 @@ ...@@ -23,10 +23,6 @@
namespace cameo { namespace cameo {
struct Name {
static bool check(const std::string& str);
};
std::vector<std::string> split(const std::string& str, char c); std::vector<std::string> split(const std::string& str, char c);
class Endpoint { class Endpoint {
...@@ -46,71 +42,6 @@ private: ...@@ -46,71 +42,6 @@ private:
int m_port; int m_port;
}; };
class NameId {
public:
/**
* Constructor.
*/
NameId(const std::string& name);
/**
* Constructor.
*/
NameId(const std::string& name, int id);
const std::string& getName() const;
/**
* Return the id which may not exist.
*/
const std::optional<int>& getId() const;
static NameId parse(const std::string& str);
std::string toString() const;
private:
std::string m_name;
std::optional<int> m_id;
};
class ApplicationIdentity {
public:
ApplicationIdentity(const NameId& nameId, const Endpoint& endpoint);
const NameId& getNameId() const;
const Endpoint& getEndpoint() const;
static ApplicationIdentity parse(const std::string& str);
std::string toString() const;
private:
NameId m_nameId;
Endpoint m_endpoint;
};
class ApplicationAndStarterIdentities {
public:
ApplicationAndStarterIdentities(const ApplicationIdentity& application);
ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter);
const ApplicationIdentity& getApplication() const;
const std::optional<ApplicationIdentity>& getStarter() const;
static ApplicationAndStarterIdentities parse(const std::string& str);
std::string toString() const;
private:
ApplicationIdentity m_application;
std::optional<ApplicationIdentity> m_starter;
};
} }
#endif #endif
...@@ -22,14 +22,6 @@ using namespace std; ...@@ -22,14 +22,6 @@ using namespace std;
namespace cameo { namespace cameo {
bool Name::check(const std::string& str) {
regex nameRegex("[a-zA-Z0-9\\-_]+");
std::cmatch m;
return regex_match(str.c_str(), m, nameRegex);
}
std::vector<std::string> split(const std::string& str, char c) { std::vector<std::string> split(const std::string& str, char c) {
vector<string> result; vector<string> result;
...@@ -61,12 +53,7 @@ int Endpoint::getPort() const { ...@@ -61,12 +53,7 @@ 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);
}
string substr = str.substr(6);
vector<string> tokens = split(substr, ':');
if (tokens.size() != 2) { if (tokens.size() != 2) {
throw new BadFormatException("Bad format for endpoint " + str); throw new BadFormatException("Bad format for endpoint " + str);
...@@ -86,151 +73,7 @@ Endpoint Endpoint::parse(const std::string& str) { ...@@ -86,151 +73,7 @@ Endpoint Endpoint::parse(const std::string& str) {
} }
std::string Endpoint::toString() const { std::string Endpoint::toString() const {
return string("tcp://") + m_address + ":" + to_string(m_port); return m_address + ":" + to_string(m_port);
}
NameId::NameId(const std::string& name) :
m_name(name) {
}
NameId::NameId(const std::string& name, int id) :
m_name(name),
m_id(id) {
}
const std::string& NameId::getName() const {
return m_name;
}
const std::optional<int>& NameId::getId() const {
return m_id;
}
NameId NameId::parse(const std::string& str) {
vector<string> tokens = split(str, '.');
if (tokens.size() > 2) {
throw BadFormatException("Bad format for nameid " + str);
}
string name = tokens[0];
if (tokens.size() == 2) {
try {
int id = stoi(tokens[1]);
return NameId(name, id);
}
catch (...) {
throw new BadFormatException("Bad format for endpoint " + str);
}
}
return NameId(name);
}
std::string NameId::toString() const {
if (m_id > 0) {
return m_name + "." + to_string(m_id.value());
}
return m_name;
}
ApplicationIdentity::ApplicationIdentity(const NameId& nameId, const Endpoint& endpoint) :
m_nameId(nameId),
m_endpoint(endpoint) {
}
const NameId& ApplicationIdentity::getNameId() const {
return m_nameId;
}
const Endpoint& ApplicationIdentity::getEndpoint() const {
return m_endpoint;
}
ApplicationIdentity ApplicationIdentity::parse(const std::string& str) {
vector<string> tokens = split(str, '@');
if (tokens.size() != 2) {
throw BadFormatException("Bad format for application identity " + str);
}
return ApplicationIdentity(NameId::parse(tokens[0]), Endpoint::parse(tokens[1]));
}
std::string ApplicationIdentity::toString() const {
return m_nameId.toString() + "@" + m_endpoint.toString();
}
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application) :
m_application(application) {
}
ApplicationAndStarterIdentities::ApplicationAndStarterIdentities(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
m_application(application),
m_starter(starter) {
}
const ApplicationIdentity& ApplicationAndStarterIdentities::getApplication() const {
return m_application;
}
const std::optional<ApplicationIdentity>& ApplicationAndStarterIdentities::getStarter() const {
return m_starter;
}
ApplicationAndStarterIdentities ApplicationAndStarterIdentities::parse(const std::string& str) {
// The string is either <name>@<endpoint>:<name>@<endpoint> or <name>@<endpoint>:
// To separate the two identities, we search for the last : before the last @.
string::size_type firstIndex = str.find_first_of('@');
if (firstIndex == string::npos) {
throw new BadFormatException("Bad format for application and starter identities " + str);
}
string::size_type index = str.find_last_of('@');
if (index == firstIndex) {
// Format <name>@<endpoint>:
if (str[str.length() - 1] != ':') {
throw BadFormatException("Bad format for application and starter identities " + str);
}
string applicationString = str.substr(0, str.length() - 1);
ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
return ApplicationAndStarterIdentities(application);
}
else {
// Format <name>@<endpoint>:<name>@<endpoint>
string substring = str.substr(0, index);
index = substring.find_last_of(':');
if (index == string::npos) {
throw BadFormatException("Bad format for application and starter identities " + str);
}
string applicationString = str.substr(0, index);
string starterString = str.substr(index + 1, str.length() - index);
ApplicationIdentity application = ApplicationIdentity::parse(applicationString);
ApplicationIdentity starter = ApplicationIdentity::parse(starterString);
return ApplicationAndStarterIdentities(application, starter);
}
}
std::string ApplicationAndStarterIdentities::toString() const {
if (m_starter.has_value()) {
return m_application.toString() + ":" + m_starter.value().toString();
}
return m_application.toString() + ":";
} }
} }
......
...@@ -76,6 +76,13 @@ namespace message { ...@@ -76,6 +76,13 @@ namespace message {
constexpr const char* KEYVALUE = "KEYVALUE"; constexpr const char* KEYVALUE = "KEYVALUE";
} }
namespace ArgumentInfo {
constexpr const char* NAME = "name"; // string
constexpr const char* ID = "id"; // int32
constexpr const char* SERVER = "server"; // string
constexpr const char* STARTER = "starter"; // object
}
namespace SyncStreamRequest { namespace SyncStreamRequest {
constexpr const char* NAME = "name"; // string constexpr const char* NAME = "name"; // string
} }
......
...@@ -7,39 +7,3 @@ target_link_libraries(testendpoint PUBLIC ...@@ -7,39 +7,3 @@ target_link_libraries(testendpoint PUBLIC
) )
add_test(endpoint testendpoint) add_test(endpoint testendpoint)
add_executable(testname TestName.cpp)
target_link_libraries(testname PUBLIC
cameo
)
add_test(name testname)
add_executable(testnameid TestNameId.cpp)
target_link_libraries(testnameid PUBLIC
cameo
)
add_test(nameid testnameid)
add_executable(testapplicationidentity TestApplicationIdentity.cpp)
target_link_libraries(testapplicationidentity PUBLIC
cameo
)
add_test(applicationidentity testapplicationidentity)
add_executable(testapplicationandstarteridentities TestApplicationAndStarterIdentities.cpp)
target_link_libraries(testapplicationandstarteridentities PUBLIC
cameo
)
add_test(applicationandstarteridentities testapplicationandstarteridentities)
\ No newline at end of file
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
ApplicationAndStarterIdentities identities = ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999:your-app.15@tcp://gamma63:789");
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == identities.getApplication().toString());
CAMEO_ASSERT_TRUE(identities.getStarter().has_value());
CAMEO_ASSERT_TRUE("your-app.15@tcp://gamma63:789" == identities.getStarter().value().toString());
identities = ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999:");
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == identities.getApplication().toString());
CAMEO_ASSERT_TRUE(!identities.getStarter().has_value());
bool error = false;
try {
ApplicationAndStarterIdentities::parse("my-app.31@tcp://gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
CAMEO_ASSERT_TRUE("my-app.31@tcp://gamma75:9999" == ApplicationIdentity(NameId("my-app", 31), Endpoint("gamma75", 9999)).toString());
CAMEO_ASSERT_TRUE("my-app@tcp://gamma75:9999" == ApplicationIdentity(NameId("my-app"), Endpoint("gamma75", 9999)).toString());
ApplicationIdentity identity = ApplicationIdentity::parse("my-app.31@tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(identity.getNameId().getId().has_value());
CAMEO_ASSERT_EQUAL(31, identity.getNameId().getId().value());
CAMEO_ASSERT_TRUE("gamma75" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
identity = ApplicationIdentity::parse("my-app.31@tcp://127.65.198.1:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(identity.getNameId().getId().has_value());
CAMEO_ASSERT_EQUAL(31, identity.getNameId().getId().value());
CAMEO_ASSERT_TRUE("127.65.198.1" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
identity = ApplicationIdentity::parse("my-app@tcp://gamma75:9999");
CAMEO_ASSERT_TRUE("my-app" == identity.getNameId().getName());
CAMEO_ASSERT_TRUE(!identity.getNameId().getId().has_value());
CAMEO_ASSERT_TRUE("gamma75" == identity.getEndpoint().getAddress());
CAMEO_ASSERT_EQUAL(9999, identity.getEndpoint().getPort());
bool error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp://gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp:/gamma75:9999");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
error = false;
try {
ApplicationIdentity::parse("my-app.ff@tcp://gamma75:99G");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
...@@ -9,21 +9,21 @@ int main(int argc, char *argv[]) { ...@@ -9,21 +9,21 @@ int main(int argc, char *argv[]) {
Endpoint endpoint("gamma75", 9999); Endpoint endpoint("gamma75", 9999);
CAMEO_ASSERT_TRUE("tcp://gamma75:9999" == endpoint.toString()); CAMEO_ASSERT_TRUE("gamma75:9999" == endpoint.toString());
endpoint = Endpoint::parse("tcp://gamma75:9999"); endpoint = Endpoint::parse("gamma75:9999");
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("175.29.285.15:9999");
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("gamma:75:9999");
} }
catch (...) { catch (...) {
error = true; error = true;
...@@ -33,7 +33,7 @@ int main(int argc, char *argv[]) { ...@@ -33,7 +33,7 @@ int main(int argc, char *argv[]) {
error = false; error = false;
try { try {
Endpoint::parse("tcp://gamma75:"); Endpoint::parse("gamma75:");
} }
catch (...) { catch (...) {
error = true; error = true;
......
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
CAMEO_ASSERT_TRUE(Name::check("myapp"));
CAMEO_ASSERT_TRUE(Name::check("MyApp"));
CAMEO_ASSERT_TRUE(Name::check("MyApp0"));
CAMEO_ASSERT_TRUE(Name::check("My-App0"));
CAMEO_ASSERT_TRUE(Name::check("My_App0"));
CAMEO_ASSERT_TRUE(!Name::check("myapp!"));
CAMEO_ASSERT_TRUE(!Name::check("my app"));
return 0;
}
#include "Test.h"
#include "../include/Strings.h"
#include <iostream>
using namespace std;
using namespace cameo;
int main(int argc, char *argv[]) {
CAMEO_ASSERT_TRUE("my-app.31" == NameId("my-app", 31).toString());
NameId nameId = NameId::parse("my-app.31");
CAMEO_ASSERT_TRUE("my-app" == nameId.getName());
CAMEO_ASSERT_TRUE(nameId.getId().has_value());
CAMEO_ASSERT_EQUAL(31, nameId.getId().value());
nameId = NameId::parse("my-app32");
CAMEO_ASSERT_TRUE(!nameId.getId().has_value());
bool error = false;
try {
NameId::parse("my-app.ff");
}
catch (...) {
error = true;
}
CAMEO_ASSERT_TRUE(error);
return 0;
}
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