INET Framework for OMNeT++/OMNEST
inet::EthernetSocketIo Class Reference

#include <EthernetSocketIo.h>

Inheritance diagram for inet::EthernetSocketIo:
inet::ApplicationBase inet::EthernetSocket::ICallback inet::OperationalBase inet::OperationalMixin< cSimpleModule > inet::ILifecycle

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessageWhenUp (cMessage *message) override
 
virtual void finish () override
 
virtual void refreshDisplay () const override
 
virtual void setSocketOptions ()
 
virtual void socketDataArrived (EthernetSocket *socket, Packet *packet) override
 Notifies about data arrival, packet ownership is transferred to the callee. More...
 
virtual void socketErrorArrived (EthernetSocket *socket, Indication *indication) override
 Notifies about error indication arrival, indication ownership is transferred to the callee. More...
 
virtual void socketClosed (EthernetSocket *socket) override
 Notifies about the socket closed. More...
 
virtual void handleStartOperation (LifecycleOperation *operation) override
 
virtual void handleStopOperation (LifecycleOperation *operation) override
 
virtual void handleCrashOperation (LifecycleOperation *operation) override
 
- Protected Member Functions inherited from inet::ApplicationBase
virtual bool isInitializeStage (int stage) const override
 
virtual bool isModuleStartStage (int stage) const override
 
virtual bool isModuleStopStage (int stage) const override
 
- Protected Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual int numInitStages () const override
 
virtual void refreshDisplay () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual State getInitialOperationalState () const
 Returns initial operational state: OPERATING or NOT_OPERATING. More...
 
virtual void handleActiveOperationTimeout (cMessage *message)
 
virtual bool isUp () const
 utility functions More...
 
virtual bool isDown () const
 
virtual void setOperationalState (State newState)
 
virtual void scheduleOperationTimeout (simtime_t timeout)
 
virtual void setupActiveOperation (LifecycleOperation *operation, IDoneCallback *doneCallback, State)
 
virtual void delayActiveOperationFinish (simtime_t timeout)
 
virtual void startActiveOperationExtraTime (simtime_t delay=SIMTIME_ZERO)
 
virtual void startActiveOperationExtraTimeOrFinish (simtime_t extraTime)
 
virtual void finishActiveOperation ()
 

Protected Attributes

MacAddress remoteAddress
 
MacAddress localAddress
 
EthernetSocket socket
 
int numSent = 0
 
int numReceived = 0
 
- Protected Attributes inherited from inet::OperationalMixin< cSimpleModule >
State operationalState
 
simtime_t lastChange
 
Operation activeOperation
 
cMessage * activeOperationTimeout
 
cMessage * activeOperationExtraTimer
 

Additional Inherited Members

- Public Member Functions inherited from inet::ApplicationBase
 ApplicationBase ()
 
- Public Member Functions inherited from inet::OperationalMixin< cSimpleModule >
virtual ~OperationalMixin ()
 }@ More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- Public Member Functions inherited from inet::EthernetSocket::ICallback
virtual ~ICallback ()
 
- Protected Types inherited from inet::OperationalMixin< cSimpleModule >
enum  State
 

Member Function Documentation

◆ finish()

void inet::EthernetSocketIo::finish ( )
overrideprotectedvirtual
74 {
75  recordScalar("packets sent", numSent);
76  recordScalar("packets received", numReceived);
77  ApplicationBase::finish();
78 }

◆ handleCrashOperation()

void inet::EthernetSocketIo::handleCrashOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

137 {
138  socket.destroy();
139 }

◆ handleMessageWhenUp()

void inet::EthernetSocketIo::handleMessageWhenUp ( cMessage *  message)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

56 {
57  if (socket.belongsToSocket(message))
58  socket.processMessage(message);
59  else {
60  auto packet = check_and_cast<Packet *>(message);
61  if (packet->findTag<PacketProtocolTag>() == nullptr) {
62  auto packetProtocolTag = packet->addTag<PacketProtocolTag>();
63  packetProtocolTag->setProtocol(&Protocol::unknown);
64  }
65  auto& macAddressReq = packet->addTag<MacAddressReq>();
66  macAddressReq->setDestAddress(remoteAddress);
67  socket.send(packet);
68  numSent++;
69  emit(packetSentSignal, packet);
70  }
71 }

