-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageListener.java
More file actions
192 lines (176 loc) · 7.64 KB
/
MessageListener.java
File metadata and controls
192 lines (176 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bfd.fsm;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.BitSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class is to generate a message listener that listens for incoming BFD or ECHO packets
* When a BFD message is recieved the sessions to which it belongs is updated with the MessageRecived function
* @author angelos
*/
public class MessageListener implements Runnable
{
Thread t;
String threadName;
String serverName = "localhost";
private final ServerSocket serverSocket;
SessionHandler sh=new SessionHandler();
MessageListener(String name,int port) throws IOException{
threadName = name;
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(0);
System.out.println("Creating " + threadName );
}
public void StartListener(){
System.out.println("Starting a message listener" );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
@Override
public void run() {
System.out.println("Running " + threadName );
while(true){
try {
System.out.println("Connecting to " + serverName);
Socket server;
server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataInputStream in =new DataInputStream(server.getInputStream());
//When sending and recieving messages send or recieve the length first as an integer
int length=in.readInt();
byte[] data = new byte[length];
in.readFully(data);
BitSet bits = new BitSet();
for (int i = 0; i < data.length * 8; i++) {
if ((data[data.length - i / 8 - 1] & (1 << (i % 8))) > 0) {
bits.set(i);
}
}
System.out.println(" Message recieved is "+bits);
BFDmessage msg=new BFDmessage();
msg.setBFDmessage(bits);
BFDMessageRecieved(msg);
server.close();
} catch (IOException ex) {
Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void BFDMessageRecieved(BFDmessage msg){
//1.Check if the message is a valid BFD message
//2.Check if a session exists based on the discriminator values
//3.Update this session
ValidityCheck check=new ValidityCheck();
//If the message is valid continue with finding and
//updating the session values
if(check.ValidityCheck(msg,sh)){
for (int i=0; i<sh.getSessions().size();i++){
if (convert(msg.getYourDiscriminator())==sh.getSession(i).getRemoteDiscr()){
if (convert(msg.getMyDiscriminator())==sh.getSession(i).getLocalDiscr()){
//Session found
BFDSession tmpSession;
tmpSession=sh.getSession(i);
//Set remote Discriminator value
tmpSession.setRemoteDiscr(convert(msg.getMyDiscriminator()));
//Set remote state value
tmpSession.setRemoteSessionState(convert(msg.getState()));
//Set remote demand mode
if (msg.getDemand()) tmpSession.setRemoteDemandMode(1);
else tmpSession.setRemoteDemandMode(0);
//Set remote min rx interval
tmpSession.setRemoteMinRxInterval(convert(msg.getRequiredMinRxInterval()));
//Cease ECHO if required min echo rx interval is zeto
if (convert(msg.getRequiredMinEchoRxInterval())==0){
//Here stop echo transmission
}
//Terminate Poll if Poll is on in the local system and Final has been recieved
if(msg.getFinal()){
//Check if Poll sequence is enabled and terminate
}
// UPDATE the timers
//The FSM implementaion see page 35 of RFC
if (tmpSession.getSessionState()==0){
//Discard the packet
}
if (convert(msg.getState())==0){
if (tmpSession.getSessionState()!=1){
tmpSession.setLocalDiag(3);
tmpSession.setSessionState(1);
}
}
else {
if (tmpSession.getSessionState()==1){
if (convert(msg.getState())==1){
tmpSession.setSessionState(2);
}
else if(convert(msg.getState())==2){
tmpSession.setSessionState(3);
}
}
if (tmpSession.getSessionState()==2){
if ((convert(msg.getState())==2)||(convert(msg.getState())==3)){
tmpSession.setSessionState(3);
}
}
if (tmpSession.getSessionState()==3){
if (convert(msg.getState())==1){
tmpSession.setLocalDiag(3);
tmpSession.setSessionState(1);
}
}
}
//ADD
//Check to see if Demand mode should become active or not
//Cease BFD messages if remote demand is 1 local session state is UP
//and remote session state is UP
if(tmpSession.getRemoteDemandMode()==1){
if(tmpSession.getSessionState()==3){
if(tmpSession.getRemoteSessionState()==3){
//
//CEASE PERIODIC BFD MESSAGES
//
}
}
}
//Send BFD messages if remote demand mode is 0 or local session sate is not UP or remote session state is not UP
if((tmpSession.getRemoteDemandMode()==0)||(tmpSession.getRemoteSessionState()!=3)||(tmpSession.getSessionState()!=3)){
//
//Local system must send periodic BFD messages
//
}
if (msg.getPoll()){
//
//Send a BFD controll packet with P=0 and F=1
//
}
}
}
}
}
//IF the packet has not been discarded then UPDATE timers
}
//Convert from Bitset to int
public static int convert(BitSet bits) {
int value = 0;
int j=0;
for (int i = bits.length()-1; i >=0 ; --i) {
if (bits.get(i)){
value=value+ (int)Math.pow(2, j);
}
j++;
}
return value;
}
}