Client.java 3.77 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.socketio.impl;

import com.adobe.granite.socketio.SocketIONamespace;
import com.adobe.granite.socketio.SocketIOSocket;
import com.adobe.granite.socketio.impl.Packet;
import com.adobe.granite.socketio.impl.PacketType;
import com.adobe.granite.socketio.impl.SocketIONamespaceImpl;
import com.adobe.granite.socketio.impl.SocketIOServiceImpl;
import com.adobe.granite.socketio.impl.SocketIOSocketImpl;
import com.adobe.granite.socketio.impl.engine.EIOPacket;
import com.adobe.granite.socketio.impl.engine.EIOSocket;
import com.adobe.granite.socketio.impl.engine.EIOSocketListener;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Client
implements EIOSocketListener {
    private static final Logger log = LoggerFactory.getLogger(Client.class);
    private final String id;
    private final SocketIOServiceImpl svc;
    private EIOSocket transport;
    private final ConcurrentHashMap<String, SocketIOSocketImpl> sockets = new ConcurrentHashMap();

    public Client(String id, SocketIOServiceImpl svc, EIOSocket transport) {
        this.id = id;
        this.svc = svc;
        this.transport = transport;
        transport.setListener(this);
    }

    public String getId() {
        return this.id;
    }

    public SocketIOServiceImpl getSocketIOService() {
        return this.svc;
    }

    public void connect(String nspName) {
        log.debug("[{}] connecting to namespace {}", (Object)this.id, (Object)nspName);
        if (this.sockets.containsKey(nspName)) {
            log.error("[{}] already connected to namespace {}", (Object)this.id, (Object)nspName);
            return;
        }
        SocketIONamespaceImpl nsp = this.svc.getNamespace(nspName, true);
        SocketIOSocketImpl socket = new SocketIOSocketImpl(this, nsp);
        this.sockets.put(nspName, socket);
        socket.onConnect();
    }

    public void detach(SocketIOSocketImpl socket) {
        this.sockets.remove(socket.getNamespace().getName());
    }

    public void disconnect() {
        for (SocketIOSocketImpl s : this.sockets.values().toArray(new SocketIOSocketImpl[this.sockets.size()])) {
            s.disconnect(false);
        }
        this.close();
    }

    public void close() {
        if (this.transport.isOpen()) {
            log.debug("[{}] forcing transport close");
            this.transport.close();
            this.onClose("forced server close");
        }
        this.svc.detach(this);
    }

    public void send(Packet p) throws IOException {
        this.transport.send(p.encode());
    }

    @Override
    public void onOpen() {
    }

    @Override
    public void onPacket(EIOPacket packet) {
    }

    @Override
    public void onHeartbeat() {
    }

    @Override
    public void onMessage(String data) {
        Packet p = Packet.decode(data);
        if (p.type == PacketType.PARSER_ERROR) {
            log.error("[{}] parser error.");
            this.close();
        } else if (p.type == PacketType.CONNECT) {
            this.connect(p.nsp);
        } else {
            SocketIOSocketImpl socket = this.sockets.get(p.nsp);
            if (socket == null) {
                log.warn("[{}] No socket on namespace {}", (Object)this.id, (Object)p.nsp);
            } else {
                socket.onPacket(p);
            }
        }
    }

    @Override
    public void onClose(String reason) {
        log.debug("[{}] client close: {}", (Object)this.id, (Object)reason);
        this.disconnect();
    }

    public Principal getUserPrincipal() {
        return this.transport.getUserPrincipal();
    }
}