diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-03-11 13:38:05 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-03-12 22:53:22 +0100 |
commit | 389447b4e02f380208eae8435f5e2653399ecfea (patch) | |
tree | c6b39cd4e07569f238f102ae538cfd928b90a621 | |
parent | 6303d84ef047721e972ae7407e94320afeb3db24 (diff) |
Allow stop in opensent and established states
4 files changed, 90 insertions, 3 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPFsm.java b/src/main/java/com/lumaserv/bgp/BGPFsm.java index a2143bd..7929d36 100644 --- a/src/main/java/com/lumaserv/bgp/BGPFsm.java +++ b/src/main/java/com/lumaserv/bgp/BGPFsm.java @@ -244,6 +244,36 @@ public class BGPFsm { } class OpenSent extends State { + protected void stop(boolean isAutomatic) { + try { + BGPNotification notification = new BGPNotification() + .setMajorErrorCode(BGPNotification.Error.CEASE.getCode()); + session.sendNotification(notification); + } catch (IOException ex) { + } + setConnectRetryTimer(0); + // - releases all the BGP resources, + session.dropConnection(); + if (isAutomatic) { + connectRetryCounter++; + // - (optionally) performs peer oscillation damping if the + // DampPeerOscillations attribute is set to TRUE, and + } else { + connectRetryCounter = 0; + } + setState(IDLE); + } + + @Override + void manualStop() { + stop(false); + } + + @Override + void automaticStop() { + stop(true); + } + @Override void openMsg(BGPOpen msg) { System.out.println("openMsg:" + msg); @@ -311,6 +341,37 @@ public class BGPFsm { } class Established extends State { + protected void stop(boolean isAutomatic) { + try { + BGPNotification notification = new BGPNotification() + .setMajorErrorCode(BGPNotification.Error.CEASE.getCode()); + session.sendNotification(notification); + } catch (IOException ex) { + } + setConnectRetryTimer(0); + // - deletes all routes associated with this connection, + // - releases all BGP resources, + session.dropConnection(); + if (isAutomatic) { + connectRetryCounter++; + // - (optionally in automaticStop) performs peer oscillation damping if the + // DampPeerOscillations attribute is set to TRUE, and + } else { + connectRetryCounter = 0; + } + setState(IDLE); + } + + @Override + void manualStop() { + stop(false); + } + + @Override + void automaticStop() { + stop(true); + } + @Override void keepAliveMsg() { setHoldTimer(holdTime); diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index 695ef96..80ec7ff 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -15,6 +15,7 @@ public class BGPServer implements Runnable { final ServerSocket serverSocket; @Getter final List<BGPSessionConfiguration> sessionConfigurations = new ArrayList<>(); + final List<BGPSession> sessions = new ArrayList<>(); public BGPServer() throws IOException { this(179); @@ -58,6 +59,7 @@ public class BGPServer implements Runnable { BGPFsm fsm = new BGPFsm(); BGPSession session = new BGPSession(socket, config, fsm); fsm.setSession(session); + sessions.add(session); System.out.println("automaticStartPassive"); fsm.getCurrentState().automaticStartPassive(); @@ -78,6 +80,7 @@ public class BGPServer implements Runnable { BGPFsm fsm = new BGPFsm(); BGPSession session = new BGPSession(config, fsm, host, port); fsm.setSession(session); + sessions.add(session); fsm.getCurrentState().automaticStart(); return true; } catch (IOException ex) { @@ -85,4 +88,10 @@ public class BGPServer implements Runnable { throw(ex); } } + + public void shutdown() { + for(BGPSession session : sessions) { + session.automaticStop(); + } + } } diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java index 21a5564..c6a8cd2 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -47,6 +47,10 @@ public class BGPSession implements Runnable { this.port = port; } + public void automaticStop() { + fsm.getCurrentState().automaticStop(); + } + public void keepAlive() { try { outputStream.write(new BGPPacket().setType(BGPPacket.Type.KEEPALIVE).setMessage(new byte[0]).build()); @@ -94,6 +98,10 @@ public class BGPSession implements Runnable { thread.start(); } + void sendNotification(BGPNotification notification) throws IOException { + outputStream.write(new BGPPacket().setType(BGPPacket.Type.NOTIFICATION).setMessage(notification.build()).build()); + } + void sendOpen(BGPOpen request) throws IOException { outputStream.write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request.build()).build()); System.out.println("Sent open"); @@ -167,9 +175,10 @@ System.out.println("Sent open"); } System.out.println("Closed!"); } catch (IOException ex) { - if (!closed) + if (!closed) { fsm.getCurrentState().tcpConnectionFails(); - ex.printStackTrace(); + ex.printStackTrace(); + } closed = true; configuration.getListener().onClose(this); } diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java index 19272e5..94e3fae 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java +++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java @@ -1,10 +1,11 @@ package com.lumaserv.bgp.protocol.message; -import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; import lombok.Getter; import lombok.Setter; @Getter +@NoArgsConstructor @Setter public class BGPNotification { @@ -49,6 +50,13 @@ public class BGPNotification { this.minorErrorCode = message[1]; } + public byte[] build() { + byte[] message = new byte[2]; + message[0] = majorErrorCode; + message[1] = minorErrorCode; + return message; + } + @Override public String toString() { return "" + majorErrorCode + ":" + minorErrorCode; |