diff options
Diffstat (limited to 'src/main/java/com/lumaserv/bgp/BGPServer.java')
-rw-r--r-- | src/main/java/com/lumaserv/bgp/BGPServer.java | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index b8f934a..695ef96 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -42,34 +42,47 @@ public class BGPServer implements Runnable { while (true) { try { Socket socket = serverSocket.accept(); - BGPPacket packet = BGPPacket.read(socket.getInputStream()); - if(packet.getType() != BGPPacket.Type.OPEN) - continue; - BGPOpen request = new BGPOpen(packet.getMessage()); + System.out.println("Accept"); + BGPSessionConfiguration config = sessionConfigurations.stream() - .filter(c -> c.getRemoteAs() == request.getAsn()) + .filter(c -> socket.getInetAddress().equals(c.getRemoteAddr())) .findFirst() .orElse(null); - if(config == null) + System.out.println("Config: " + config); + if(config == null) { + System.out.println("Peer not found:" + socket.getInetAddress()); + socket.close(); continue; - BGPSession session = new BGPSession(socket, config); - config.getListener().onOpen(session); - BGPOpen response = new BGPOpen() - .setAsn(config.getLocalAs()) - .setHoldTime(request.getHoldTime()) - .setVersion(request.getVersion()) - .setIdentifier(config.getLocalIdentifier()); - try { - socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response.build()).build()); - } catch (IOException e) { - e.printStackTrace(); } - session.keepAlive(); - new Thread(session).start(); + + BGPFsm fsm = new BGPFsm(); + BGPSession session = new BGPSession(socket, config, fsm); + fsm.setSession(session); + System.out.println("automaticStartPassive"); + fsm.getCurrentState().automaticStartPassive(); + + // System.out.println("tcpConnectionConfirmed in " + fsm.getCurrentState()); + // fsm.getCurrentState().tcpConnectionConfirmed(); } catch (IOException ex) { ex.printStackTrace(); } } } + public boolean connect(BGPSessionConfiguration config, String host) throws IOException { + return connect(config, host, 179); + } + + public boolean connect(BGPSessionConfiguration config, String host, int port) throws IOException { + try { + BGPFsm fsm = new BGPFsm(); + BGPSession session = new BGPSession(config, fsm, host, port); + fsm.setSession(session); + fsm.getCurrentState().automaticStart(); + return true; + } catch (IOException ex) { + ex.printStackTrace(); + throw(ex); + } + } } |