Strings.cpp 4.76 KB
Newer Older
legoc's avatar
legoc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * Copyright 2015 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://joinup.ec.europa.eu/software/page/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "Strings.h"
18
19
#include "JSON.h"
#include "message/Message.h"
legoc's avatar
legoc committed
20
#include "BadFormatException.h"
legoc's avatar
legoc committed
21
#include <regex>
legoc's avatar
legoc committed
22
23
24
25
26
27
28
29
30

using namespace std;

namespace cameo {

std::vector<std::string> split(const std::string& str, char c) {

	vector<string> result;

legoc's avatar
legoc committed
31
32
	string::size_type lastIndex = 0;
	string::size_type index = str.find(c);
legoc's avatar
legoc committed
33
34
35
36
37
38
39
40
41
42
	while (index != string::npos) {
		result.push_back(str.substr(lastIndex, index - lastIndex));
		lastIndex = index + 1;
		index = str.find(c, lastIndex);
	}
	result.push_back(str.substr(lastIndex));

	return result;
}

43
44
45
46
47
48
Endpoint::Endpoint(const std::string& protocol, const std::string& address, int port) {
	m_protocol = protocol;
	m_address = address;
	m_port = port;
}

legoc's avatar
legoc committed
49
Endpoint::Endpoint(const std::string& address, int port) {
50
	m_protocol = "tcp";
legoc's avatar
legoc committed
51
52
53
54
	m_address = address;
	m_port = port;
}

55
56
57
58
const std::string& Endpoint::getProtocol() const {
	return m_protocol;
}

legoc's avatar
legoc committed
59
60
61
62
63
64
65
66
67
68
const std::string& Endpoint::getAddress() const {
	return m_address;
}

int Endpoint::getPort() const {
	return m_port;
}

Endpoint Endpoint::parse(const std::string& str) {

69
70
71
72
	vector<string> tokens = split(str, ':');

	if (tokens.size() != 3) {
		throw BadFormatException("Bad format for endpoint " + str);
legoc's avatar
legoc committed
73
74
	}

75
76
	string protocol = tokens[0];
	string substr = tokens[1];
legoc's avatar
legoc committed
77

78
79
80
81
82
83
84
	string address = substr.substr(2);

	try {
		address = substr.substr(2);
	}
	catch (...) {
		throw BadFormatException("Bad format for endpoint " + str);
legoc's avatar
legoc committed
85
86
87
88
89
	}

	int port = 0;

	try {
90
		port = stoi(tokens[2]);
legoc's avatar
legoc committed
91
92
	}
	catch (...) {
93
		throw BadFormatException("Bad format for endpoint " + str);
legoc's avatar
legoc committed
94
95
	}

96
	return Endpoint(protocol, address, port);
legoc's avatar
legoc committed
97
98
99
}

std::string Endpoint::toString() const {
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	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();
legoc's avatar
legoc committed
193
194
195
196
}

}