/** Closes all open sockets and stops the internal server thread that processes messages. */ public void close() { ServerThread st = server; if (st != null) { st.close(); try { st.join(); } catch (InterruptedException ex) { logger.warn(ex); } server = null; for (Iterator it = sockets.values().iterator(); it.hasNext(); ) { SocketEntry entry = (SocketEntry) it.next(); try { synchronized (entry) { entry.getSocket().close(); } logger.debug("Socket to " + entry.getPeerAddress() + " closed"); } catch (IOException iox) { // ingore logger.debug(iox); } } if (socketCleaner != null) { socketCleaner.cancel(); } socketCleaner = null; } }
/** * Add the default SecurityProtocols. * * <p>The names of the SecurityProtocols to add are read from a properties file. * * @throws InternalError if the properties file cannot be opened/read. */ public synchronized void addDefaultProtocols() { if (SNMP4JSettings.isExtensibilityEnabled()) { String secProtocols = System.getProperty(SECURITY_PROTOCOLS_PROPERTIES, SECURITY_PROTOCOLS_PROPERTIES_DEFAULT); InputStream is = SecurityProtocols.class.getResourceAsStream(secProtocols); if (is == null) { throw new InternalError("Could not read '" + secProtocols + "' from classpath!"); } Properties props = new Properties(); try { props.load(is); for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) { String className = en.nextElement().toString(); try { Class c = Class.forName(className); Object proto = c.newInstance(); if (proto instanceof AuthenticationProtocol) { addAuthenticationProtocol((AuthenticationProtocol) proto); } else if (proto instanceof PrivacyProtocol) { addPrivacyProtocol((PrivacyProtocol) proto); } else { logger.error( "Failed to register security protocol because it does " + "not implement required interfaces: " + className); } } catch (Exception cnfe) { logger.error(cnfe); throw new InternalError(cnfe.toString()); } } } catch (IOException iox) { String txt = "Could not read '" + secProtocols + "': " + iox.getMessage(); logger.error(txt); throw new InternalError(txt); } finally { try { is.close(); } catch (IOException ex) { // ignore logger.warn(ex); } } } else { addAuthenticationProtocol(new AuthMD5()); addAuthenticationProtocol(new AuthSHA()); addPrivacyProtocol(new PrivDES()); addPrivacyProtocol(new PrivAES128()); addPrivacyProtocol(new PrivAES192()); addPrivacyProtocol(new PrivAES256()); } }
/** * Closes a connection to the supplied remote address, if it is open. This method is particularly * useful when not using a timeout for remote connections. * * @param remoteAddress the address of the peer socket. * @return <code>true</code> if the connection has been closed and <code>false</code> if there was * nothing to close. * @since 1.7.1 */ public synchronized boolean close(Address remoteAddress) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Closing socket for peer address " + remoteAddress); } SocketEntry entry = (SocketEntry) sockets.remove(remoteAddress); if (entry != null) { synchronized (entry) { entry.getSocket().close(); } logger.info("Socket to " + entry.getPeerAddress() + " closed"); return true; } return false; }
/** * Closes the socket and stops the listener thread. * * @throws IOException */ public void close() throws IOException { boolean interrupted = false; WorkerTask l = listener; if (l != null) { l.terminate(); l.interrupt(); if (socketTimeout > 0) { try { l.join(); } catch (InterruptedException ex) { interrupted = true; logger.warn(ex); } } listener = null; } DatagramSocket closingSocket = socket; if ((closingSocket != null) && (!closingSocket.isClosed())) { closingSocket.close(); } socket = null; if (interrupted) { Thread.currentThread().interrupt(); } }
public void sendMessage( UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference) throws java.io.IOException { InetSocketAddress targetSocketAddress = new InetSocketAddress(targetAddress.getInetAddress(), targetAddress.getPort()); if (logger.isDebugEnabled()) { logger.debug( "Sending message to " + targetAddress + " with length " + message.length + ": " + new OctetString(message).toHexString()); } DatagramSocket s = ensureSocket(); s.send(new DatagramPacket(message, message.length, targetSocketAddress)); }
private void _log(String logMsg) { if (_isCurrentlyOnMainThread()) { _logs.add(0, logMsg + " (main thread) "); _adapter.clear(); _adapter.addAll(_logs); } else { _logs.add(0, logMsg + " (NOT main thread) "); // You can only do below stuff on main thread. new Handler(Looper.getMainLooper()) .post( () -> { _adapter.clear(); _adapter.addAll(_logs); }); } }
/** Default constructor, initializes the salt to a random value. */ protected Salt() { byte[] rnd = new byte[8]; try { SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); sr.nextBytes(rnd); } catch (NoSuchAlgorithmException nsae) { logger.warn("Could not use SecureRandom. Using Random instead."); Random r = new Random(); r.nextBytes(rnd); } salt = rnd[0]; for (int i = 0; i < 7; i++) { salt = (salt * 256) + ((int) rnd[i]) + 128; } if (logger.isDebugEnabled() == true) { logger.debug("Initialized Salt to " + Long.toHexString(salt) + "."); } }
/** 返回以调用者的类命名的Log,是获取Log对象最简单的方法! */ public static Log get() { return adapter.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); }
/** * Get a Log by name * * @param className the name of Log * @return Log * @throws NullPointerException when className is null, maybe it will case NPE */ public static Log getLog(String className) { return adapter.getLogger(className); }