Avoid service overhead about checking netstat ports
[anna.git] / source / comm / RoundRobinDelivery.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <algorithm>
10
11 #include <anna/core/tracing/Logger.hpp>
12 #include <anna/core/tracing/TraceMethod.hpp>
13
14 #include <anna/xml/Node.hpp>
15 #include <anna/xml/Attribute.hpp>
16
17 #include <anna/app/functions.hpp>
18
19 #include <anna/comm/Network.hpp>
20 #include <anna/comm/Host.hpp>
21 #include <anna/comm/Server.hpp>
22
23 #include <anna/comm/Communicator.hpp>
24 #include <anna/comm/RoundRobinDelivery.hpp>
25 #include <anna/comm/functions.hpp>
26
27 using namespace std;
28 using namespace anna;
29
30 void comm::RoundRobinDelivery::do_initialize()
31 throw(RuntimeException) {
32   LOGMETHOD(TraceMethod tm("comm::RoundRobinDelivery", "do_initialize", ANNA_FILE_LOCATION));
33   a_iiserver = comm::Delivery::begin();
34 }
35
36 comm::Resource* comm::RoundRobinDelivery::do_apply()
37 throw(RuntimeException) {
38   LOGMETHOD(TraceMethod tm(Logger::Local7, "comm::RoundRobinDelivery", "do_apply", ANNA_FILE_LOCATION));
39   comm::Resource* result = NULL;
40
41   if(comm::Delivery::size() == 0)
42     return NULL;
43
44   iterator end = a_iiserver;
45   comm::Resource* w;
46
47   do {
48     w = resource(a_iiserver);
49
50     if(w->isAvailable() == true && w->isEnabled() == true) {
51       advance();
52       result = w;
53       break;
54     }
55   } while(advance() != end);
56
57   return result;
58 }
59
60 comm::RoundRobinDelivery::iterator comm::RoundRobinDelivery::advance()
61 throw() {
62   a_iiserver ++;
63
64   if(a_iiserver == comm::Delivery::end())
65     a_iiserver = comm::Delivery::begin();
66
67   return a_iiserver;
68 }
69
70 string comm::RoundRobinDelivery::asString() const
71 throw() {
72   string result = className();
73   result += " { ";
74   result += comm::Service::asString();
75   return result += " }";
76 }
77
78 xml::Node* comm::RoundRobinDelivery::asXML(xml::Node* parent) const
79 throw() {
80   xml::Node* node = parent->createChild("comm.RoundRobinDelivery");
81   comm::Service::asXML(node);
82   return node;
83 }
84