as3snapi / as3snapi-core / src / as3snapi / ConnectionFactory.as

  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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package as3snapi {
import as3snapi.api.INetworkConnectHandler;
import as3snapi.api.feautures.IFeatureEventDispatcher;
import as3snapi.api.feautures.social.IFeatureUserId;
import as3snapi.api.feautures.social.profiles.IFeatureAppFriendsProfiles;
import as3snapi.api.feautures.social.profiles.IFeatureFriendsProfiles;
import as3snapi.api.feautures.social.profiles.IFeatureProfiles;
import as3snapi.api.feautures.social.profiles.IFeatureProfilesBase;
import as3snapi.api.feautures.social.profiles.IFeatureSelfProfile;
import as3snapi.api.feautures.social.uids.IFeatureAppFriendUids;
import as3snapi.api.feautures.social.uids.IFeatureFriendUids;
import as3snapi.base.INetworkConfig;
import as3snapi.base.INetworkModule;
import as3snapi.base.INetworkModuleContext;
import as3snapi.base.features.IFeatureConfigGetter;
import as3snapi.base.features.asyncinit.AsyncInitHandler;
import as3snapi.base.features.asyncinit.IFeatureAsyncInit;
import as3snapi.base.features.flashvars.FlashVars;
import as3snapi.base.features.flashvars.IFeatureFlashVarsGetter;
import as3snapi.base.features.javascript.FeatureJavaScript;
import as3snapi.base.features.javascript.IFeatureJavaScript;
import as3snapi.base.features.log.FeatureLogNULL;
import as3snapi.base.features.log.IFeatureLog;
import as3snapi.base.features.requester.FeatureHttpRequester;
import as3snapi.base.features.requester.IFeatureHttpRequester;
import as3snapi.base.plugins.BusFactoryEvent;
import as3snapi.utils.EnumUtils;
import as3snapi.utils.bus.IMutableBus;

import flash.utils.clearTimeout;
import flash.utils.setTimeout;

public class ConnectionFactory implements IConnectionFactory {

    private static const CRITICAL_TIMEOUT:int = 60000;

    private var flashVars:FlashVars;
    private var networkModules:Vector.<INetworkModule> = new <INetworkModule>[];
    private var networkConfigs:Vector.<INetworkConfig> = new <INetworkConfig>[];
    private var busFactory:IBusFactory;

    /**
     *
     * @param flashVars объект с параметрами полученными приложением от соцсети
     * @param networkConfigs список настроек для доступных соцсетей
     * @param networkModules спсиок модулей для используемых соцсетей
     * @param busModules список модулей для навешивания дополнительных возможностей на шину
     */
    public function ConnectionFactory(flashVars:* = null, networkConfigs:Vector.<INetworkConfig> = null, networkModules:Vector.<INetworkModule> = null, busFactory:IBusFactory = null) {
        if (flashVars != null) {
            setFlashVars(flashVars);
        }
        if (networkConfigs != null) {
            this.networkConfigs = networkConfigs;
        }
        if (networkModules != null) {
            this.networkModules = networkModules;
        }
        this.busFactory = busFactory || new DefaultBusFactory()
    }

    public function setFlashVars(flashVars:*):void {
        this.flashVars = FlashVars.fromObject(flashVars);
    }

    public function getFlashVars():FlashVars {
        return flashVars;
    }

    public function createConnection(handler:INetworkConnectHandler):void {
        var bus:IMutableBus = busFactory.createBus();
        // Add default features
        bus.addFeatureIfNotExist(IFeatureEventDispatcher, new FeatureEventDispatcher());
        bus.addFeatureIfNotExist(IFeatureLog, new FeatureLogNULL());
        bus.addFeatureIfNotExist(IFeatureFlashVarsGetter, new FeatureFlashVarsGetter(flashVars, bus));
        bus.addFeatureIfNotExist(IFeatureHttpRequester, new FeatureHttpRequester(bus));
        bus.addFeatureIfNotExist(IFeatureJavaScript, new FeatureJavaScript(bus));
        bus.dispatchEvent(new BusFactoryEvent(BusFactoryEvent.BASIC_FEATURES_ADDED));
        // log
        logFlashVars(bus);
        // Detect
        for each(var networkModule:INetworkModule in networkModules) {
            for each(var config:INetworkConfig in networkConfigs) {
                var context:INetworkModuleContext = new NetworkModuleContext(bus, config);
                if (networkModule.isAvailable(context)) {
                    bus.addFeatureIfNotExist(IFeatureConfigGetter, new FeatureConfig(config));
                    doConnect(networkModule, context, handler);
                    return;
                }
            }
        }
        handler.onFail("Detection fail");
    }

    private function doConnect(networkModule:INetworkModule, context:INetworkModuleContext, handler:INetworkConnectHandler):void {
        var bus:IMutableBus = context.getBus();

        // Configure network
        networkModule.install(context);

        // Post configure bus
        // Добавляем IFeatureProfiles на основе IFeatureProfilesBase
        implementProfilesIfNotExist(bus);

        bus.dispatchEvent(new BusFactoryEvent(BusFactoryEvent.SOCIAL_NETWORK_FEATURES_ADDED));

        // Добавляем IFeature*Profiles на основе IFeatureProfiles
        implementHelperProfilesIfNotExist(bus);
        bus.dispatchEvent(new BusFactoryEvent(BusFactoryEvent.FEATURES_READY));

        logFeatures(bus);
        var connection:NetworkConnection = new NetworkConnection(bus, context.getConfig());

        // таймаут
        var timerId:uint = setTimeout(onTimeout, CRITICAL_TIMEOUT);

        function onTimeout():void {
            handler.onFail("Connection timeout");
            handler = null;
        }

        var log:IFeatureLog = bus.getFeature(IFeatureLog);
        if (bus.hasFeature(IFeatureAsyncInit)) {
            log.log("IFeatureAsyncInit#init()...");
            var feature:IFeatureAsyncInit = bus.getFeature(IFeatureAsyncInit);
            feature.init(new AsyncInitHandler(onReady, onFail));
            bus.disable(IFeatureAsyncInit);
        } else {
            onReady();
        }

        function onReady(result:Object = null):void {
            clearTimeout(timerId);
            log.log("Init: ready.");
            if (handler != null) {
                handler.onSuccess(connection);
            }
        }

        function onFail(result:Object):void {
            log.log("Init: fail.");
            clearTimeout(timerId);
            if (handler != null) {
                handler.onFail(result);
            }
        }
    }

    private function logFlashVars(bus:IMutableBus):void {
        var log:IFeatureLog = bus.getFeature(IFeatureLog);
        log.log("FlashVars: " + flashVars);
    }

    private function logFeatures(bus:IMutableBus):void {
        var log:IFeatureLog = bus.getFeature(IFeatureLog);
        var features:Array = [];
        for each(var featureClass:Class in bus.getFeatures()) {
            features.push(EnumUtils.getShortClassName(featureClass));
        }
        features.sort();
        log.log("Features: ");
        log.log(features.join("\n"));
    }


    /**
     * Подключить дополнительные возможности на основе уже подключенных базовых возможностей
     * @param bus
     */
    private function implementProfilesIfNotExist(bus:IMutableBus):void {
        var profilesBase:IFeatureProfilesBase = bus.getFeature(IFeatureProfilesBase);
        if (profilesBase != null && !bus.hasFeature(IFeatureProfiles)) {
            bus.addFeature(IFeatureProfiles, new FeatureProfilesHelper(profilesBase));
            bus.disable(IFeatureProfilesBase);
        }
    }

    /**
     * Подключить дополнительные возможности на основе уже подключенных базовых возможностей
     * @param bus
     */
    private function implementHelperProfilesIfNotExist(bus:IMutableBus):void {
        var profiles:IFeatureProfiles = bus.getFeature(IFeatureProfiles);
        var friendsIds:IFeatureFriendUids = bus.getFeature(IFeatureFriendUids);
        var appFriendsIds:IFeatureAppFriendUids = bus.getFeature(IFeatureAppFriendUids);
        var userId:IFeatureUserId = bus.getFeature(IFeatureUserId);

        if (profiles != null) {
            if (friendsIds) {
                bus.addFeatureIfNotExist(IFeatureFriendsProfiles, new FeatureFriendsProfilesHelper(profiles, friendsIds));
            }
            if (appFriendsIds) {
                bus.addFeatureIfNotExist(IFeatureAppFriendsProfiles, new FeatureAppFriendsProfilesHelper(profiles, appFriendsIds));
            }
            if (userId) {
                bus.addFeatureIfNotExist(IFeatureSelfProfile, new FeatureSelfProfileHelper(profiles, userId));
            }
        }
    }

}
}

