Strings.cpp 4.89 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;
}

legoc's avatar
legoc committed
55
56
57
58
Endpoint::Endpoint() :
	m_port(0) {
}

59
60
61
62
const std::string& Endpoint::getProtocol() const {
	return m_protocol;
}

legoc's avatar
legoc committed
63
64
65
66
67
68
69
70
71
72
const std::string& Endpoint::getAddress() const {
	return m_address;
}

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

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

73
74
75
76
	vector<string> tokens = split(str, ':');

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

79
80
	string protocol = tokens[0];
	string substr = tokens[1];
legoc's avatar
legoc committed
81

82
83
84
85
86
87
88
	string address = substr.substr(2);

	try {
		address = substr.substr(2);
	}
	catch (...) {
		throw BadFormatException("Bad format for endpoint " + str);
legoc's avatar
legoc committed
89
90
91
92
93
	}

	int port = 0;

	try {
94
		port = stoi(tokens[2]);
legoc's avatar
legoc committed
95
96
	}
	catch (...) {
97
		throw BadFormatException("Bad format for endpoint " + str);
legoc's avatar
legoc committed
98
99
	}

100
	return Endpoint(protocol, address, port);
legoc's avatar
legoc committed
101
102
103
}

std::string Endpoint::toString() const {
104
105
106
107
108
109
110
111
112
113
114
	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),
legoc's avatar
legoc committed
115
	m_id(Null),
116
117
118
	m_endpoint(endpoint) {
}

legoc's avatar
legoc committed
119
120
121
122
ApplicationIdentity::ApplicationIdentity() :
	m_id(Null) {
}

123
124
125
126
const std::string& ApplicationIdentity::getName() const {
	return m_name;
}

legoc's avatar
legoc committed
127
int ApplicationIdentity::getId() const {
128
129
130
131
132
133
134
135
136
137
138
139
140
141
	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);

legoc's avatar
legoc committed
142
	if (m_id != Null) {
143
		jsonObject.pushKey(message::ApplicationIdentity::ID);
legoc's avatar
legoc committed
144
		jsonObject.pushInt(m_id);
145
146
147
148
149
150
151
152
153
154
	}

	jsonObject.pushKey(message::ApplicationIdentity::SERVER);
	jsonObject.pushString(m_endpoint.toString());

	return jsonObject.toString();
}

ApplicationWithStarterIdentity::ApplicationWithStarterIdentity(const ApplicationIdentity& application, const ApplicationIdentity& starter) :
	m_application(application),
legoc's avatar
legoc committed
155
	m_hasStarter(true),
156
157
158
159
	m_starter(starter) {
}

ApplicationWithStarterIdentity::ApplicationWithStarterIdentity(const ApplicationIdentity& application) :
legoc's avatar
legoc committed
160
161
	m_application(application),
	m_hasStarter(false) {
162
163
164
165
166
167
}

const ApplicationIdentity& ApplicationWithStarterIdentity::getApplication() const {
	return m_application;
}

legoc's avatar
legoc committed
168
169
170
171
172
bool ApplicationWithStarterIdentity::hasStarter() const {
	return m_hasStarter;
}

const ApplicationIdentity& ApplicationWithStarterIdentity::getStarter() const {
173
174
175
176
177
178
179
180
181
182
	return m_starter;
}

std::string ApplicationWithStarterIdentity::toJSONString() const {

	json::StringObject jsonObject;

	jsonObject.pushKey(message::ApplicationIdentity::NAME);
	jsonObject.pushString(m_application.getName());

legoc's avatar
legoc committed
183
	if (m_application.getId() != Null) {
184
		jsonObject.pushKey(message::ApplicationIdentity::ID);
legoc's avatar
legoc committed
185
		jsonObject.pushInt(m_application.getId());
186
187
188
189
190
	}

	jsonObject.pushKey(message::ApplicationIdentity::SERVER);
	jsonObject.pushString(m_application.getEndpoint().toString());

legoc's avatar
legoc committed
191
	if (m_hasStarter) {
192
193
194
195
		jsonObject.pushKey(message::ApplicationIdentity::STARTER);
		jsonObject.startObject();

		jsonObject.pushKey(message::ApplicationIdentity::NAME);
legoc's avatar
legoc committed
196
		jsonObject.pushString(m_starter.getName());
197
198

		jsonObject.pushKey(message::ApplicationIdentity::ID);
legoc's avatar
legoc committed
199
		jsonObject.pushInt(m_starter.getId());
200
201

		jsonObject.pushKey(message::ApplicationIdentity::SERVER);
legoc's avatar
legoc committed
202
		jsonObject.pushString(m_starter.getEndpoint().toString());
203
204
205
206
207

		jsonObject.endObject();
	}

	return jsonObject.toString();
legoc's avatar
legoc committed
208
209
210
211
}

}