Finite State Machine – Arivi Network Layer
States
Arivi network layer has 5 states which are listed below:IDLE state
This state indicates the existence of a connection instance and not the connection itself has it not yet created ( i.e no peer is connected using the current instance which is in IDLE state ) node is waiting for higher or lower layer to either start a event for a handshake initiation or to accept a handshake request by other peers.INVITED state
This state is only valid in the perspective of initiator ( i.e a node which starts a handshake process). It is set with a timer called Handshake init timer of about 30 seconds to receive a handshake response parcel from the recipient. In the event that response is not received before timer expires then state will change to terminated. Note: Connection is not yet formed between peers when they are either in IDLE or INVITED state.ESTABLISHED state
A connection is said to be completely formed when it gets associated with ESTABLISHED state either when node’s handshake init request was accepted by a peer node to form a secure connection or when it itself accepts handshake request of a peer to do the same. Established state is mainly concerned with exchange of data packets hence whenever a data exchange takes place data message timer is set which expires in 60 seconds if there is no response from the connected peer in this time frame then state will be changed to PINGED state.PINGED state
If a node’s connection is in pinged state then it will send ping parcels to its peer with timer set to 30 seconds, if the peer replies with the data parcel or pong parcel then connection will go back to ESTABLISHED state but in the event peer fails to reply with either data or pong parcel in under 30 seconds connection will be marked stale and move to TERMINATED state.TERMINATED state
If this state is encountered then we assume connection was destroyed and we clear up the socket.Events
Event drives the state change in the connection instance when paired with states each pair results in different outcome. Event is written to a queue called eventQueue. Each connection will have a individual eventQueue and there are about 10 possible events which will result in state change, which are:- Handshake initiation event
HS_INIT_EVENT
- Key exchange initiation event
Key_Ex_INIT_EVENT
- Key exchange response event
Key_Ex_RESP_EVENT
- Send data event
Send_DATA_EVENT
- Receive data event
Recv_DATA_EVENT
- Ping data event
Ping_EVENT
- Pong data event
Pong_EVENT
- Clean up event
Clean_Up_EVENT
- Terminate connection event
Terminate_EVENT
- Timeout event
TimeOut_EVENT : {HandS, DataEx, PING}

When a connection instance is in a IDLE state it will be waiting for a event to be written into a eventQueue. There are only two possible events which can drive a valid state change if the current state is IDLE. first is Handshake Initiation Event which occurs in initiator node and second is Key Exchange Initiation Event which is with respect to a recipient node.
(IDLE,HS_INIT_EVENT)
This state and event pair creates handshake initiation parcel and sends it to the required peer node while driving a state change to INVITED from IDLE this pair of state and event classifies the node as a Initiator.
(IDLE,Key_Ex_INIT_EVENT)
This state and event pair occurs when a node in IDLE receives a handshake initiation request from a peer node (i.e a initiator) in turn classifying the parent node as a recipient. If the request is not discarded by the node then it will send a parcel accepting the handshake response to the initiator while also driving a state change to ESTABLISHED from IDLE.
The INVITED state can only be entered by a initiator after sending handshake initiation request. When initiator sends the request it sets a timer for 30 seconds known as Handshake Timer.
(INVITED, Key_Ex_RESP_EVENT)
In the event that the request is received before the handshake timer runs out then node will drive a state change to ESTABLISHED from INVITED.
(INVITED, TimeOut_EVENT.HandS)
If the initiator does not get a response for handshake request back before timer runs out then state change to Terminated from INVITED will take place.
ESTABLISHED state is where node exchange data using a secure connection. Node after sending or receiving the data will start a timer set to 60 seconds which is known as a Data exchange timer.
(ESTABLISHED, Send_DATA_EVENT)
This state and event pair notifies the node that upper layer wants to send data to the connected peer node. Data for it will be read from a another Queue called dataQueue assigned for each connection. After sending the data node the data exchange timer resets.
(ESTABLISHED, TimeOut_EVENT.DataEx)
If node does not receive any data from the peer node before the timer expires then node will drive a state change to PINGED from ESTABLISHED.
(ESTABLISHED, Recv_DATA_EVENT)
This state and event pair is executed when there is data to be read and processed after this event is completed data exchange timer is reset.
(ESTABLISHED, Terminate_EVENT)
When a node receives a ‘bye’ parcel i.e peer node requesting to break the connection. Then this state and event pair is executed later making the node to drive a state change to TERMINATED from ESTABLISHED.
PINGED state is entered by any connection instance from ESTABLISHED state when the data exchange timer expires.
(PINGED, Ping_EVENT)
As soon as node enters a PINGED state it sends a ping parcel to the peer to check connectivity between them and sets a timer of 30 seconds known as Ping timer.
(PINGED, Pong_EVENT)
When a node in PINGED state receives a pong parcel from peer node before the ping timer expires the state changes to ESTABLISHED from PINGED and data exchange timer is reset.
(PINGED, Recv_DATA_EVENT)
This event is similar to PONG_EVENT but here the data is returned back which needs additional processing with the change in state.
(PINGED, TimeOut_EVENT.PING)
In the case of peer fails to reply before ping timer expires the state is changed to TERMINATED from PINGED.
(TERMINATED, Clean_Up_EVENT)
Whenever a instance enters the TERMINATED state it destroys the newly created socket for connection and kills the FSM thread irrespective of event or state it came from.