import as3snapi.api.INetworkConnection;
import as3snapi.api.feautures.IFeatureEventDispatcher;
import as3snapi.api.feautures.social.IFeatureUserId;
import as3snapi.api.feautures.social.profiles.*;
import as3snapi.api.feautures.social.uids.IFeatureAppFriendUids;
import as3snapi.api.feautures.social.uids.IFeatureFriendUids;
import as3snapi.api.feautures.social.uids.IdsHandler;
import as3snapi.base.INetworkConfig;
import as3snapi.base.INetworkModuleContext;
import as3snapi.base.features.IFeatureConfigGetter;
import as3snapi.base.features.flashvars.FlashVars;
import as3snapi.base.features.flashvars.IFeatureFlashVarsGetter;
import as3snapi.base.features.javascript.IFeatureJavaScript;
import as3snapi.base.features.javascript.JavaScriptUtils;
import as3snapi.base.features.log.FeatureLogTrace;
import as3snapi.base.features.log.IFeatureLog;
import as3snapi.base.features.requester.IFeatureHttpRequester;
import as3snapi.utils.bus.IBus;
import as3snapi.utils.bus.IMutableBus;

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

internal class FeatureConfig implements IFeatureConfigGetter {
    private var config:INetworkConfig;

    public function FeatureConfig(config:INetworkConfig) {
        this.config = config;
    }

