Commit 85140634 authored by legoc's avatar legoc

Re-added some strings classes to prepare JSON argument info

parent 3446f0b1
......@@ -28,8 +28,10 @@ std::vector<std::string> split(const std::string& str, char c);
class Endpoint {
public:
Endpoint(const std::string& protocol, const std::string& address, int port);
Endpoint(const std::string& address, int port);
const std::string& getProtocol() const;
const std::string& getAddress() const;
int getPort() const;
......@@ -38,10 +40,45 @@ public:
std::string toString() const;
private:
std::string m_protocol;
std::string m_address;
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
......@@ -15,6 +15,8 @@
*/
#include "Strings.h"
#include "JSON.h"
#include "message/Message.h"
#include "BadFormatException.h"
#include <regex>
......@@ -38,11 +40,22 @@ std::vector<std::string> split(const std::string& str, char c) {
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) {
m_protocol = "tcp";
m_address = address;
m_port = port;
}
const std::string& Endpoint::getProtocol() const {
return m_protocol;
}
const std::string& Endpoint::getAddress() const {
return m_address;
}
......@@ -53,32 +66,130 @@ int Endpoint::getPort() const {
Endpoint Endpoint::parse(const std::string& str) {
if (str.substr(0, 6) != "tcp://") {
throw new BadFormatException("Bad format for endpoint " + str);
vector<string> tokens = split(str, ':');
if (tokens.size() != 3) {
throw BadFormatException("Bad format for endpoint " + str);
}
string substr = str.substr(6);
vector<string> tokens = split(substr, ':');
string protocol = tokens[0];
string substr = tokens[1];
if (tokens.size() != 2) {
throw new BadFormatException("Bad format for endpoint " + str);
string address = substr.substr(2);
try {
address = substr.substr(2);
}
catch (...) {
throw BadFormatException("Bad format for endpoint " + str);
}
string address = tokens[0];
int port = 0;
try {
port = stoi(tokens[1]);
port = stoi(tokens[2]);
}
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 {
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 {
constexpr const char* KEYVALUE = "KEYVALUE";
}
namespace ArgumentInfo {
namespace ApplicationIdentity {
constexpr const char* NAME = "name"; // string
constexpr const char* ID = "id"; // int32
constexpr const char* SERVER = "server"; // string
......
......@@ -7,3 +7,21 @@ target_link_libraries(testendpoint PUBLIC
)
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[]) {
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_EQUAL(9999, endpoint.getPort());
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_EQUAL(9999, endpoint.getPort());
bool error = false;
try {
Endpoint::parse("tcp:/gamma75:9999");
Endpoint::parse("gamma75:9999");
}
catch (...) {
error = true;
......
......@@ -25,6 +25,10 @@ public class JSON {
public static boolean getBoolean(JSONObject object, String key) {
return (Boolean)object.get(key);
}
public static JSONObject getObject(JSONObject object, String key) {
return (JSONObject)object.get(key);
}
public static JSONArray getArray(JSONObject object, String key) {
return (JSONArray)object.get(key);
......@@ -54,4 +58,5 @@ public class JSON {
public static JSONObject parse(String string) throws ParseException {
return (JSONObject)new JSONParser().parse(string);
}
}
......@@ -64,7 +64,7 @@ public class Message {
public static final String KEYVALUE = "KEYVALUE";
}
public static class ArgumentInfo {
public static class ApplicationIdentity {
public static final String NAME = "name"; // string
public static final String ID = "id"; // int32
public static final String SERVER = "server"; // string
......
package fr.ill.ics.cameo.strings;
import org.json.simple.JSONObject;
import fr.ill.ics.cameo.messages.Message;
public class ApplicationIdentity {
private String name;
private Integer id;
private Endpoint endpoint;
public ApplicationIdentity(String name, Integer id, Endpoint endpoint) {
super();
this.name = name;
this.id = id;
this.endpoint = endpoint;
}
public ApplicationIdentity(String name, Endpoint endpoint) {
super();
this.name = name;
this.endpoint = endpoint;
}
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public Endpoint getEndpoint() {
return endpoint;
}
public JSONObject toJSON() {
JSONObject result = new JSONObject();
result.put(Message.ApplicationIdentity.NAME, name);
if (id != null) {
result.put(Message.ApplicationIdentity.ID, id);
}
result.put(Message.ApplicationIdentity.SERVER, endpoint.toString());
return result;
}
}
package fr.ill.ics.cameo.strings;
import org.json.simple.JSONObject;
import fr.ill.ics.cameo.messages.Message;
public class ApplicationWithStarterIdentity {
private ApplicationIdentity application;
private ApplicationIdentity starter;
public ApplicationWithStarterIdentity(ApplicationIdentity application, ApplicationIdentity starter) {
this.application = application;
this.starter = starter;
}
public ApplicationWithStarterIdentity(ApplicationIdentity application) {
this.application = application;
}
public ApplicationIdentity getApplication() {
return application;
}
public ApplicationIdentity getStarter() {
return starter;
}
public JSONObject toJSON() {
JSONObject result = application.toJSON();
if (starter != null) {
result.put(Message.ApplicationIdentity.STARTER, starter.toJSON());
}
return result;
}
}
......@@ -4,15 +4,27 @@ import fr.ill.ics.cameo.BadFormatException;
public class Endpoint {
private String protocol = "tcp";
private String address;
private int port;
public Endpoint(String protocol, String address, int port) {
super();
this.protocol = protocol;
this.address = address;
this.port = port;
}
public Endpoint(String address, int port) {
super();
this.address = address;
this.port = port;
}
public String getProtocol() {
return protocol;
}
public String getAddress() {
return address;
}
......@@ -23,33 +35,38 @@ public class Endpoint {
public static Endpoint parse(String string) {
if (!string.startsWith("tcp://")) {
String[] tokens = string.split(":");
if (tokens.length != 3) {
throw new BadFormatException("Bad format for endpoint " + string);
}
String substring = string.substring(6);
String[] tokens = substring.split(":");
String protocol = tokens[0];
String substring = tokens[1];
String address;
if (tokens.length != 2) {
try {
address = substring.substring(2);
}
catch (IndexOutOfBoundsException e) {
throw new BadFormatException("Bad format for endpoint " + string);
}
String address = tokens[0];
int port = 0;
try {
port = Integer.parseInt(tokens[1]);
port = Integer.parseInt(tokens[2]);
}
catch (NumberFormatException e) {
throw new BadFormatException("Bad format for endpoint " + string);
}
return new Endpoint(address, port);
return new Endpoint(protocol, address, port);
}
@Override
public String toString() {
return "tcp://" + address + ":" + port;
return protocol + "://" + address + ":" + port;
}
}
package fr.ill.ics.cameo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.junit.Test;
import fr.ill.ics.cameo.messages.JSON;
import fr.ill.ics.cameo.messages.Message;
import fr.ill.ics.cameo.strings.ApplicationIdentity;
import fr.ill.ics.cameo.strings.ApplicationWithStarterIdentity;
import fr.ill.ics.cameo.strings.Endpoint;
public class TestStrings {
......@@ -13,15 +20,18 @@ public class TestStrings {
public void testEndpoint() {
assertEquals("tcp://gamma75:9999", new Endpoint("gamma75", 9999).toString());
assertEquals("tcp://gamma75:9999", new Endpoint("tcp", "gamma75", 9999).toString());
assertEquals("ws://gamma75:9999", new Endpoint("ws", "gamma75", 9999).toString());
Endpoint endpoint = Endpoint.parse("tcp://gamma75:9999");
assertEquals("tcp", endpoint.getProtocol());
assertEquals("gamma75", endpoint.getAddress());
assertEquals(9999, endpoint.getPort());
boolean error = false;
try {
Endpoint.parse("tc://gamma75:9999");
Endpoint.parse("gamma75:9999");
}
catch (Exception e) {
error = true;
......@@ -37,4 +47,64 @@ public class TestStrings {
}
assertTrue(error);
}
@Test
public void testApplicationIdentity() {
ApplicationIdentity identity = new ApplicationIdentity("my-app", 31, new Endpoint("gamma75", 9000));
String jsonString = identity.toJSON().toJSONString();
try {
JSONObject jsonIdentity = JSON.parse(jsonString);
assertEquals("my-app", JSON.getString(jsonIdentity, Message.ApplicationIdentity.NAME));
assertEquals(31, JSON.getInt(jsonIdentity, Message.ApplicationIdentity.ID));
assertEquals("tcp://gamma75:9000", JSON.getString(jsonIdentity, Message.ApplicationIdentity.SERVER));
}
catch (ParseException e) {
assertTrue(false);
}
}
@Test
public void testApplicationWithStarterIdentity() {
ApplicationIdentity application = new ApplicationIdentity("my-app", 31, new Endpoint("gamma75", 9000));
ApplicationIdentity starter = new ApplicationIdentity("your-app", 76, new Endpoint("gamma57", 7000));
ApplicationWithStarterIdentity identity = new ApplicationWithStarterIdentity(application, starter);