OverSim
BaseTcpSupport.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010 Institut fuer Telematik, Karlsruher Institut fuer Technologie (KIT)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include "BaseTcpSupport.h"
25 #include <GlobalStatisticsAccess.h>
26 #include <UDPSocket.h>
27 #include <TCPCommand_m.h>
28 #include <omnetpp.h>
29 
31 {
32  TCPSocket* socket = sockets.findSocketFor(msg);
33 
34  if (socket == NULL) {
35  socket = new TCPSocket(msg);
36  socket->setCallbackObject(this);
37  socket->setOutputGate(getTcpOut());
38  sockets.addSocket(socket);
39  TransportAddress newAddress =
40  TransportAddress(socket->getRemoteAddress(),
41  socket->getRemotePort());
42 
43  socket->processMessage(msg);
44  handleIncomingConnection(newAddress);
45  } else {
46  socket->processMessage(msg);
47  }
48 }
49 
51 {
52  if (sockets.size() != 0) {
53  return;
54  }
55 
56  TCPSocket* newSocket = new TCPSocket();
57  newSocket->bind(port);
58  newSocket->setOutputGate(getTcpOut());
59  newSocket->setCallbackObject(this);
60  newSocket->listen();
61  sockets.addSocket(newSocket);
62 }
63 
65 {
66  TCPSocket* newSocket = sockets.findSocketFor(address.getIp(),
67  address.getPort());
68  if (newSocket == NULL) {
69  return false;
70  } else if (newSocket->getState() >= TCPSocket::PEER_CLOSED) {
71  return false;
72  } else return true;
73 }
74 
76 {
77  if (isAlreadyConnected(address)) {
78  return;
79  }
80 
81  TCPSocket* newSocket = new TCPSocket();
82  newSocket->setOutputGate(getTcpOut());
83  newSocket->setCallbackObject(this);
84  newSocket->connect(address.getIp(), address.getPort());
85 
86  sockets.addSocket(newSocket);
87 }
88 
89 void BaseTcpSupport::sendTcpData(cPacket *msg, TransportAddress address)
90 {
91  if (!isAlreadyConnected(address)) {
93  return;
94  }
95 
96  TCPSocket* socket = sockets.findSocketFor(address.getIp(),
97  address.getPort());
98 
99  if (socket->getState() == TCPSocket::CONNECTED) {
100  socket->send(msg);
101  }
102 
103  transQueue::iterator tx = queuedTx.find(address);
104 
105  if (tx != queuedTx.end()) {
106  tx->second->push_back(msg);
107  } else {
108  msgQueue* newQueue = new msgQueue();
109  newQueue->push_back(msg);
110  queuedTx[address] = newQueue;
111  }
112 }
113 
115 {
116  if (code == NO_EST_CONNECTION) {
117  establishTcpConnection(address);
118  }
119 }
120 
122  bool urgent)
123 {
124 }
125 
127 {
128 }
129 
130 void BaseTcpSupport::socketDataArrived(int connId, void *yourPtr, cPacket *msg,
131  bool urgent)
132 {
133  TCPSocket* socket = sockets.findSocketFor(connId);
134  TransportAddress remoteAddress(socket->getRemoteAddress(),
135  socket->getRemotePort());
136 
137  handleDataReceived(remoteAddress, msg, urgent);
138 }
139 
140 void BaseTcpSupport::socketEstablished(int connId, void *yourPtr)
141 {
142  TCPSocket* socket = sockets.findSocketFor(connId);
143 
144  if (socket == NULL) {
145  return;
146  }
147 
148  TransportAddress remoteAddress(socket->getRemoteAddress(),
149  socket->getRemotePort());
150 
151  transQueue::iterator tx = queuedTx.find(remoteAddress);
152 
153  if (tx != queuedTx.end()) {
154  for (uint32 i = 0 ; i < tx->second->size(); i++) {
155  socket->send(tx->second->at(i));
156  }
157 
158  tx->second->clear();
159  delete tx->second;
160  queuedTx.erase(remoteAddress);
161  }
162 }
163 
164 void BaseTcpSupport::socketPeerClosed(int connId, void *yourPtr)
165 {
166  TCPSocket* socket = sockets.findSocketFor(connId);
167  TransportAddress remoteAddress(socket->getRemoteAddress(),
168  socket->getRemotePort());
169 
170  if (socket->getState() == TCPSocket::PEER_CLOSED) {
171  socket->close();
172  handleConnectionEvent(PEER_CLOSED, remoteAddress);
173  } else if (socket->getState() == TCPSocket::CLOSED) {
174  sockets.removeSocket(socket);
176  }
177 }
178 
179 void BaseTcpSupport::socketFailure(int connId, void *yourPtr, int code)
180 {
181  TCPSocket* socket = sockets.findSocketFor(connId);
182  TransportAddress remoteAddress(socket->getRemoteAddress(),
183  socket->getRemotePort());
184 
185  if (code == TCP_I_CONNECTION_REFUSED) {
186  handleConnectionEvent(PEER_REFUSED, remoteAddress);
187  } else if (code == TCP_I_TIMED_OUT) {
188  handleConnectionEvent(PEER_TIMEDOUT, remoteAddress);
189  } else if (code == TCP_I_CONNECTION_RESET) {
190  handleConnectionEvent(CONNECTION_RESET, remoteAddress);
191  } else {
192  throw new cRuntimeError("Invalid error code on socketFailure.");
193  }
194 }
195 
197 {
198  if (!isAlreadyConnected(address)) {
199  return;
200  }
201 
202  TCPSocket* oldSocket = sockets.findSocketFor(address.getIp(),
203  address.getPort());
204 
205  oldSocket->close();
206 }