Search in sources :

Example 1 with MultiTypeArgs

use of com.corundumstudio.socketio.MultiTypeArgs in project netty-socketio by mrniko.

the class AckManager method onAck.

@SuppressWarnings("unchecked")
public void onAck(SocketIOClient client, Packet packet) {
    AckSchedulerKey key = new AckSchedulerKey(Type.ACK_TIMEOUT, client.getSessionId(), packet.getAckId());
    scheduler.cancel(key);
    AckCallback callback = removeCallback(client.getSessionId(), packet.getAckId());
    if (callback == null) {
        return;
    }
    if (callback instanceof MultiTypeAckCallback) {
        callback.onSuccess(new MultiTypeArgs(packet.<List<Object>>getData()));
    } else {
        Object param = null;
        List<Object> args = packet.getData();
        if (!args.isEmpty()) {
            param = args.get(0);
        }
        if (args.size() > 1) {
            log.error("Wrong ack args amount. Should be only one argument, but current amount is: {}. Ack id: {}, sessionId: {}", args.size(), packet.getAckId(), client.getSessionId());
        }
        callback.onSuccess(param);
    }
}
Also used : MultiTypeAckCallback(com.corundumstudio.socketio.MultiTypeAckCallback) AckCallback(com.corundumstudio.socketio.AckCallback) MultiTypeAckCallback(com.corundumstudio.socketio.MultiTypeAckCallback) List(java.util.List) MultiTypeArgs(com.corundumstudio.socketio.MultiTypeArgs)

Example 2 with MultiTypeArgs

use of com.corundumstudio.socketio.MultiTypeArgs in project netty-socketio by mrniko.

the class OnEventScanner method addListener.

@Override
@SuppressWarnings("unchecked")
public void addListener(Namespace namespace, final Object object, final Method method, Annotation annot) {
    OnEvent annotation = (OnEvent) annot;
    if (annotation.value() == null || annotation.value().trim().length() == 0) {
        throw new IllegalArgumentException("OnEvent \"value\" parameter is required");
    }
    final int socketIOClientIndex = paramIndex(method, SocketIOClient.class);
    final int ackRequestIndex = paramIndex(method, AckRequest.class);
    final List<Integer> dataIndexes = dataIndexes(method);
    if (dataIndexes.size() > 1) {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        for (int index : dataIndexes) {
            Class<?> param = method.getParameterTypes()[index];
            classes.add(param);
        }
        namespace.addMultiTypeEventListener(annotation.value(), new MultiTypeEventListener() {

            @Override
            public void onData(SocketIOClient client, MultiTypeArgs data, AckRequest ackSender) {
                try {
                    Object[] args = new Object[method.getParameterTypes().length];
                    if (socketIOClientIndex != -1) {
                        args[socketIOClientIndex] = client;
                    }
                    if (ackRequestIndex != -1) {
                        args[ackRequestIndex] = ackSender;
                    }
                    int i = 0;
                    for (int index : dataIndexes) {
                        args[index] = data.get(i);
                        i++;
                    }
                    method.invoke(object, args);
                } catch (InvocationTargetException e) {
                    throw new SocketIOException(e.getCause());
                } catch (Exception e) {
                    throw new SocketIOException(e);
                }
            }
        }, classes.toArray(new Class[classes.size()]));
    } else {
        Class objectType = Void.class;
        if (!dataIndexes.isEmpty()) {
            objectType = method.getParameterTypes()[dataIndexes.iterator().next()];
        }
        namespace.addEventListener(annotation.value(), objectType, new DataListener<Object>() {

            @Override
            public void onData(SocketIOClient client, Object data, AckRequest ackSender) {
                try {
                    Object[] args = new Object[method.getParameterTypes().length];
                    if (socketIOClientIndex != -1) {
                        args[socketIOClientIndex] = client;
                    }
                    if (ackRequestIndex != -1) {
                        args[ackRequestIndex] = ackSender;
                    }
                    if (!dataIndexes.isEmpty()) {
                        int dataIndex = dataIndexes.iterator().next();
                        args[dataIndex] = data;
                    }
                    method.invoke(object, args);
                } catch (InvocationTargetException e) {
                    throw new SocketIOException(e.getCause());
                } catch (Exception e) {
                    throw new SocketIOException(e);
                }
            }
        });
    }
}
Also used : SocketIOException(com.corundumstudio.socketio.handler.SocketIOException) AckRequest(com.corundumstudio.socketio.AckRequest) ArrayList(java.util.ArrayList) MultiTypeEventListener(com.corundumstudio.socketio.listener.MultiTypeEventListener) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketIOException(com.corundumstudio.socketio.handler.SocketIOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SocketIOClient(com.corundumstudio.socketio.SocketIOClient) MultiTypeArgs(com.corundumstudio.socketio.MultiTypeArgs)

Aggregations

MultiTypeArgs (com.corundumstudio.socketio.MultiTypeArgs)2 AckCallback (com.corundumstudio.socketio.AckCallback)1 AckRequest (com.corundumstudio.socketio.AckRequest)1 MultiTypeAckCallback (com.corundumstudio.socketio.MultiTypeAckCallback)1 SocketIOClient (com.corundumstudio.socketio.SocketIOClient)1 SocketIOException (com.corundumstudio.socketio.handler.SocketIOException)1 MultiTypeEventListener (com.corundumstudio.socketio.listener.MultiTypeEventListener)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1