◆ handleStartOperation()

void inet::EthernetSocketIo::handleStartOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

123 {
125  socket.setOutputGate(gate("socketOut"));
126 // TODO breaks if there are queueing components between the application and the socket handling modules
127 // socket.bind(localAddress, remoteAddress, nullptr, true);
128 }

◆ handleStopOperation()

void inet::EthernetSocketIo::handleStopOperation ( LifecycleOperation operation)
overrideprotectedvirtual

Implements inet::OperationalMixin< cSimpleModule >.

131 {
132  socket.close();
133  delayActiveOperationFinish(par("stopOperationTimeout"));
134 }

◆ initialize()

void inet::EthernetSocketIo::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::OperationalMixin< cSimpleModule >.

23 {
25  if (stage == INITSTAGE_LOCAL) {
26  numSent = 0;
27  numReceived = 0;
28  WATCH(numSent);
29  WATCH(numReceived);
30  }
31  else if (stage == INITSTAGE_NETWORK_INTERFACE_CONFIGURATION + 1) {
32  const char *localAddressString = par("localAddress");
33  if (*localAddressString != '\0') {
34  L3Address l3Address;
35  L3AddressResolver addressResolver;
36  addressResolver.tryResolve(localAddressString, l3Address, L3AddressResolver::ADDR_MAC);
37  if (l3Address.getType() == L3Address::MAC)
38  localAddress = l3Address.toMac();
39  else
40  localAddress = MacAddress(localAddressString);
41  }
42  const char *remoteAddressString = par("remoteAddress");
43  if (*remoteAddressString != '\0') {
44  L3Address l3Address;
45  L3AddressResolver addressResolver;
46  addressResolver.tryResolve(remoteAddressString, l3Address, L3AddressResolver::ADDR_MAC);
47  if (l3Address.getType() == L3Address::MAC)
48  remoteAddress = l3Address.toMac();
49  else
50  remoteAddress = MacAddress(remoteAddressString);
51  }
52  }
53 }

◆ numInitStages()

virtual int inet::EthernetSocketIo::numInitStages ( ) const
inlineoverrideprotectedvirtual
26 { return NUM_INIT_STAGES; }

◆ refreshDisplay()

void inet::EthernetSocketIo::refreshDisplay ( ) const
overrideprotectedvirtual
81 {
83  char buf[100];
84  sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent);
85  getDisplayString().setTagArg("t", 0, buf);
86 }

◆ setSocketOptions()

void inet::EthernetSocketIo::setSocketOptions ( )
protectedvirtual
89 {
90  socket.setCallback(this);
91  const char *interface = par("interface");
92  if (interface[0] != '\0') {
93  auto interfaceTable = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
94  auto networkInterface = interfaceTable->findInterfaceByName(interface);
95  if (networkInterface == nullptr)
96  throw cRuntimeError("Cannot find network interface");
97  socket.setNetworkInterface(networkInterface);
98  }
99 }

Referenced by handleStartOperation().

◆ socketClosed()

void inet::EthernetSocketIo::socketClosed ( EthernetSocket socket)
overrideprotectedvirtual

Notifies about the socket closed.

Implements inet::EthernetSocket::ICallback.

117 {
118  if (operationalState == State::STOPPING_OPERATION)
119  startActiveOperationExtraTimeOrFinish(par("stopOperationExtraTime"));
120 }

◆ socketDataArrived()

void inet::EthernetSocketIo::socketDataArrived ( EthernetSocket socket,
Packet packet 
)
overrideprotectedvirtual

Notifies about data arrival, packet ownership is transferred to the callee.

Implements inet::EthernetSocket::ICallback.

102 {
103  emit(packetReceivedSignal, packet);
104  EV_INFO << "Received packet: " << packet << endl;
105  numReceived++;
106  packet->removeTag<SocketInd>();
107  send(packet, "trafficOut");
108 }

◆ socketErrorArrived()

void inet::EthernetSocketIo::socketErrorArrived ( EthernetSocket socket,
Indication indication 
)
overrideprotectedvirtual

Notifies about error indication arrival, indication ownership is transferred to the callee.

Implements inet::EthernetSocket::ICallback.

111 {
112  EV_WARN << "Ignoring Ethernet error report " << indication->getName() << endl;
113  delete indication;
114 }

Member Data Documentation

