-
Notifications
You must be signed in to change notification settings - Fork 5
/
ezy-managers.js
207 lines (181 loc) · 5.46 KB
/
ezy-managers.js
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
197
198
199
200
201
202
203
204
205
206
207
import Const from './ezy-constants';
import Util from './ezy-util';
import DataHandler from './ezy-data-handlers';
import EventHandler from './ezy-event-handlers';
export class EzyAppManager {
constructor(zoneName) {
this.zoneName = zoneName;
this.appList = [];
this.appsById = {};
this.appsByName = {};
}
getApp() {
var app = null;
if (this.appList.length > 0) app = this.appList[0];
else Util.EzyLogger.console('has no app in zone: ' + this.zoneName);
return app;
}
addApp(app) {
this.appList.push(app);
this.appsById[app.id] = app;
this.appsByName[app.name] = app;
}
removeApp(appId) {
var app = this.appsById[appId];
if (app) {
delete this.appsById[appId];
delete this.appsByName[app.name];
this.appList = this.appList.filter((item) => item.id !== appId);
}
return app;
}
getAppById(id) {
var app = this.appsById[id];
return app;
}
getAppByName(name) {
var app = this.appsByName[name];
return app;
}
}
//======================================
export class EzyPluginManager {
constructor(zoneName) {
this.zoneName = zoneName;
this.pluginList = [];
this.pluginsById = {};
this.pluginsByName = {};
}
getPlugin() {
var plugin = null;
if (this.pluginList.length > 0) plugin = this.pluginList[0];
else Util.EzyLogger.console('has no plugin in zone: ' + this.zoneName);
return plugin;
}
addPlugin(plugin) {
this.pluginList.push(plugin);
this.pluginsById[plugin.id] = plugin;
this.pluginsByName[plugin.name] = plugin;
}
getPluginById(id) {
var plugin = this.pluginsById[id];
return plugin;
}
getPluginByName(name) {
var plugin = this.pluginsByName[name];
return plugin;
}
}
//======================================
export class EzyPingManager {
constructor() {
this.pingPeriod = 5000;
this.lostPingCount = 0;
this.maxLostPingCount = 5;
}
increaseLostPingCount() {
return ++this.lostPingCount;
}
}
//======================================
export class EzyHandlerManager {
constructor(client) {
this.client = client;
this.dataHandlers = this.newDataHandlers();
this.eventHandlers = this.newEventHandlers();
this.appDataHandlersByName = {};
this.pluginDataHandlersByName = {};
}
newEventHandlers() {
var handlers = new EventHandler.EzyEventHandlers(this.client);
handlers.addHandler(
Const.EzyEventType.CONNECTION_SUCCESS,
new EventHandler.EzyConnectionSuccessHandler()
);
handlers.addHandler(
Const.EzyEventType.CONNECTION_FAILURE,
new EventHandler.EzyConnectionFailureHandler()
);
handlers.addHandler(
Const.EzyEventType.DISCONNECTION,
new EventHandler.EzyDisconnectionHandler()
);
return handlers;
}
newDataHandlers() {
var handlers = new DataHandler.EzyDataHandlers(this.client);
handlers.addHandler(
Const.EzyCommand.PONG,
new DataHandler.EzyPongHandler()
);
handlers.addHandler(
Const.EzyCommand.HANDSHAKE,
new DataHandler.EzyHandshakeHandler()
);
handlers.addHandler(
Const.EzyCommand.LOGIN,
new DataHandler.EzyLoginSuccessHandler()
);
handlers.addHandler(
Const.EzyCommand.LOGIN_ERROR,
new DataHandler.EzyLoginErrorHandler()
);
handlers.addHandler(
Const.EzyCommand.APP_ACCESS,
new DataHandler.EzyAppAccessHandler()
);
handlers.addHandler(
Const.EzyCommand.APP_REQUEST,
new DataHandler.EzyAppResponseHandler()
);
handlers.addHandler(
Const.EzyCommand.APP_EXIT,
new DataHandler.EzyAppExitHandler()
);
handlers.addHandler(
Const.EzyCommand.PLUGIN_INFO,
new DataHandler.EzyPluginInfoHandler()
);
handlers.addHandler(
Const.EzyCommand.PLUGIN_REQUEST,
new DataHandler.EzyPluginResponseHandler()
);
return handlers;
}
getDataHandler(cmd) {
var handler = this.dataHandlers.getHandler(cmd);
return handler;
}
getEventHandler(eventType) {
var handler = this.eventHandlers.getHandler(eventType);
return handler;
}
getAppDataHandlers(appName) {
var answer = this.appDataHandlersByName[appName];
if (!answer) {
answer = new DataHandler.EzyAppDataHandlers();
this.appDataHandlersByName[appName] = answer;
}
return answer;
}
getPluginDataHandlers(pluginName) {
var answer = this.pluginDataHandlersByName[pluginName];
if (!answer) {
answer = new DataHandler.EzyPluginDataHandlers();
this.pluginDataHandlersByName[pluginName] = answer;
}
return answer;
}
addDataHandler(cmd, dataHandler) {
this.dataHandlers.addHandler(cmd, dataHandler);
}
addEventHandler(eventType, eventHandler) {
this.eventHandlers.addHandler(eventType, eventHandler);
}
}
export default {
EzyAppManager,
EzyPluginManager,
EzyPingManager,
EzyHandlerManager,
};