IOConnections.cpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo is free software: you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License v3 (LGPL)
13  * as published by the Free Software Foundation.
14  *
15  * mimmo is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23 \*---------------------------------------------------------------------------*/
24 #include "IOConnections.hpp"
25 #include "portManager.hpp"
26 
27 namespace mimmo{
28 
37 IOConnections_MIMMO::IOConnections_MIMMO(std::unordered_map<std::string, BaseManipulation * > mapConn) {
38 
39  m_log = &bitpit::log::cout(MIMMO_LOG_FILE);
40 
41  for(auto &val: mapConn){
42  if(val.second == nullptr) continue;
43  m_mapConn[val.first] = val.second;
44  m_invMapConn[val.second] = val.first;
45  }
46 };
47 
52 
57  m_mapConn = other.m_mapConn;
58  m_invMapConn = other.m_invMapConn;
59  m_log = &bitpit::log::cout(MIMMO_LOG_FILE);
60 };
61 
72 void
73 IOConnections_MIMMO::absorbConnections(const bitpit::Config & slotXML, bool debug){
74 
75  if(debug)
76  m_log->setPriority(bitpit::log::NORMAL);
77 
78  if(slotXML.getSectionCount() == 0) {
79  (*m_log)<<"IOConnections::absorbConnections does not found any connection to read in current XML slot."<<std::endl;
80  return;
81  }
82  bool checkConnection = true;
83 
84  for( auto & sect : slotXML.getSections()){
85  std::string snd_str;
86  std::string rcv_str;
87  std::string sndP_str;
88  std::string rcvP_str;
89 
90  if(sect.second->hasOption("sender")) snd_str = sect.second->get("sender");
91  if(sect.second->hasOption("receiver")) rcv_str = sect.second->get("receiver");
92  if(sect.second->hasOption("senderPort")) sndP_str = sect.second->get("senderPort");
93  if(sect.second->hasOption("receiverPort")) rcvP_str = sect.second->get("receiverPort");
94 
95  snd_str = bitpit::utils::string::trim(snd_str);
96  rcv_str = bitpit::utils::string::trim(rcv_str);
97  sndP_str = bitpit::utils::string::trim(sndP_str);
98  rcvP_str = bitpit::utils::string::trim(rcvP_str);
99 
100 
101  auto itSend = m_mapConn.find(snd_str);
102  auto itRece = m_mapConn.find(rcv_str);
103  bool checkSP = mimmo::PortManager::instance().containsPort(sndP_str);
104  bool checkRP = mimmo::PortManager::instance().containsPort(rcvP_str);
105 
106  if(itSend == m_mapConn.end() || itRece == m_mapConn.end() || !checkSP || !checkRP ){
107  (*m_log)<<"---------------------------------------------"<<std::endl;
108  (*m_log)<<"sender: "<<snd_str<<std::endl;
109  (*m_log)<<"receiver: "<<rcv_str<<std::endl;
110  (*m_log)<<"senderPort: "<<sndP_str<<std::endl;
111  (*m_log)<<"receiverPort: "<<rcvP_str<<std::endl;
112  (*m_log)<<"---------------------------------------------"<<std::endl;
113  (*m_log)<<""<<std::endl;
114  (*m_log)<<"IOConnections::absorbConnections does not found any sender/receiver and/or port compatible with API."<<std::endl;
115  (*m_log)<<"Please control connection data entries in your XML file"<<std::endl;
116  (*m_log)<<""<<std::endl;
117  continue;
118  }
119 
120  m_log->setPriority(bitpit::log::NORMAL);
121  bool check = pin::addPin(itSend->second, itRece->second, sndP_str, rcvP_str);
122  if(!debug) m_log->setPriority(bitpit::log::DEBUG);
123 
124  if(!check){
125  (*m_log)<<"---------------------------------------------"<<std::endl;
126  (*m_log)<<"sender: "<<snd_str<<std::endl;
127  (*m_log)<<"receiver: "<<rcv_str<<std::endl;
128  (*m_log)<<"senderPort: "<<sndP_str<<std::endl;
129  (*m_log)<<"receiverPort: "<<rcvP_str<<std::endl;
130  (*m_log)<<"---------------------------------------------"<<std::endl;
131  (*m_log)<<""<<std::endl;
132  (*m_log)<<"IOConnections::absorbConnections failed creating connection."<<std::endl;
133  (*m_log)<<""<<std::endl;
134  checkConnection = false;
135  }
136  else{
137  (*m_log)<<"IOConnections::absorbConnections successfully created connection."<<std::endl;
138  }
139  }
140 
141  m_log->setPriority(bitpit::log::DEBUG);
142  if(!checkConnection){
143  throw std::runtime_error ("IOConnections : one or more failed pin connections found.");
144  }
145  return;
146 };
147 
158 void
159 IOConnections_MIMMO::flushConnections(bitpit::Config & slotXML, bool debug ){
160 
161  int counterGlob = 0;
162  for( auto object : m_mapConn){
163 
164  std::string sender = m_invMapConn[object.second]; //recover sender name
165  auto mapPOut = (object.second)->getPortsOut(); //get its slot of output port
166 
167 
168  for(auto & pOut : mapPOut ){
169 
170  std::string senderPort = pOut.first; //gets the Port Type of sender
171 
172  std::vector<BaseManipulation * > links = (pOut.second)->getLink(); //get all possible links to this specific port
173  std::vector<PortID> lids = (pOut.second)->getPortLink();
174 
175  int counter = 0;
176  for( auto & val : links){
177  std::string receiver = m_invMapConn[val];
178  std::string receiverPort = lids[counter];
179 
180  std::string title = "conn" + std::to_string(counter+counterGlob);
181  bitpit::Config::Section & conn = slotXML.addSection(title);
182  conn.set("sender", sender);
183  conn.set("senderPort", senderPort);
184  conn.set("receiver", receiver);
185  conn.set("receiverPort", receiverPort);
186 
187  ++counter;
188  }//loop on possible links on the same port
189  counterGlob += counter;
190  }//loop on all the port of the single object
191  }//loop on all instantiated object.
192 
193  if(debug)
194  m_log->setPriority(bitpit::log::NORMAL);
195  (*m_log)<<"IOConnections::flushConnections wrote "<< slotXML.getSectionCount()<< " active connections."<<std::endl;
196  m_log->setPriority(bitpit::log::DEBUG);
197  return;
198 };
199 
200 }
void flushConnections(bitpit::Config &slotXML, bool debug=false)
static PortManager & instance()
Definition: portManager.hpp:85
void absorbConnections(const bitpit::Config &slotXML, bool debug=false)
std::unordered_map< BaseManipulation *, std::string > m_invMapConn
bool addPin(BaseManipulation *objSend, BaseManipulation *objRec, PortID portS, PortID portR, bool forced)
IOConnections_MIMMO(std::unordered_map< std::string, BaseManipulation * > mapConn)
std::unordered_map< std::string, BaseManipulation * > m_mapConn
IOConnections_MIMMO is a class to read/write mimmo pin connections from XML IO/parser defined in bitp...
bool containsPort(const std::string name)