package com.lumaserv.bgp; import com.lumaserv.bgp.protocol.BGPPacket; import com.lumaserv.bgp.protocol.message.BGPNotification; import com.lumaserv.bgp.protocol.message.BGPOpen; import lombok.Getter; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; public class BGPServer implements Runnable { final ServerSocket serverSocket; @Getter final List sessionConfigurations = new ArrayList<>(); final List sessions = new ArrayList<>(); public BGPServer() throws IOException { this(179); } public BGPServer(int port) throws IOException { serverSocket = new ServerSocket(port); } private static boolean checkEqual(byte[] a, byte[] b) { if(a == b) return true; if(a == null || b == null) return false; if(a.length != b.length) return false; for(int i=0; i socket.getInetAddress().equals(c.getRemoteAddr())) .findFirst() .orElse(null); System.out.println("Config: " + config); if(config == null) { System.out.println("Peer not found:" + socket.getInetAddress()); socket.close(); continue; } BGPSession session = new BGPSession(this, socket, config); sessions.add(session); session.getFsm().getCurrentState().automaticStartPassive(); // System.out.println("tcpConnectionConfirmed in " + fsm.getCurrentState()); // fsm.getCurrentState().tcpConnectionConfirmed(); } catch (IOException ex) { if (serverSocket.isClosed()) break; 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 { for (BGPSession session : sessions) { if (session.getConfiguration().equals(config)) { // Already connected return false; } } try { BGPSession session = new BGPSession(this, config, host, port); sessions.add(session); session.getFsm().getCurrentState().automaticStart(); return true; } catch (IOException ex) { ex.printStackTrace(); throw(ex); } } public void shutdown() { for(BGPSession session : sessions) { session.automaticStop(); session.cleanup(); } sessions.clear(); if (serverSocket != null) { try { serverSocket.close(); } catch(IOException ex) { ex.printStackTrace(); } } } }