1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/core/tracing/TraceMethod.hpp>
10 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/xml/Node.hpp>
14 #include <anna/comm/Service.hpp>
15 #include <anna/comm/Server.hpp>
20 void comm::Service::attach(comm::Server* server)
21 throw(RuntimeException) {
22 LOGMETHOD(TraceMethod tm(Logger::Local7, "comm::Service", "attach", ANNA_FILE_LOCATION));
23 comm::Delivery::add(server);
27 //----------------------------------------------------------------------------------
28 // (1) Cuando no queda ningun recurso disponible se devuelve una excepcion
29 //----------------------------------------------------------------------------------
30 comm::Server* comm::Service::send(comm::Message& message)
31 throw(RuntimeException) {
32 LOGMETHOD(TraceMethod tm(Logger::Local7, "comm::Service", "send", ANNA_FILE_LOCATION));
33 comm::Server* tryServer = NULL;
34 comm::Server* init = NULL;
38 while(stop == false) {
39 tryServer = static_cast <Server*>(Delivery::apply()); // (1)
44 else if(init == tryServer)
47 tryServer->send(message);
50 } catch(RuntimeException& ex) {
52 stop = Delivery::fault(tryServer);
57 string msg(asString());
58 msg += " | Service unavailable";
59 throw RuntimeException(msg, ANNA_FILE_LOCATION);
65 comm::Server* comm::Service::send(comm::Message* message)
66 throw(RuntimeException) {
68 throw RuntimeException("comm::Service::send | Cannot send a NULL message", ANNA_FILE_LOCATION);
70 return (send(*message));
73 int comm::Service::broadcast(comm::Message& message)
75 using namespace anna::comm;
78 for(iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
80 server(ii)->send(message);
82 } catch(RuntimeException& ex) {
88 string msg("comm::Service::broadcast | ");
90 msg += functions::asText(" | Sents number: ", result);
91 Logger::debug(msg, ANNA_FILE_LOCATION);
96 int comm::Service::broadcast(comm::Message* message)
99 Logger::write(Logger::Error, "comm::Service::broadcast | Cannot broadcast a NULL message", ANNA_FILE_LOCATION);
103 return broadcast(*message);
106 string comm::Service::asString() const
108 string result("comm::Service { ");
109 result += Delivery::asString();
110 result += " | Critical: ";
111 result += functions::asString(a_isCritical);
112 return result += " }";
115 xml::Node* comm::Service::asXML(xml::Node* parent) const
117 xml::Node* service = parent->createChild("comm.Service");
118 Delivery::asXML(service);
119 service->createAttribute("Critical", functions::asString(a_isCritical));
124 comm::Server* comm::Service::server(comm::Delivery::iterator& ii)
126 return static_cast <Server*>(Delivery::resource(ii));
130 const comm::Server* comm::Service::server(comm::Delivery::const_iterator& ii)
132 return static_cast <const Server*>(Delivery::resource(ii));