    public function getConfig():INetworkConfig {
        return config;
    }
}

internal class FeatureFlashVarsGetter implements IFeatureFlashVarsGetter {
    private var flashVars:FlashVars;

    public function FeatureFlashVarsGetter(flashVars:FlashVars, bus:IMutableBus) {
        this.flashVars = flashVars;
    }

    public function getFlashVars():FlashVars {
        return flashVars;
    }
}

internal class FeatureEventDispatcher extends EventDispatcher implements IFeatureEventDispatcher {
}

//-- profiles


internal class FeatureProfilesHelper implements IFeatureProfiles {
    private var profilesBase:IFeatureProfilesBase;

    public function FeatureProfilesHelper(profilesBase:IFeatureProfilesBase) {
        this.profilesBase = profilesBase;
    }

    public function getProfiles(uids:Array, handler:IProfilesHandler):void {
        var profilesChunkSize:int = profilesBase.getProfilesChunkSize();
        var resultProfiles:Array = [];
        nextRequest();

        function nextRequest():void {
            if (uids == null || uids.length <= 0) {
                handler.onSuccess(resultProfiles);
            } else {
                var uidsPart:Array = uids.splice(0, profilesChunkSize);
                profilesBase.getProfilesBase(uidsPart, new ProfilesHandler(function (profiles:Array):void {
                    resultProfiles = resultProfiles.concat(profiles);
                    nextRequest()
                }, function (result:Object):void {
                    handler.onFail(result);
                }));

            }
        }
    }
}

internal class FeatureAppFriendsProfilesHelper implements IFeatureAppFriendsProfiles {
    private var profiles:IFeatureProfiles;
    private var ids:IFeatureAppFriendUids;

    public function FeatureAppFriendsProfilesHelper(profiles:IFeatureProfiles, ids:IFeatureAppFriendUids) {
        this.profiles = profiles;
        this.ids = ids;
    }

    public function getAppFriendsProfiles(handler:IProfilesHandler):void {
        ids.getAppFriendUids(
                new IdsHandler(function (uids:Array):void {
                    profiles.getProfiles(uids, handler);
                }, function (result:Object):void {
                    handler.onFail(result);
                }));
    }
}

