/** {@inheritDoc} */ public TransportMapping createTransportMapping(TransportIpAddress transportIpAddress) throws IOException { return new DefaultTcpTransportMapping((TcpAddress) transportIpAddress); } }
protected void socketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException { logger.debug("Socket closed remotely"); sk.cancel(); readChannel.close(); TransportStateEvent e = new TransportStateEvent(DefaultTcpTransportMapping.this, incomingAddress, TransportStateEvent. STATE_DISCONNECTED_REMOTELY, null); fireConnectionStateChanged(e); sockets.remove(incomingAddress); }
private void dispatchMessage(TcpAddress incomingAddress, ByteBuffer byteBuffer, long bytesRead) { byteBuffer.flip(); if (logger.isDebugEnabled()) { logger.debug("Received message from " + incomingAddress + " with length " + bytesRead + ": " + new OctetString(byteBuffer.array(), 0, (int)bytesRead).toHexString()); } ByteBuffer bis; if (isAsyncMsgProcessingSupported()) { byte[] bytes = new byte[(int)bytesRead]; System.arraycopy(byteBuffer.array(), 0, bytes, 0, (int)bytesRead); bis = ByteBuffer.wrap(bytes); } else { bis = ByteBuffer.wrap(byteBuffer.array(), 0, (int) bytesRead); } fireProcessMessage(incomingAddress, bis); }
public ServerThread() throws IOException { buf = new byte[getMaxInboundMessageSize()]; // Selector for incoming requests selector = Selector.open(); if (serverEnabled) { // Create a new server socket and set to non blocking mode ssc = ServerSocketChannel.open(); ssc.configureBlocking(false); // Bind the server socket InetSocketAddress isa = new InetSocketAddress(tcpAddress.getInetAddress(), tcpAddress.getPort()); setSocketOptions(ssc.socket()); ssc.socket().bind(isa); // Register accepts on the server socket with the selector. This // step tells the selector that the socket wants to be put on the // ready list when accept operations occur, so allowing multiplexed // non-blocking I/O to take place. ssc.register(selector, SelectionKey.OP_ACCEPT); } }
/** * Listen for incoming and outgoing requests. If the {@code serverEnabled} * member is {@code false} the server for incoming requests is not * started. This starts the internal server thread that processes messages. * * @throws SocketException * when the transport is already listening for incoming/outgoing messages. * @throws IOException * if the listen port could not be bound to the server thread. */ public synchronized void listen() throws java.io.IOException { if (server != null) { throw new SocketException("Port already listening"); } serverThread = new ServerThread(); if (logger.isInfoEnabled()) { logger.info("TCP address " + getListenAddress() + " bound successfully"); } server = SNMP4JSettings.getThreadFactory().createWorkerThread( "DefaultTCPTransportMapping_" + getAddress(), serverThread, true); if (getConnectionTimeout() > 0) { // run as daemon socketCleaner = SNMP4JSettings.getTimerFactory().createTimer(); } server.run(); }
entry.addRegistration(selector, SelectionKey.OP_READ); sockets.put(incomingAddress, entry); timeoutSocket(entry); TransportStateEvent e = new TransportStateEvent(DefaultTcpTransportMapping.this, STATE_CONNECTED, null); fireConnectionStateChanged(e); if (e.isCancelled()) { logger.warn("Incoming connection cancelled"); if (!readMessage(sk, readChannel, incomingAddress)) { SocketEntry entry = (SocketEntry) sk.attachment(); if ((entry != null) && (getMaxBusyLoops() > 0)) { int busyLoops = entry.nextBusyLoop(); if (busyLoops > getMaxBusyLoops()) { if (logger.isDebugEnabled()) { logger.debug("After " + busyLoops + " read key has been removed: " + entry); socketClosedRemotely(sk, readChannel, incomingAddress);
STATE_CONNECTED, null); fireConnectionStateChanged(e); if (e.isCancelled()) { logger.warn("Incoming connection cancelled"); if (!readMessage(sk, readChannel, incomingAddress)) { SocketEntry entry = (SocketEntry) sk.attachment(); if ((entry != null) && (getMaxBusyLoops() > 0)) { int busyLoops = entry.nextBusyLoop(); if (busyLoops > getMaxBusyLoops()) { if (logger.isDebugEnabled()) { logger.debug("After " + busyLoops + " read key has been removed: " + entry); socketClosedRemotely(sk, readChannel, incomingAddress);
logger.debug("Message length is "+messageLength); if ((messageLength.getMessageLength() > getMaxInboundMessageSize()) || (messageLength.getMessageLength() <= 0)) { logger.error("Received message length "+messageLength+ " is greater than inboundBufferSize "+ getMaxInboundMessageSize()); if (entry != null) { Socket s = entry.getSocket(); STATE_DISCONNECTED_REMOTELY, null); fireConnectionStateChanged(e);
logger.debug("Message length is "+messageLength); if ((messageLength.getMessageLength() > getMaxInboundMessageSize()) || (messageLength.getMessageLength() <= 0)) { logger.error("Received message length "+messageLength+ " is greater than inboundBufferSize "+ getMaxInboundMessageSize()); if (entry != null) { Socket s = entry.getSocket(); logger.debug("Adding buffer content to read buffer of entry "+entry+", buffer "+byteBuffer); addBufferToReadBuffer(entry, byteBuffer);
/** * Listen for incoming and outgoing requests. If the {@code serverEnabled} * member is {@code false} the server for incoming requests is not * started. This starts the internal server thread that processes messages. * @throws SocketException * when the transport is already listening for incoming/outgoing messages. * @throws IOException * if the listen port could not be bound to the server thread. */ public synchronized void listen() throws java.io.IOException { if (server != null) { throw new SocketException("Port already listening"); } serverThread = new ServerThread(); if (logger.isInfoEnabled()) { logger.info("TCP address "+getListenAddress()+" bound successfully"); } server = SNMP4JSettings.getThreadFactory().createWorkerThread( "DefaultTCPTransportMapping_"+getAddress(), serverThread, true); if (connectionTimeout > 0) { // run as daemon socketCleaner = SNMP4JSettings.getTimerFactory().createTimer(); } server.run(); }
/** * Sends a SNMP message to the supplied address. * @param address * an <code>TcpAddress</code>. A <code>ClassCastException</code> is thrown * if <code>address</code> is not a <code>TcpAddress</code> instance. * @param message byte[] * the message to sent. * @throws IOException */ public void sendMessage(Address address, byte[] message) throws java.io.IOException { if (server == null) { listen(); } serverThread.sendMessage(address, message); }
/** * Listen for incoming and outgoing requests. If the <code>serverEnabled</code> * member is <code>false</code> the server for incoming requests is not * started. This starts the internal server thread that processes messages. * @throws SocketException * when the transport is already listening for incoming/outgoing messages. * @throws IOException */ public synchronized void listen() throws java.io.IOException { if (server != null) { throw new SocketException("Port already listening"); } serverThread = new ServerThread(); server = SNMP4JSettings.getThreadFactory().createWorkerThread( "DefaultTCPTransportMapping_"+getAddress(), serverThread, true); if (connectionTimeout > 0) { // run as daemon socketCleaner = SNMP4JSettings.getTimerFactory().createTimer(); } server.run(); }
public ServerThread() throws IOException { super(DefaultTcpTransportMapping.this); buf = new byte[getMaxInboundMessageSize()]; setSocketOptions(ssc.socket()); ssc.socket().bind(isa);
logger.debug("Message length is " + messageLength); if ((messageLength.getMessageLength() > getMaxInboundMessageSize()) || (messageLength.getMessageLength() <= 0)) { logger.error("Received message length " + messageLength + " is greater than inboundBufferSize " + getMaxInboundMessageSize()); if (entry != null) { Socket s = entry.getSocket(); logger.debug("Adding buffer content to read buffer of entry " + entry + ", buffer " + byteBuffer); addBufferToReadBuffer(entry, byteBuffer);
/** * Sends a SNMP message to the supplied address. * @param address * an {@code TcpAddress}. A {@code ClassCastException} is thrown * if {@code address} is not a {@code TcpAddress} instance. * @param message byte[] * the message to sent. * @param tmStateReference * the (optional) transport model state reference as defined by * RFC 5590 section 6.1. * @throws IOException * if an IO exception occurs while trying to send the message. */ public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference) throws java.io.IOException { if (server == null) { listen(); } serverThread.sendMessage(address, message, tmStateReference); }
@Override protected void doStart() throws Exception { super.doStart(); // load connection data only if the endpoint is enabled if (LOG.isInfoEnabled()) { LOG.info("Starting trap consumer on {}", this.endpoint.getAddress()); } this.listenGenericAddress = GenericAddress.parse(this.endpoint.getAddress()); // either tcp or udp if ("tcp".equals(endpoint.getProtocol())) { this.transport = new DefaultTcpTransportMapping((TcpAddress)this.listenGenericAddress); } else if ("udp".equals(endpoint.getProtocol())) { this.transport = new DefaultUdpTransportMapping((UdpAddress)this.listenGenericAddress); } else { throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol()); } this.snmp = new Snmp(transport); this.snmp.addCommandResponder(this); // listen to the transport if (LOG.isDebugEnabled()) { LOG.debug("Starting trap consumer on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol()); } this.transport.listen(); if (LOG.isInfoEnabled()) { LOG.info("Started trap consumer on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol()); } }
public ServerThread() throws IOException { buf = new byte[getMaxInboundMessageSize()]; setSocketOptions(ssc.socket()); ssc.socket().bind(isa);
private void dispatchMessage(TcpAddress incomingAddress, ByteBuffer byteBuffer, long bytesRead, Object sessionID) { byteBuffer.flip(); if (logger.isDebugEnabled()) { logger.debug("Received message from " + incomingAddress + " with length " + bytesRead + ": " + new OctetString(byteBuffer.array(), 0, (int) bytesRead).toHexString()); } ByteBuffer bis; if (isAsyncMsgProcessingSupported()) { byte[] bytes = new byte[(int) bytesRead]; System.arraycopy(byteBuffer.array(), 0, bytes, 0, (int) bytesRead); bis = ByteBuffer.wrap(bytes); } else { bis = ByteBuffer.wrap(byteBuffer.array(), 0, (int) bytesRead); } TransportStateReference stateReference = new TransportStateReference(DefaultTcpTransportMapping.this, incomingAddress, null, SecurityLevel.undefined, SecurityLevel.undefined, false, sessionID); fireProcessMessage(incomingAddress, bis, stateReference); }
protected void socketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException { logger.debug("Socket closed remotely"); sk.cancel(); readChannel.close(); TransportStateEvent e = new TransportStateEvent(DefaultTcpTransportMapping.this, incomingAddress, TransportStateEvent. STATE_DISCONNECTED_REMOTELY, null); fireConnectionStateChanged(e); sockets.remove(incomingAddress); }
/** * Sends a SNMP message to the supplied address. * * @param address * an {@code TcpAddress}. A {@code ClassCastException} is thrown * if {@code address} is not a {@code TcpAddress} instance. * @param message * byte[] * the message to sent. * @param tmStateReference * the (optional) transport model state reference as defined by * RFC 5590 section 6.1. * * @throws IOException * if an IO exception occurs while trying to send the message. */ public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws java.io.IOException { if (server == null) { listen(); } serverThread.sendMessage(address, message, tmStateReference); }