ScreensSessionImpl.java
5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* 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);
}
}
}