internal class FeatureFriendsProfilesHelper implements IFeatureFriendsProfiles {
    private var profiles:IFeatureProfiles;
    private var ids:IFeatureFriendUids;

    public function FeatureFriendsProfilesHelper(profiles:IFeatureProfiles, ids:IFeatureFriendUids) {
        this.profiles = profiles;
        this.ids = ids;
    }

    public function getFriendsProfiles(handler:IProfilesHandler):void {
        ids.getFriendUids(
                new IdsHandler(function (uids:Array):void {
                    profiles.getProfiles(uids, handler);
                }, function (result:Object):void {
                    handler.onFail(result);
                }));
    }
}

internal class FeatureSelfProfileHelper implements IFeatureSelfProfile {
    private var profiles:IFeatureProfiles;
    private var userId:IFeatureUserId;

    public function FeatureSelfProfileHelper(profiles:IFeatureProfiles, userId:IFeatureUserId) {
        this.profiles = profiles;
        this.userId = userId;
    }

    public function getSelfProfile(handler:IOneProfileHandler):void {
        profiles.getProfiles([userId.getUserId()],
                new ProfilesHandler(function (profiles:Array):void {
                    handler.onSuccess(profiles[0]);
                }, function (result:Object):void {
                    handler.onFail(result);
                }));
    }
}

internal class NetworkConnection implements INetworkConnection {
    private var bus:IBus;
    private var config:INetworkConfig;

    public function NetworkConnection(bus:IBus, config:INetworkConfig) {
        this.bus = bus;
        this.config = config;
    }

    public function getBus():IBus {
        return bus;
    }

    public function getConfig():INetworkConfig {
        return config;
    }

    public function getFeature(featureClass:Class):* {
        return bus.getFeature(featureClass);
    }

    public function hasFeature(featureClass:Class):Boolean {
        return bus.hasFeature(featureClass);
    }
}

internal class NetworkModuleContext implements INetworkModuleContext {
    private var bus:IMutableBus;
    private var config:INetworkConfig;
    private var fFlashVarsGetter:IFeatureFlashVarsGetter;
    private var fLog:IFeatureLog;
    private var fHttpRequester:IFeatureHttpRequester;
    private var fJavaScript:IFeatureJavaScript;
    private var fEventDispatcher:IFeatureEventDispatcher;
    private var jsUtils:JavaScriptUtils;


    public function NetworkModuleContext(bus:IMutableBus, config:INetworkConfig) {
        this.bus = bus;
        this.config = config;
        this.fFlashVarsGetter = bus.getFeature(IFeatureFlashVarsGetter);
        this.fLog = bus.getFeature(IFeatureLog);
        this.fHttpRequester = bus.getFeature(IFeatureHttpRequester);
        this.fJavaScript = bus.getFeature(IFeatureJavaScript);
        this.fEventDispatcher = bus.getFeature(IFeatureEventDispatcher);
    }

    public function getBus():IMutableBus {
        return bus;
    }

    public function getConfig():INetworkConfig {
        return config;
    }

    public function getFlashVars():FlashVars {
        return fFlashVarsGetter ? fFlashVarsGetter.getFlashVars() : new FlashVars(null);
    }

    public function getLog():IFeatureLog {
        return fLog;
    }

    public function getHttpRequester():IFeatureHttpRequester {
        return fHttpRequester;
    }

    public function getJavaScript():IFeatureJavaScript {
        return fJavaScript;
    }

    public function getEventDispatcher():IEventDispatcher {
        return fEventDispatcher;
    }

    public function log(msg:*):void {
        fLog.log(msg);
    }

    public function apiLog(msg:*):void {
        fLog.apiLog("API: " + FeatureLogTrace.universalDump(msg));
    }

    public function eventLog(msg:*):void {
        fLog.eventLog("EVENT: " + FeatureLogTrace.universalDump(msg));
    }

    public function getJavaScriptUtils():JavaScriptUtils {
        if (jsUtils == null) {
            jsUtils = new JavaScriptUtils(fJavaScript);
        }
        return jsUtils;
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.