ScreensSessionImpl.java 5.95 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.socketio.SocketIOEmitter
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.screens.sessions.impl;

import com.adobe.cq.screens.sessions.Device;
import com.adobe.cq.screens.sessions.Display;
import com.adobe.cq.screens.sessions.ScreensSession;
import com.adobe.cq.screens.sessions.ScreensSessionListener;
import com.adobe.cq.screens.sessions.impl.ScreensManagerImpl;
import com.adobe.granite.socketio.SocketIOEmitter;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.security.auth.login.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScreensSessionImpl
implements ScreensSession {
    private static final Logger log = LoggerFactory.getLogger(ScreensSessionImpl.class);
    private static final ScreensSessionListener NOP = new ScreensSessionListener(){

        @Override
        public void onConnect(Device d) {
        }

        @Override
        public void onEvent(String name, JSONArray data) {
        }

        @Override
        public void onDisconnect(Device d) {
        }

        @Override
        public void onLogin() {
        }

        @Override
        public void onLogout() {
        }

        @Override
        public void onClose() {
        }
    };
    private final Display display;
    private final ScreensManagerImpl mgr;
    private final ConcurrentMap<String, Device> connectedDevices = new ConcurrentHashMap<String, Device>();
    private final ConcurrentMap<String, Object> userData = new ConcurrentHashMap<String, Object>();
    private ScreensSessionListener listener = NOP;
    private SocketIOEmitter emitter;
    private ScreensManagerImpl.AuthInfo authInfo;

    public ScreensSessionImpl(Display display, ScreensManagerImpl mgr) {
        this.display = display;
        this.mgr = mgr;
    }

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

    @Override
    public void setListener(ScreensSessionListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        if (this.listener != NOP) {
            throw new IllegalArgumentException("Overriding existing listener not allowed.");
        }
        this.listener = listener;
    }

    @Override
    public void removeListener(ScreensSessionListener listener) {
        if (this.listener != listener && this.listener != NOP) {
            throw new IllegalArgumentException("Unable to remove foreign listener " + listener);
        }
        this.listener = NOP;
    }

    @Override
    public Display getDisplay() {
        return this.display;
    }

    @Override
    public ResourceResolver getResourceResolver() {
        return this.authInfo == null ? null : this.authInfo.getResolver();
    }

    @Override
    public ConcurrentMap<String, Object> getUserData() {
        return this.userData;
    }

    @Override
    public void broadcast() throws IOException {
        this.getEmitter().emit("display", new Object[]{this.display.toJSON()});
    }

    public ScreensManagerImpl.AuthInfo getAuthInfo() {
        return this.authInfo;
    }

    @Override
    public SocketIOEmitter getEmitter() {
        return this.emitter;
    }

    public void open(SocketIOEmitter emitter) {
        this.emitter = emitter;
        this.mgr.onOpen(this);
    }

    public boolean connect(Device d) {
        if (this.connectedDevices.putIfAbsent(d.getId(), d) != null) {
            log.error("device '{}' already connected to screen '{}'", (Object)d.getId(), (Object)this.getId());
            return false;
        }
        log.info("[{}] device connected: '{}'", (Object)this.getId(), (Object)d.getId());
        this.listener.onConnect(d);
        return true;
    }

    public void disconnect(Device d) {
        if (this.connectedDevices.remove(d.getId()) == null) {
            log.error("device '{}' was not connected to screen '{}'", (Object)d.getId(), (Object)this.getId());
        } else {
            log.info("[{}] device disconnected: '{}'", (Object)this.getId(), (Object)d.getId());
            this.listener.onDisconnect(d);
        }
    }

    public boolean isConnected(Device device) {
        return this.connectedDevices.containsKey(device.getId());
    }

    public void login(String username, String password) throws LoginException {
        if (this.authInfo != null) {
            throw new IllegalStateException("already logged in");
        }
        this.authInfo = this.mgr.login(username, password);
        log.info("[{}] user logged in: '{}'", (Object)this.getId(), (Object)username);
        this.listener.onLogin();
    }

    public void logout() {
        if (this.authInfo == null) {
            throw new IllegalStateException("already logged out");
        }
        log.info("[{}] user logged out: '{}'", (Object)this.getId(), (Object)this.authInfo.getUserId());
        this.authInfo.logout();
        this.authInfo = null;
        this.userData.clear();
        this.listener.onLogout();
    }

    public void close() {
        for (Device d : this.connectedDevices.values().toArray(new Device[this.connectedDevices.size()])) {
            this.disconnect(d);
        }
        if (this.authInfo != null) {
            this.logout();
        }
        this.emitter = null;
        this.mgr.onClose(this);
        this.listener.onClose();
        this.listener = NOP;
    }

    public void onEvent(String s, JSONArray data) {
        try {
            this.listener.onEvent(s, data);
        }
        catch (Exception e) {
            log.error("listener threw exception", (Throwable)e);
        }
    }

}