TTEthernet Model for INET Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Buffer.cc
Go to the documentation of this file.
1 //
2 // This program is free software: you can redistribute it and/or modify
3 // it under the terms of the GNU Lesser General Public License as published by
4 // the Free Software Foundation, either version 3 of the License, or
5 // (at your option) any later version.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public License
13 // along with this program. If not, see http://www.gnu.org/licenses/.
14 //
15 
16 #include "Buffer.h"
17 #include "TTEApplicationBase.h"
18 #include "TTE4INETDefs.h"
19 
20 #include "HelperFunctions.h"
21 
22 using namespace TTEthernetModel;
23 
25 
26 simsignal_t Buffer::txPkSignal = SIMSIGNAL_NULL;
27 simsignal_t Buffer::latencySignal = SIMSIGNAL_NULL;
28 
30  destinationGates.clear();
31  receiveCallbacks.clear();
32  transmitCallbacks.clear();
33 }
34 
36 {
37  return 1;
38 }
39 
40 void Buffer::initialize(int stage)
41 {
42  if(stage==0){
43  ev << "Initialize Buffer" << endl;
45  }
46 }
47 
49 {
50  txPkSignal = registerSignal("txPk");
51  latencySignal = registerSignal("latency");
52 }
53 
55 {
56  emit(txPkSignal, 1L);
57 }
58 
59 void Buffer::setIsEmpty(bool empty)
60 {
61  if(ev.isGUI()){
62  if (empty)
63  {
64  getDisplayString().setTagArg("i", 1, "black");
65  getDisplayString().setTagArg("tt", 0, "Buffer is empty");
66  }
67  else
68  {
69  getDisplayString().setTagArg("i", 1, "");
70  getDisplayString().setTagArg("tt", 0, "");
71  }
72  }
73 }
74 
76 {
77  receiveCallbacks[application]=cb;
78 }
79 
81 {
82  if(receiveCallbacks.find(application) == receiveCallbacks.end())
83  return NULL;
84  return receiveCallbacks[application];
85 }
86 
88 {
89  transmitCallbacks[application]=cb;
90 }
91 
93 {
94  if(transmitCallbacks.find(application) == transmitCallbacks.end())
95  return NULL;
96  return transmitCallbacks[application];
97 }
98 
99 EtherFrame* Buffer::getFrame(){
100  return dequeue();
101 }
102 
103  void Buffer::putFrame(EtherFrame* frame){
104  enqueue(frame);
105 }
106 
107 void Buffer::handleMessage(cMessage *msg)
108 {
109  if (msg->arrivedOn("in"))
110  {
111  EtherFrame *frame = check_and_cast<EtherFrame *>(msg);
112  emit(latencySignal, simTime()-msg->getCreationTime());
113  putFrame((EtherFrame*) frame);
114  // Now execute callbacks if there are some
115  for(std::map<TTEApplicationBase*,Callback*>::const_iterator iter = receiveCallbacks.begin();
116  iter != receiveCallbacks.end(); ++iter){
117  iter->first->executeCallback(iter->second);
118  }
119  }
120 }
121 
122 void Buffer::handleParameterChange(const char* parname){
123  destinationGates.clear();
124  if(ev.isGUI()){
125  //TODO check why this does not work
126  //getDisplayString().setTagArg("i2", 0, "");
127  //getDisplayString().setTagArg("tt", 0, "");
128  }
129  std::string destinationGatesString = par("destination_gates").stdstringValue();
130  std::vector<std::string> destinationGatePaths;
131  split(destinationGatesString,',',destinationGatePaths);
132  for(std::vector<std::string>::iterator destinationGatePath = destinationGatePaths.begin();
133  destinationGatePath!=destinationGatePaths.end();destinationGatePath++){
134  cGate* gate = gateByFullPath((*destinationGatePath));
135  if(gate){
136  destinationGates.push_back(gate);
137  }
138  else{
139  if(ev.isGUI()){
140  ev<<"Configuration problem: Gate "<<(*destinationGatePath)<<" could not be resolved!"<<endl;
141  getDisplayString().setTagArg("i2", 0, "status/excl3");
142  getDisplayString().setTagArg("tt", 0, "WARNING: Configuration Problem outgoing gate!");
143  }
144  }
145  }
146 }
147 
148 void Buffer::enqueue(EtherFrame *newFrame)
149 {
150  ev << "Buffer::enqueue not implemented" << endl;
151  throw;
152 }
153 
154 EtherFrame * Buffer::dequeue()
155 {
156  ev << "Buffer::dequeue not implemented" << endl;
157  throw;
158 }