-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEzyManagers.swift
136 lines (114 loc) · 4.55 KB
/
EzyManagers.swift
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
//
// EzyManagers.swift
// hello-swift
//
// Created by Dung Ta Van on 10/30/18.
// Copyright © 2018 Young Monkeys. All rights reserved.
//
import Foundation
public class EzyAppManager {
public let zoneName : String
private let appList : NSMutableArray
private let appsById : NSMutableDictionary
private let appsByName : NSMutableDictionary
public init(zoneName: String) {
self.zoneName = zoneName
self.appList = NSMutableArray()
self.appsById = NSMutableDictionary()
self.appsByName = NSMutableDictionary()
}
public func getApp() -> EzyApp {
var app : Any? = nil
if(self.appList.count > 0) {
app = self.appList[0]
}
else {
EzyLogger.warn(msg: "has no app in zone: \(self.zoneName)");
}
return app as! EzyApp
}
public func addApp(app: EzyApp) -> Void {
self.appList.add(app)
self.appsById[app.id] = app
self.appsByName[app.name] = app
}
public func removeApp(appId: Int) -> EzyApp? {
let app = self.appsById[appId] as? EzyApp;
if(app != nil) {
self.appList.remove(app!);
self.appsById.removeObject(forKey: appId);
self.appsByName.removeObject(forKey: app!.name)
}
return app;
}
public func getAppById(id: Int) -> EzyApp {
let app = self.appsById[id]
return app as! EzyApp
}
public func getAppByName(name: String) -> EzyApp {
let app = self.appsByName[name]
return app as! EzyApp
}
}
//===================================================
public class EzyHandlerManager {
private let client : EzyClient
public var dataHandlers : EzyDataHandlers?
public var eventHandlers : EzyEventHandlers?
private let appDataHandlerss : NSMutableDictionary
public static func create(client: EzyClient) -> EzyHandlerManager {
let pRet = EzyHandlerManager(client: client)
pRet.postInit()
return pRet
}
public init(client: EzyClient) {
self.client = client;
self.appDataHandlerss = NSMutableDictionary()
self.dataHandlers = nil
self.eventHandlers = nil
}
private func postInit() -> Void {
self.dataHandlers = self.newDataHandlers()
self.eventHandlers = self.newEventHandlers()
}
private func newEventHandlers() -> EzyEventHandlers {
let handlers = EzyEventHandlers(client: self.client)
handlers.addHandler(eventType: EzyEventType.CONNECTION_SUCCESS, handler: EzyConnectionSuccessHandler())
handlers.addHandler(eventType: EzyEventType.CONNECTION_FAILURE, handler: EzyConnectionFailureHandler())
handlers.addHandler(eventType: EzyEventType.DISCONNECTION, handler: EzyDisconnectionHandler())
return handlers
}
private func newDataHandlers() -> EzyDataHandlers {
let handlers = EzyDataHandlers(client: self.client);
handlers.addHandler(cmd: EzyCommand.PONG, handler: EzyPongHandler())
handlers.addHandler(cmd: EzyCommand.HANDSHAKE, handler: EzyHandshakeHandler())
handlers.addHandler(cmd: EzyCommand.LOGIN, handler: EzyLoginSuccessHandler())
handlers.addHandler(cmd: EzyCommand.LOGIN_ERROR, handler: EzyLoginErrorHandler())
handlers.addHandler(cmd: EzyCommand.APP_ACCESS, handler: EzyAppAccessHandler())
handlers.addHandler(cmd: EzyCommand.APP_EXIT, handler: EzyAppExitHandler())
handlers.addHandler(cmd: EzyCommand.APP_REQUEST, handler: EzyAppResponseHandler())
return handlers;
}
public func getDataHandler(cmd: String) -> EzyDataHandler? {
let handler = self.dataHandlers!.getHandler(cmd: cmd)
return handler
}
public func getEventHandler(eventType: String) -> EzyEventHandler? {
let handler = self.eventHandlers!.getHandler(eventType: eventType)
return handler
}
public func getAppDataHandlers(appName: String) -> EzyAppDataHandlers {
var answer = self.appDataHandlerss[appName]
if(answer == nil) {
answer = EzyAppDataHandlers()
self.appDataHandlerss[appName] = answer
}
return answer as! EzyAppDataHandlers
}
public func addDataHandler(cmd: String, handler: EzyDataHandler) -> Void {
self.dataHandlers!.addHandler(cmd: cmd, handler: handler)
}
public func addEventHandler(eventType: String, handler: EzyEventHandler) -> Void {
self.eventHandlers!.addHandler(eventType: eventType, handler: handler)
}
}