@@ -102,23 +102,26 @@ namespace GridKit
102102
103103 template <typename scalar_type>
104104 detail::Channel<scalar_type>::Channel(
105- Disposition disposition,
106- const std::string& endpoint,
107- int timeout_ms,
108- const std::atomic<RunState>* state)
105+ Disposition disposition,
106+ const std::string& endpoint,
107+ int timeout_ms,
108+ const std::atomic<RunState>* state
109+ )
109110 : context_(),
110111 socket_ (
111- context_,
112- disposition == Disposition::SERVER ? zmq::socket_type::rep
113- : zmq::socket_type::req),
112+ context_,
113+ disposition == Disposition::SERVER ? zmq::socket_type::rep
114+ : zmq::socket_type::req
115+ ),
114116 timeout_ms_(timeout_ms),
115117 state_(state),
116118 can_send_(disposition == Disposition::CLIENT )
117119 {
118120 if (timeout_ms <= 0 || endpoint.empty ())
119121 {
120122 throw std::invalid_argument (
121- " co-simulation requires an endpoint and a positive timeout" );
123+ " co-simulation requires an endpoint and a positive timeout"
124+ );
122125 }
123126
124127 // bound socket cleanup
@@ -140,37 +143,41 @@ namespace GridKit
140143 {
141144 if (state_
142145 && state_->load (std::memory_order_relaxed)
143- == RunState::STOP_REQUESTED )
146+ == RunState::STOP_REQUESTED )
144147 {
145148 throw Stopped{};
146149 }
147150 }
148151
149152 template <typename scalar_type>
150153 void detail::Channel<scalar_type>::wait(
151- short events,
152- const char * operation,
153- std::chrono::steady_clock::time_point deadline)
154+ short events,
155+ const char * operation,
156+ std::chrono::steady_clock::time_point deadline
157+ )
154158 {
155159 using Clock = std::chrono::steady_clock;
156160
157161 while (true )
158162 {
159163 checkStop ();
160164 const auto remaining =
161- std::chrono::duration_cast<std::chrono::milliseconds>(
162- deadline - Clock::now ());
165+ std::chrono::duration_cast<std::chrono::milliseconds>(
166+ deadline - Clock::now ()
167+ );
163168 if (remaining.count () <= 0 )
164169 {
165170 throw std::runtime_error (
166- std::format (" Co-simulation {} timed out" , operation));
171+ std::format (" Co-simulation {} timed out" , operation)
172+ );
167173 }
168174
169175 zmq::pollitem_t item{socket_.handle (), 0 , events, 0 };
170176 try
171177 {
172178 zmq::poll (
173- &item, 1 , std::min (remaining, std::chrono::milliseconds (25 )));
179+ &item, 1 , std::min (remaining, std::chrono::milliseconds (25 ))
180+ );
174181 }
175182 catch (const zmq::error_t & error)
176183 {
@@ -190,7 +197,8 @@ namespace GridKit
190197
191198 template <typename scalar_type>
192199 void detail::Channel<scalar_type>::send(
193- const MessageData<scalar_type>& message)
200+ const MessageData<scalar_type>& message
201+ )
194202 {
195203 checkStop ();
196204 if (!can_send_)
@@ -200,10 +208,11 @@ namespace GridKit
200208
201209 validate (message);
202210 json wire = {
203- {" type" , typeName (message.type )},
204- {" session" , message.session },
205- {" time" , message.time },
206- {" signals" , message.signals }};
211+ {" type" , typeName (message.type )},
212+ {" session" , message.session },
213+ {" time" , message.time },
214+ {" signals" , message.signals }
215+ };
207216
208217 if (message.type == MessageType::INIT )
209218 {
@@ -237,7 +246,8 @@ namespace GridKit
237246 if (can_send_)
238247 {
239248 throw std::logic_error (
240- " Co-simulation socket must send before receiving" );
249+ " Co-simulation socket must send before receiving"
250+ );
241251 }
242252
243253 zmq::message_t raw;
@@ -280,7 +290,8 @@ namespace GridKit
280290
281291 template <typename scalar_type>
282292 MessageData<scalar_type> detail::Channel<scalar_type>::exchange(
283- const MessageData<scalar_type>& request)
293+ const MessageData<scalar_type>& request
294+ )
284295 {
285296 send (request);
286297 auto reply = receive ();
@@ -291,13 +302,14 @@ namespace GridKit
291302 }
292303
293304 const bool final_signals =
294- request.type == MessageType::STEP && reply.type == MessageType::END ;
305+ request.type == MessageType::STEP && reply.type == MessageType::END ;
295306
296307 if ((!final_signals && reply.type != request.type )
297308 || reply.session != request.session || reply.time != request.time )
298309 {
299310 throw std::runtime_error (
300- " co-simulation acknowledgement has the wrong type, session or time" );
311+ " co-simulation acknowledgement has the wrong type, session or time"
312+ );
301313 }
302314
303315 return reply;
@@ -341,7 +353,8 @@ namespace GridKit
341353
342354 template <typename scalar_type>
343355 void detail::Channel<scalar_type>::fail(
344- const MessageData<scalar_type>& message) noexcept
356+ const MessageData<scalar_type>& message
357+ ) noexcept
345358 {
346359 Teardown teardown (*this );
347360
@@ -368,7 +381,8 @@ namespace GridKit
368381
369382 template <typename scalar_type>
370383 void detail::Channel<scalar_type>::closePeer(
371- const MessageMetadata& last) noexcept
384+ const MessageMetadata& last
385+ ) noexcept
372386 {
373387 Teardown teardown (*this );
374388
@@ -401,7 +415,8 @@ namespace GridKit
401415
402416 template <typename scalar_type>
403417 void detail::Channel<scalar_type>::finish(
404- const MessageData<scalar_type>& reply)
418+ const MessageData<scalar_type>& reply
419+ )
405420 {
406421 send (reply);
407422 Teardown teardown (*this );
@@ -417,10 +432,11 @@ namespace GridKit
417432
418433 template <typename scalar_type, Disposition disposition>
419434 CoSim<scalar_type, disposition>::CoSim(
420- const std::string& address,
421- MessageStepFunction<scalar_type> step,
422- MessageExtractFunction<scalar_type> extract,
423- int timeout_ms)
435+ const std::string& address,
436+ MessageStepFunction<scalar_type> step,
437+ MessageExtractFunction<scalar_type> extract,
438+ int timeout_ms
439+ )
424440 : address_(address),
425441 step_ (std::move(step)),
426442 extract_(std::move(extract)),
@@ -429,22 +445,25 @@ namespace GridKit
429445 if (address.empty () || timeout_ms <= 0 || !step_ || !extract_)
430446 {
431447 throw std::invalid_argument (
432- " co-simulation requires an endpoint, callbacks and a positive timeout" );
448+ " co-simulation requires an endpoint, callbacks and a positive timeout"
449+ );
433450 }
434451 }
435452
436453 template <typename scalar_type, Disposition disposition>
437454 CoSim<scalar_type, disposition>::CoSim(
438- const std::string& address,
439- StepFunction<scalar_type> step,
440- ExtractFunction<scalar_type> extract,
441- int timeout_ms)
455+ const std::string& address,
456+ StepFunction<scalar_type> step,
457+ ExtractFunction<scalar_type> extract,
458+ int timeout_ms
459+ )
442460 : address_(address), timeout_ms_(timeout_ms)
443461 {
444462 if (address.empty () || timeout_ms <= 0 || !step || !extract)
445463 {
446464 throw std::invalid_argument (
447- " co-simulation requires an endpoint, callbacks and a positive timeout" );
465+ " co-simulation requires an endpoint, callbacks and a positive timeout"
466+ );
448467 }
449468
450469 step_ = [step = std::move (step)](const auto & message, const auto &)
@@ -463,13 +482,15 @@ namespace GridKit
463482 {
464483 auto expected = detail::RunState::IDLE ;
465484 if (!state_.compare_exchange_strong (
466- expected,
467- detail::RunState::RUNNING ,
468- std::memory_order_acquire,
469- std::memory_order_relaxed))
485+ expected,
486+ detail::RunState::RUNNING ,
487+ std::memory_order_acquire,
488+ std::memory_order_relaxed
489+ ))
470490 {
471491 throw std::logic_error (
472- " attempted to start a co-simulation while it was already running" );
492+ " attempted to start a co-simulation while it was already running"
493+ );
473494 }
474495
475496 // ensure the state is reset on scope exit
@@ -484,7 +505,8 @@ namespace GridKit
484505 } guard{state_};
485506
486507 detail::Channel<scalar_type> channel (
487- disposition, address_, timeout_ms_, &state_);
508+ disposition, address_, timeout_ms_, &state_
509+ );
488510 MessageMetadata current;
489511
490512 struct Completed
@@ -503,7 +525,8 @@ namespace GridKit
503525 if (message.type == MessageType::FAIL )
504526 {
505527 throw std::runtime_error (
506- " co-simulation client failed: " + message.error );
528+ " co-simulation client failed: " + message.error
529+ );
507530 }
508531
509532 if (message.type == MessageType::CLOSE )
@@ -514,7 +537,7 @@ namespace GridKit
514537 return message;
515538 };
516539 const MessageExchangeFunction<scalar_type> exchange =
517- [&](const auto & message)
540+ [&](const auto & message)
518541 {
519542 if constexpr (disposition == Disposition::SERVER )
520543 {
@@ -532,7 +555,8 @@ namespace GridKit
532555 if (peer_finished)
533556 {
534557 throw std::runtime_error (
535- " co-simulation peer ended before the callback completed" );
558+ " co-simulation peer ended before the callback completed"
559+ );
536560 }
537561 current = message;
538562 auto reply = channel.exchange (message);
@@ -591,9 +615,9 @@ namespace GridKit
591615 {
592616 channel.closePeer (last_reply);
593617 return state_.load (std::memory_order_relaxed)
594- == detail::RunState::STOP_REQUESTED
595- ? RunResult::STOPPED
596- : RunResult::COMPLETED ;
618+ == detail::RunState::STOP_REQUESTED
619+ ? RunResult::STOPPED
620+ : RunResult::COMPLETED ;
597621 }
598622 }
599623 }
0 commit comments