OverSim
I3Composite.cc
Go to the documentation of this file.
1 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
23 #include "I3BaseApp.h"
24 
25 using namespace std;
26 
27 struct I3CompositeMessage : public cPacket {
28  string sentence;
29 
31  return new I3CompositeMessage(*this);
32  }
33 };
34 
43 class I3Composite : public I3BaseApp
44 {
45 private:
46  static int index; // HACK Change to use index module when it's done
47 public:
48  int myIndex;
49  cMessage *sendPacketTimer;
50 
51  void initializeApp(int stage);
52  void initializeI3();
53  void deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg);
54  void handleTimerEvent(cMessage *msg);
55  void createMessage();
56 };
57 
58 int I3Composite::index = 0;
59 
61 {
62  myIndex = index++;
63 
64 }
65 
67 {
68  if (myIndex == 0) {
69  sendPacketTimer = new cMessage("packet timer");
70  scheduleAt(simTime() + 50, sendPacketTimer);
71  }
72 
73  ostringstream os;
74  os << "Composite " << myIndex;
75 
76  I3Identifier identifier(os.str());
77  insertTrigger(identifier);
78 }
79 
82 
83  cmsg->sentence = "";
84 
85  I3Identifier id0("Composite 0"),
86  id1("Composite 1"),
87  id2("Composite 2"),
88  id3("Composite 3"),
89  id4("Composite 4");
90 
91  I3IdentifierStack stack;
92  stack.push(id0);
93  stack.push(id4);
94  stack.push(id3);
95  stack.push(id2);
96  stack.push(id1);
97 
98  sendPacket(stack, cmsg);
99 }
100 
101 void I3Composite::handleTimerEvent(cMessage *msg)
102 {
103  if (myIndex == 0) { // only the first node
104  getParentModule()->bubble("Starting chain!");
105  createMessage();
106  }
107  delete msg;
108 }
109 
110 void I3Composite::deliver(I3Trigger &trigger, I3IdentifierStack &stack, cPacket *msg)
111 {
112  I3CompositeMessage *cmsg = check_and_cast<I3CompositeMessage*>(msg);
113 
114  switch (myIndex) {
115  case 0:
116  {
117  string final = "Final sentence: " + cmsg->sentence;
118  getParentModule()->bubble(final.c_str());
119  delete msg;
120  createMessage();
121  return;
122  }
123  case 1:
124  getParentModule()->bubble("Adding 'He pounds'");
125  cmsg->sentence += "He pounds ";
126  break;
127  case 2:
128  getParentModule()->bubble("Adding 'his fists'");
129  cmsg->sentence += "his fists ";
130  break;
131  case 3:
132  getParentModule()->bubble("Adding 'against'");
133  cmsg->sentence += "against ";
134  break;
135  case 4:
136  getParentModule()->bubble("Adding 'the posts'");
137  cmsg->sentence += "the posts ";
138  break;
139  default:
140  delete msg;
141  return;
142  }
143  sendPacket(stack, cmsg);
144 }
145 
146 
148 
149