Search in sources :

Example 1 with GenericMessageHandler

use of com.biglybt.pif.messaging.generic.GenericMessageHandler in project BiglyBT by BiglySoftware.

the class GenericMessageConnectionIndirect method receive.

protected static Map receive(MessageManagerImpl message_manager, InetSocketAddress originator, Map message) {
    if (TRACE) {
        System.out.println("receive:" + originator + "/" + message);
    }
    if (!message.containsKey("type")) {
        return (null);
    }
    int type = ((Long) message.get("type")).intValue();
    if (type == MESSAGE_TYPE_CONNECT) {
        String msg_id = new String((byte[]) message.get("msg_id"));
        String msg_desc = new String((byte[]) message.get("msg_desc"));
        GenericMessageEndpointImpl endpoint = new GenericMessageEndpointImpl(originator);
        endpoint.addUDP(originator);
        GenericMessageHandler handler = message_manager.getHandler(msg_id);
        if (handler == null) {
            Debug.out("No message handler registered for '" + msg_id + "'");
            return (null);
        }
        try {
            Long con_id;
            synchronized (remote_connections) {
                if (remote_connections.size() >= MAX_REMOTE_CONNECTIONS) {
                    Debug.out("Maximum remote connections exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
                    return (null);
                }
                int num_from_this_ip = 0;
                Iterator it = remote_connections.values().iterator();
                while (it.hasNext()) {
                    GenericMessageConnectionIndirect con = (GenericMessageConnectionIndirect) it.next();
                    if (con.getEndpoint().getNotionalAddress().getAddress().equals(originator.getAddress())) {
                        num_from_this_ip++;
                    }
                }
                if (num_from_this_ip >= MAX_REMOTE_CONNECTIONS_PER_IP) {
                    Debug.out("Maximum remote connections per-ip exceeded - request from " + originator + " denied [" + getRemoteConnectionStatus() + "]");
                    return (null);
                }
                con_id = new Long(connection_id_next++);
            }
            GenericMessageConnectionIndirect indirect_connection = new GenericMessageConnectionIndirect(message_manager, msg_id, msg_desc, endpoint, con_id.longValue());
            GenericMessageConnectionImpl new_connection = new GenericMessageConnectionImpl(message_manager, indirect_connection);
            if (handler.accept(new_connection)) {
                new_connection.accepted();
                synchronized (remote_connections) {
                    remote_connections.put(con_id, indirect_connection);
                }
                List replies = indirect_connection.receive((List) message.get("data"));
                Map reply = new HashMap();
                reply.put("type", new Long(MESSAGE_TYPE_CONNECT));
                reply.put("con_id", con_id);
                reply.put("data", replies);
                return (reply);
            } else {
                return (null);
            }
        } catch (MessageException e) {
            Debug.out("Error accepting message", e);
            return (null);
        }
    } else if (type == MESSAGE_TYPE_DATA) {
        Long con_id = (Long) message.get("con_id");
        GenericMessageConnectionIndirect indirect_connection;
        synchronized (remote_connections) {
            indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
        }
        if (indirect_connection == null) {
            return (null);
        }
        Map reply = new HashMap();
        if (indirect_connection.isClosed()) {
            reply.put("type", new Long(MESSAGE_TYPE_DISCONNECT));
        } else {
            List replies = indirect_connection.receive((List) message.get("data"));
            reply.put("type", new Long(MESSAGE_TYPE_DATA));
            reply.put("data", replies);
            if (indirect_connection.receiveIncomplete()) {
                reply.put("more_data", new Long(1));
            }
        }
        return (reply);
    } else {
        // error or disconnect
        Long con_id = (Long) message.get("con_id");
        GenericMessageConnectionIndirect indirect_connection;
        synchronized (remote_connections) {
            indirect_connection = (GenericMessageConnectionIndirect) remote_connections.get(con_id);
        }
        if (indirect_connection != null) {
            try {
                indirect_connection.close(new Throwable("Remote closed connection"));
            } catch (Throwable e) {
                Debug.printStackTrace(e);
            }
        }
        return (null);
    }
}
Also used : GenericMessageEndpoint(com.biglybt.pif.messaging.generic.GenericMessageEndpoint) MessageException(com.biglybt.pif.messaging.MessageException) GenericMessageHandler(com.biglybt.pif.messaging.generic.GenericMessageHandler)

Aggregations

MessageException (com.biglybt.pif.messaging.MessageException)1 GenericMessageEndpoint (com.biglybt.pif.messaging.generic.GenericMessageEndpoint)1 GenericMessageHandler (com.biglybt.pif.messaging.generic.GenericMessageHandler)1