TTEthernet Model for INET Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
TTBuffer.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 "TTBuffer.h"
17 #include <TTEScheduler.h>
18 #include <TTBufferEmpty_m.h>
19 #include "TTEApplicationBase.h"
20 
21 using namespace TTEthernetModel;
22 
24 
26 {
27  actionTimeEvent = new SchedulerActionTimeEvent("TTBuffer Scheduler Event", ACTION_TIME_EVENT);
28 }
29 
31 {
32  cancelEvent(actionTimeEvent);
33  delete actionTimeEvent;
34 }
35 
37 {
38  return 2;
39 }
40 
41 void TTBuffer::initialize(int stage)
42 {
43  CTBuffer::initialize(stage);
44  if(stage==1)
45  {
46  ev << "Initialize TTBuffer" << endl;
47 
48  //Register Event
49  TTEScheduler *tteScheduler = (TTEScheduler*) getParentModule()->getSubmodule("tteScheduler");
50  //Dirty FIX
51  //TODO find out what is wrong here!
52  if(actionTimeEvent->isScheduled())
53  cancelEvent(actionTimeEvent);
54  actionTimeEvent->setDestinationGate(gate("schedulerIn"));
55  tteScheduler->registerEvent(actionTimeEvent);
56 
57  setIsEmpty(true);
58  return;
59  }
60 }
61 
62 void TTBuffer::handleMessage(cMessage *msg)
63 {
64  bool arrivedOnSchedulerIn = msg->arrivedOn("schedulerIn");
65 
67 
68  if (arrivedOnSchedulerIn && msg->getKind() == ACTION_TIME_EVENT && destinationGates.size() > 0)
69  {
70  cMessage *outgoingMessage = getFrame();
71  //Send Message
72  for (std::list<cGate*>::iterator destGate = destinationGates.begin(); destGate != destinationGates.end(); ++destGate)
73  {
74  if (outgoingMessage)
75  {
76  sendDirect(outgoingMessage->dup(), *destGate);
77  }
78  else
79  {
80  sendDirect(new TTBufferEmpty("TT Buffer Empty"), *destGate);
81  }
82  }
83  if(gate("out")->isConnected()){
84  if (outgoingMessage){
85  send(outgoingMessage->dup(),"out");
86  }
87  else{
88  send(new TTBufferEmpty("TT Buffer Empty"),"out");
89  }
90  }
91  if (outgoingMessage)
92  {
94  delete outgoingMessage;
95 
96  // Now execute transmit callbacks if there are some
97  for(std::map<TTEApplicationBase*,Callback*>::const_iterator iter = transmitCallbacks.begin();
98  iter != transmitCallbacks.end(); ++iter){
99  iter->first->executeCallback(iter->second);
100  }
101  }
102  //Reregister scheduler
103  TTEScheduler *tteScheduler = (TTEScheduler*) getParentModule()->getSubmodule("tteScheduler");
104  tteScheduler->registerEvent(static_cast<SchedulerActionTimeEvent *>(msg), true);
105  }
106 }
107 
108 void TTBuffer::handleParameterChange(const char* parname){
110 
111  if(actionTimeEvent)
112  actionTimeEvent->setAction_time(par("sendWindowStart").doubleValue());
113 }