29
29
#include < iostream>
30
30
#include < set>
31
31
32
- using namespace std ;
33
-
34
32
using namespace esi ;
35
33
using namespace esi ::cosim;
36
34
using namespace esi ::services;
@@ -43,10 +41,11 @@ using grpc::ClientReaderWriter;
43
41
using grpc::ClientWriter;
44
42
using grpc::Status;
45
43
46
- static void checkStatus (Status s, const string &msg) {
44
+ static void checkStatus (Status s, const std:: string &msg) {
47
45
if (!s.ok ())
48
- throw runtime_error (msg + " . Code " + to_string (s.error_code ()) + " : " +
49
- s.error_message () + " (" + s.error_details () + " )" );
46
+ throw std::runtime_error (msg + " . Code " + to_string (s.error_code ()) +
47
+ " : " + s.error_message () + " (" +
48
+ s.error_details () + " )" );
50
49
}
51
50
52
51
// / Hack around C++ not having a way to forward declare a nested class.
@@ -56,25 +55,25 @@ struct esi::backends::cosim::CosimAccelerator::StubContainer {
56
55
std::unique_ptr<ChannelServer::Stub> stub;
57
56
};
58
57
59
- // / Parse the connection string and instantiate the accelerator. Support the
60
- // / traditional 'host:port' syntax and a path to 'cosim.cfg' which is output by
61
- // / the cosimulation when it starts (which is useful when it chooses its own
58
+ // / Parse the connection std:: string and instantiate the accelerator. Support
59
+ // / the traditional 'host:port' syntax and a path to 'cosim.cfg' which is output
60
+ // / by the cosimulation when it starts (which is useful when it chooses its own
62
61
// / port).
63
- unique_ptr<AcceleratorConnection>
64
- CosimAccelerator::connect (Context &ctxt, string connectionString) {
65
- string portStr;
66
- string host = " localhost" ;
62
+ std:: unique_ptr<AcceleratorConnection>
63
+ CosimAccelerator::connect (Context &ctxt, std:: string connectionString) {
64
+ std:: string portStr;
65
+ std:: string host = " localhost" ;
67
66
68
67
size_t colon;
69
- if ((colon = connectionString.find (' :' )) != string::npos) {
68
+ if ((colon = connectionString.find (' :' )) != std:: string::npos) {
70
69
portStr = connectionString.substr (colon + 1 );
71
70
host = connectionString.substr (0 , colon);
72
71
} else if (connectionString.ends_with (" cosim.cfg" )) {
73
- ifstream cfg (connectionString);
74
- string line, key, value;
72
+ std:: ifstream cfg (connectionString);
73
+ std:: string line, key, value;
75
74
76
75
while (getline (cfg, line))
77
- if ((colon = line.find (" :" )) != string::npos) {
76
+ if ((colon = line.find (" :" )) != std:: string::npos) {
78
77
key = line.substr (0 , colon);
79
78
value = line.substr (colon + 1 );
80
79
if (key == " port" )
@@ -84,7 +83,7 @@ CosimAccelerator::connect(Context &ctxt, string connectionString) {
84
83
}
85
84
86
85
if (portStr.size () == 0 )
87
- throw runtime_error (" port line not found in file" );
86
+ throw std:: runtime_error (" port line not found in file" );
88
87
} else if (connectionString == " env" ) {
89
88
char *hostEnv = getenv (" ESI_COSIM_HOST" );
90
89
if (hostEnv)
@@ -95,20 +94,21 @@ CosimAccelerator::connect(Context &ctxt, string connectionString) {
95
94
if (portEnv)
96
95
portStr = portEnv;
97
96
else
98
- throw runtime_error (" ESI_COSIM_PORT environment variable not set" );
97
+ throw std:: runtime_error (" ESI_COSIM_PORT environment variable not set" );
99
98
} else {
100
- throw runtime_error (" Invalid connection string '" + connectionString + " '" );
99
+ throw std::runtime_error (" Invalid connection string '" + connectionString +
100
+ " '" );
101
101
}
102
102
uint16_t port = stoul (portStr);
103
103
return make_unique<CosimAccelerator>(ctxt, host, port);
104
104
}
105
105
106
106
// / Construct and connect to a cosim server.
107
- CosimAccelerator::CosimAccelerator (Context &ctxt, string hostname,
107
+ CosimAccelerator::CosimAccelerator (Context &ctxt, std:: string hostname,
108
108
uint16_t port)
109
109
: AcceleratorConnection(ctxt) {
110
110
// Connect to the simulation.
111
- auto channel = grpc::CreateChannel (hostname + " :" + to_string (port),
111
+ auto channel = grpc::CreateChannel (hostname + " :" + std:: to_string (port),
112
112
grpc::InsecureChannelCredentials ());
113
113
rpcClient = new StubContainer (ChannelServer::NewStub (channel));
114
114
}
@@ -165,7 +165,7 @@ class CosimSysInfo : public SysInfo {
165
165
return response.esi_version ();
166
166
}
167
167
168
- vector<uint8_t > getCompressedManifest () const override {
168
+ std:: vector<uint8_t > getCompressedManifest () const override {
169
169
::esi::cosim::Manifest response = getManifest ();
170
170
std::string compressedManifestStr = response.compressed_manifest ();
171
171
return std::vector<uint8_t >(compressedManifestStr.begin (),
@@ -196,17 +196,19 @@ namespace {
196
196
class WriteCosimChannelPort : public WriteChannelPort {
197
197
public:
198
198
WriteCosimChannelPort (ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
199
- const Type *type, string name)
199
+ const Type *type, std:: string name)
200
200
: WriteChannelPort(type), rpcClient(rpcClient), desc(desc), name(name) {}
201
201
~WriteCosimChannelPort () = default ;
202
202
203
203
void connect () override {
204
204
WriteChannelPort::connect ();
205
205
if (desc.type () != getType ()->getID ())
206
- throw runtime_error (" Channel '" + name + " ' has wrong type. Expected " +
207
- getType ()->getID () + " , got " + desc.type ());
206
+ throw std::runtime_error (" Channel '" + name +
207
+ " ' has wrong type. Expected " +
208
+ getType ()->getID () + " , got " + desc.type ());
208
209
if (desc.dir () != ChannelDesc::Direction::ChannelDesc_Direction_TO_SERVER)
209
- throw runtime_error (" Channel '" + name + " ' is not a to server channel" );
210
+ throw std::runtime_error (" Channel '" + name +
211
+ " ' is not a to server channel" );
210
212
assert (desc.name () == name);
211
213
}
212
214
@@ -219,17 +221,17 @@ class WriteCosimChannelPort : public WriteChannelPort {
219
221
VoidMessage response;
220
222
grpc::Status sendStatus = rpcClient->SendToServer (&context, msg, &response);
221
223
if (!sendStatus.ok ())
222
- throw runtime_error (" Failed to write to channel '" + name +
223
- " ': " + sendStatus.error_message () +
224
- " . Details: " + sendStatus.error_details ());
224
+ throw std:: runtime_error (" Failed to write to channel '" + name +
225
+ " ': " + sendStatus.error_message () +
226
+ " . Details: " + sendStatus.error_details ());
225
227
}
226
228
227
229
protected:
228
230
ChannelServer::Stub *rpcClient;
229
231
// / The channel description as provided by the server.
230
232
ChannelDesc desc;
231
233
// / The name of the channel from the manifest.
232
- string name;
234
+ std:: string name;
233
235
};
234
236
} // namespace
235
237
@@ -241,7 +243,7 @@ class ReadCosimChannelPort
241
243
public grpc::ClientReadReactor<esi::cosim::Message> {
242
244
public:
243
245
ReadCosimChannelPort (ChannelServer::Stub *rpcClient, const ChannelDesc &desc,
244
- const Type *type, string name)
246
+ const Type *type, std:: string name)
245
247
: ReadChannelPort(type), rpcClient(rpcClient), desc(desc), name(name),
246
248
context (nullptr ) {}
247
249
virtual ~ReadCosimChannelPort () { disconnect (); }
@@ -250,10 +252,12 @@ class ReadCosimChannelPort
250
252
// Sanity checking.
251
253
ReadChannelPort::connect ();
252
254
if (desc.type () != getType ()->getID ())
253
- throw runtime_error (" Channel '" + name + " ' has wrong type. Expected " +
254
- getType ()->getID () + " , got " + desc.type ());
255
+ throw std::runtime_error (" Channel '" + name +
256
+ " ' has wrong type. Expected " +
257
+ getType ()->getID () + " , got " + desc.type ());
255
258
if (desc.dir () != ChannelDesc::Direction::ChannelDesc_Direction_TO_CLIENT)
256
- throw runtime_error (" Channel '" + name + " ' is not a to server channel" );
259
+ throw std::runtime_error (" Channel '" + name +
260
+ " ' is not a to server channel" );
257
261
assert (desc.name () == name);
258
262
259
263
// Initiate a stream of messages from the server.
@@ -304,7 +308,7 @@ class ReadCosimChannelPort
304
308
// / The channel description as provided by the server.
305
309
ChannelDesc desc;
306
310
// / The name of the channel from the manifest.
307
- string name;
311
+ std:: string name;
308
312
309
313
std::unique_ptr<ClientContext> context;
310
314
// / Storage location for the incoming message.
@@ -315,31 +319,31 @@ class ReadCosimChannelPort
315
319
316
320
} // namespace
317
321
318
- map<string, ChannelPort &>
322
+ std:: map<std:: string, ChannelPort &>
319
323
CosimAccelerator::requestChannelsFor (AppIDPath idPath,
320
324
const BundleType *bundleType) {
321
- map<string, ChannelPort &> channelResults;
325
+ std:: map<std:: string, ChannelPort &> channelResults;
322
326
323
327
// Find the client details for the port at 'fullPath'.
324
328
auto f = clientChannelAssignments.find (idPath);
325
329
if (f == clientChannelAssignments.end ())
326
330
return channelResults;
327
- const map<string, string> &channelAssignments = f->second ;
331
+ const std:: map<std:: string, std:: string> &channelAssignments = f->second ;
328
332
329
333
// Each channel in a bundle has a separate cosim endpoint. Find them all.
330
334
for (auto [name, dir, type] : bundleType->getChannels ()) {
331
335
auto f = channelAssignments.find (name);
332
336
if (f == channelAssignments.end ())
333
- throw runtime_error (" Could not find channel assignment for '" +
334
- idPath.toStr () + " ." + name + " '" );
335
- string channelName = f->second ;
337
+ throw std:: runtime_error (" Could not find channel assignment for '" +
338
+ idPath.toStr () + " ." + name + " '" );
339
+ std:: string channelName = f->second ;
336
340
337
341
// Get the endpoint, which may or may not exist. Construct the port.
338
342
// Everything is validated when the client calls 'connect()' on the port.
339
343
ChannelDesc chDesc;
340
344
if (!getChannelDesc (channelName, chDesc))
341
- throw runtime_error (" Could not find channel '" + channelName +
342
- " ' in cosimulation" );
345
+ throw std:: runtime_error (" Could not find channel '" + channelName +
346
+ " ' in cosimulation" );
343
347
344
348
ChannelPort *port;
345
349
if (BundlePort::isWrite (dir)) {
@@ -358,7 +362,7 @@ CosimAccelerator::requestChannelsFor(AppIDPath idPath,
358
362
// / Get the channel description for a channel name. Iterate through the list
359
363
// / each time. Since this will only be called a small number of times on a small
360
364
// / list, it's not worth doing anything fancy.
361
- bool CosimAccelerator::getChannelDesc (const string &channelName,
365
+ bool CosimAccelerator::getChannelDesc (const std:: string &channelName,
362
366
ChannelDesc &desc) {
363
367
ClientContext context;
364
368
VoidMessage arg;
@@ -386,11 +390,11 @@ Service *CosimAccelerator::createService(Service::Type svcType,
386
390
// Get the channel assignments for each client.
387
391
for (auto client : clients) {
388
392
AppIDPath fullClientPath = prefix + client.relPath ;
389
- map<string, string> channelAssignments;
390
- for (auto assignment : any_cast<map<string, any>>(
393
+ std:: map<std:: string, std:: string> channelAssignments;
394
+ for (auto assignment : any_cast<std:: map<std:: string, std:: any>>(
391
395
client.implOptions .at (" channel_assignments" )))
392
396
channelAssignments[assignment.first ] =
393
- any_cast<string>(assignment.second );
397
+ std:: any_cast<std:: string>(assignment.second );
394
398
clientChannelAssignments[fullClientPath] = std::move (channelAssignments);
395
399
}
396
400
}
0 commit comments