Remove warnings (work package 1)
[anna.git] / source / comm / IndexedDelivery.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/IndexedDelivery.hpp>
25 #include <anna/comm/functions.hpp>
26
27 using namespace std;
28 using namespace anna;
29
30 void comm::IndexedDelivery::prepare(const int key)
31 throw(RuntimeException) {
32   const int size = comm::Delivery::size();
33
34   if(size == 0) {
35     string msg(asString());
36     msg += " | No resource has been attached";
37     throw RuntimeException(msg, ANNA_FILE_LOCATION);
38   }
39
40   const int index = key % size;
41
42   a_iikey = begin() + index;
43
44   LOGDEBUG(
45     string msg("anna::comm::IndexedDelivery::prepare | Key: ");
46     msg += functions::asString(key);
47     msg += " | ";
48     msg += resource(a_iikey)->asString();
49     Logger::debug(msg, ANNA_FILE_LOCATION);
50   );
51 }
52
53 comm::Resource* comm::IndexedDelivery::do_apply()
54 throw(RuntimeException) {
55   comm::Resource* result = NULL;
56
57   if(a_iikey == comm::Delivery::end())
58     return NULL;
59
60   comm::Resource* w = result = comm::Delivery::resource(a_iikey);
61
62   if(w->isAvailable() == false || w->isEnabled() == false) {
63     LOGWARNING(
64       string msg(asString());
65       msg += " | ";
66       msg += w->asString();
67       msg += " | Unavailable";
68       Logger::warning(msg, ANNA_FILE_LOCATION);
69     );
70     result = NULL;
71   }
72
73   if(a_mode == Mode::Flexible) {
74     iterator end;
75     iterator ii;
76     end = a_iikey;
77
78     if((ii = a_iikey + 1) == comm::Delivery::end())
79       ii = comm::Delivery::begin();
80
81     while(ii != end) {
82       w = comm::Delivery::resource(ii);
83
84       if(w->isAvailable() == true && w->isEnabled() == true) {
85         result = w;
86         break;
87       }
88
89       ii ++;
90
91       if(ii == comm::Delivery::end())
92         ii = comm::Delivery::begin();
93     }
94   }
95
96   return result;
97 }
98
99 string comm::IndexedDelivery::asString() const
100 throw() {
101   string result = className();
102   result += " { ";
103   result += comm::Delivery::asString();
104   result += " | Mode: ";
105   result += (a_mode == Mode::Strict) ? "Strict" : "Flexible";
106   return result += " }";
107 }
108
109 xml::Node* comm::IndexedDelivery::asXML(xml::Node* parent) const
110 throw() {
111   xml::Node* node = parent->createChild("comm.IndexedDelivery");
112   node->createAttribute("Mode", (a_mode == Mode::Strict) ? "Strict" : "Flexible");
113   comm::Service::asXML(node);
114   return node;
115 }
116