public void connectionStarting(ConnectionEvent e) throws ConnectionVetoException { ipList.put(e.getConnectionModel().getHostAddress(), null); // check on the max connections GnutellaConnection conn = ((GnutellaConnectionEvent) e).getConnection(); GnutellaConnectionModel model = (GnutellaConnectionModel) e.getConnectionModel(); boolean maxReached = false; switch (model.getConnectionType()) { case GnutellaConnectionModel.INCOMING: synchronized (this) { if (currentIncomingConnections >= maxIncomingConnections) maxReached = true; // if it hasn't been reached, then increment and continue currentIncomingConnections++; } break; case GnutellaConnectionModel.OUTGOING: synchronized (this) { if (currentOutgoingConnections >= maxOutgoingConnections) maxReached = true; // if it hasn't been reached, then increment and continue currentOutgoingConnections++; } break; } // record the connection into the list synchronized (activeConnections) { activeConnections.add(conn); } conn.addMessageListener(listMsgListener); fireConnectionStartingWithoutVeto(e); if (maxReached) throw new ConnectionVetoException("Max Connection Reached!"); }
/** shutdown the connections and disallow accepting of new connections */ public void shutdown() { shutdown = true; Iterator iter = getActiveConnections().iterator(); GnutellaConnection conn; while (iter.hasNext()) { conn = (GnutellaConnection) iter.next(); conn.disconnect(); } }
/** @return the established connection specified by the connection model, null if non-existent */ public GnutellaConnection getEstablishedConnection(ConnectionModel model) { GnutellaConnection connection = null; synchronized (establishedConnections) { int size = establishedConnections.size(); for (int i = 0; i < size; i++) { connection = (GnutellaConnection) establishedConnections.get(i); if (model.equals(connection.getConnectionModel())) break; } } return connection; }
/** * Sends a message to all the established connections. This is usually used to send out our own * messages such as search requests. Note that this method does NOT add the message to the list of * seen messages. This may cause certain messages not to be fired off to listeners. * * <p>If there is an error sending the message through a particular connection, the error will be * ignored and the message will continue to be sent to all the other connections. */ public void sendMessageToAll(GnutellaMessage msg) { synchronized (establishedConnections) { int size = establishedConnections.size(); GnutellaConnection connection; for (int i = 0; i < size; i++) { connection = (GnutellaConnection) establishedConnections.get(i); try { connection.send(msg); } catch (SendMessageFailedException ex) { // ignore message sending failures } } } }
/** * Adds a connection to the list. The connection will setup some listeners so that when certain * events occur, the connection is properly recorded and sets in the connection list for future * references. The connection will not be added if the connection is already in the list. * * @return true if connection added successfully, false otherwise */ public boolean addConnection(GnutellaConnection conn, GnutellaConnectionModel cmodel) { if (shutdown) return false; // check if connection reference already exists // if so, ignore. Otherwise, add it. // The connection equality is not tested on connection model, // but on the connection object itself if (activeConnections.contains(conn)) return false; // check whether a connection to the IP is already made // if so, there is no need to duplicate the connection if (ipList.containsKey(cmodel.getHostAddress())) return false; conn.addConnectionListener(listConnListener); return true; }
public void connectionClosed(ConnectionEvent e) { GnutellaConnection conn = ((GnutellaConnectionEvent) e).getConnection(); // remove all listeners as well // as remove from active and established connection lists conn.removeConnectionListener(this); synchronized (activeConnections) { activeConnections.remove(conn); } synchronized (establishedConnections) { establishedConnections.remove(conn); } fireConnectionClosed(e); GnutellaConnectionModel model = (GnutellaConnectionModel) e.getConnectionModel(); switch (model.getConnectionType()) { case GnutellaConnectionModel.INCOMING: currentIncomingConnections--; break; case GnutellaConnectionModel.OUTGOING: currentOutgoingConnections--; break; } conn.removeMessageListener(listMsgListener); ipList.remove(e.getConnectionModel().getHostAddress()); }