◆ localAddress

MacAddress inet::EthernetSocketIo::localAddress
protected

Referenced by initialize().

◆ numReceived

int inet::EthernetSocketIo::numReceived = 0
protected

◆ numSent

int inet::EthernetSocketIo::numSent = 0
protected

◆ remoteAddress

MacAddress inet::EthernetSocketIo::remoteAddress
protected

Referenced by handleMessageWhenUp(), and initialize().

◆ socket


The documentation for this class was generated from the following files:
inet::OperationalMixin< cSimpleModule >::operationalState
State operationalState
Definition: OperationalMixin.h:23
inet::SocketBase::setOutputGate
void setOutputGate(cGate *gate)
Sets the gate on which to send messages.
Definition: SocketBase.h:36
inet::EthernetSocket::processMessage
virtual void processMessage(cMessage *msg) override
Examines the message, takes ownership, and updates socket state.
Definition: EthernetSocket.cc:40
inet::EthernetSocketIo::numReceived
int numReceived
Definition: EthernetSocketIo.h:23
inet::OperationalMixin< cSimpleModule >::initialize
virtual void initialize(int stage) override
Definition: OperationalMixinImpl.h:26
inet::EthernetSocketIo::socket
EthernetSocket socket
Definition: EthernetSocketIo.h:21
inet::packetSentSignal
simsignal_t packetSentSignal
Definition: Simsignals.cc:96
inet::EthernetSocketIo::setSocketOptions
virtual void setSocketOptions()
Definition: EthernetSocketIo.cc:88
inet::L3AddressResolver::ADDR_MAC
@ ADDR_MAC
Definition: L3AddressResolver.h:72
PacketProtocolTag
removed DscpReq Ipv4ControlInfo Ipv6ControlInfo up L3AddressInd DispatchProtocolReq L4PortInd Ipv4ControlInfo Ipv6ControlInfo down PacketProtocolTag
Definition: IUdp-gates.txt:25
inet::INITSTAGE_NETWORK_INTERFACE_CONFIGURATION
INET_API InitStage INITSTAGE_NETWORK_INTERFACE_CONFIGURATION
Initialization of network interfaces includes:
inet::packetReceivedSignal
simsignal_t packetReceivedSignal
Definition: Simsignals.cc:97
inet::EthernetSocketIo::remoteAddress
MacAddress remoteAddress
Definition: EthernetSocketIo.h:19
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::EthernetSocketIo::localAddress
MacAddress localAddress
Definition: EthernetSocketIo.h:20
inet::SocketBase::close
virtual void close() override
Unbinds the socket.
Definition: SocketBase.cc:43
inet::SocketBase::send
virtual void send(Packet *packet) override
Sends a data packet to the address and port specified previously in a connect() call.
Definition: SocketBase.cc:36
inet::OperationalMixin< cSimpleModule >::delayActiveOperationFinish
virtual void delayActiveOperationFinish(simtime_t timeout)
Definition: OperationalMixinImpl.h:161
inet::OperationalMixin< cSimpleModule >::startActiveOperationExtraTimeOrFinish
virtual void startActiveOperationExtraTimeOrFinish(simtime_t extraTime)
Definition: OperationalMixinImpl.h:179
inet::EthernetSocket::setNetworkInterface
void setNetworkInterface(NetworkInterface *networkInterface)
Definition: EthernetSocket.h:67
inet::L3Address::MAC
@ MAC
Definition: L3Address.h:38
inet::EthernetSocket::setCallback
void setCallback(ICallback *callback)
Sets a callback object, to be used with processMessage().
Definition: EthernetSocket.h:65
inet::OperationalMixin< cSimpleModule >::refreshDisplay
virtual void refreshDisplay() const override
Definition: OperationalMixinImpl.h:200
inet::SocketBase::belongsToSocket
virtual bool belongsToSocket(cMessage *msg) const override
Returns true if the message belongs to this socket instance.
Definition: SocketBase.cc:61
inet::SocketBase::destroy
virtual void destroy() override
Notify the protocol that the owner of ISocket has destroyed the socket.
Definition: SocketBase.cc:52
inet::EthernetSocketIo::numSent
int numSent
Definition: EthernetSocketIo.h:22
inet::Protocol::unknown
static const Protocol unknown
Definition: Protocol.h:133