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 | 1 | package org.vostokframework.domain.loading.loaders |
30 | |
{ |
31 | |
import org.as3utils.StringUtil; |
32 | |
import org.as3utils.URLUtil; |
33 | |
import org.vostokframework.VostokFramework; |
34 | |
import org.vostokframework.VostokIdentification; |
35 | |
import org.vostokframework.domain.assets.AssetType; |
36 | |
import org.vostokframework.domain.loading.GlobalLoadingSettings; |
37 | |
import org.vostokframework.domain.loading.ILoader; |
38 | |
import org.vostokframework.domain.loading.ILoaderFactory; |
39 | |
import org.vostokframework.domain.loading.ILoaderState; |
40 | |
import org.vostokframework.domain.loading.ILoadingSettingsFactory; |
41 | |
import org.vostokframework.domain.loading.LoadPriority; |
42 | |
import org.vostokframework.domain.loading.LoaderRepository; |
43 | |
import org.vostokframework.domain.loading.settings.LoadingCacheSettings; |
44 | |
import org.vostokframework.domain.loading.settings.LoadingExtraSettings; |
45 | |
import org.vostokframework.domain.loading.settings.LoadingMediaSettings; |
46 | |
import org.vostokframework.domain.loading.settings.LoadingPolicySettings; |
47 | |
import org.vostokframework.domain.loading.settings.LoadingSecuritySettings; |
48 | |
import org.vostokframework.domain.loading.settings.LoadingSettings; |
49 | |
import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm; |
50 | |
import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithmFactory; |
51 | |
import org.vostokframework.domain.loading.states.fileloader.QueuedFileLoader; |
52 | |
import org.vostokframework.domain.loading.states.fileloader.algorithms.FileLoadingAlgorithmFactory; |
53 | |
import org.vostokframework.domain.loading.states.queueloader.IQueueLoadingPolicy; |
54 | |
import org.vostokframework.domain.loading.states.queueloader.QueueLoadingStatus; |
55 | |
import org.vostokframework.domain.loading.states.queueloader.QueuedQueueLoader; |
56 | |
import org.vostokframework.domain.loading.states.queueloader.policies.SpecialHighestLowestQueueLoadingPolicy; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public class VostokLoaderFactory implements ILoaderFactory |
64 | |
{ |
65 | |
|
66 | |
|
67 | |
|
68 | |
private var _fileLoadingAlgorithmFactory:IFileLoadingAlgorithmFactory; |
69 | |
private var _loadingSettingsFactory:ILoadingSettingsFactory; |
70 | |
|
71 | |
public function get fileLoadingAlgorithmFactory(): IFileLoadingAlgorithmFactory { return _fileLoadingAlgorithmFactory; } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public function VostokLoaderFactory(loadingSettingsFactory:ILoadingSettingsFactory) |
80 | 1 | { |
81 | 1 | if (!loadingSettingsFactory) throw new ArgumentError("Argument <loadingSettingsFactory> must not be null."); |
82 | |
|
83 | 1 | _loadingSettingsFactory = loadingSettingsFactory; |
84 | 1 | _fileLoadingAlgorithmFactory = new FileLoadingAlgorithmFactory(); |
85 | 1 | } |
86 | |
|
87 | |
public function createComposite(identification:VostokIdentification, loaderRepository:LoaderRepository, globalLoadingSettings:GlobalLoadingSettings, priority:LoadPriority = null, localMaxConnections:int = 3):ILoader |
88 | |
{ |
89 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
90 | 1 | if (!loaderRepository) throw new ArgumentError("Argument <loaderRepository> must not be null."); |
91 | |
|
92 | 1 | if (!priority) priority = LoadPriority.MEDIUM; |
93 | |
|
94 | 1 | var policy:IQueueLoadingPolicy = createPolicy(loaderRepository, globalLoadingSettings); |
95 | 1 | var state:ILoaderState = createCompositeLoaderState(policy, localMaxConnections); |
96 | |
|
97 | 1 | return instantiateComposite(identification, state, priority); |
98 | |
} |
99 | |
|
100 | |
public function createLeaf(identification:VostokIdentification, src:String, type:AssetType, settings:LoadingSettings = null):ILoader |
101 | |
{ |
102 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
103 | 1 | if (StringUtil.isBlank(src)) throw new ArgumentError("Argument <src> must not be null nor an empty String.."); |
104 | 1 | if (!type) throw new ArgumentError("Argument <type> must not be null."); |
105 | |
|
106 | 1 | if (!settings) settings = _loadingSettingsFactory.create(); |
107 | |
|
108 | 1 | var state:ILoaderState = createLeafLoaderState(type, src, settings); |
109 | 1 | return instantiateLeaf(identification, state, settings.policy.priority); |
110 | |
} |
111 | |
|
112 | |
public function setFileLoadingAlgorithmFactory(factory:IFileLoadingAlgorithmFactory): void |
113 | |
{ |
114 | 0 | if (!factory) throw new ArgumentError("Argument <factory> must not be null."); |
115 | 0 | _fileLoadingAlgorithmFactory = factory; |
116 | 0 | } |
117 | |
|
118 | |
protected function createCompositeLoaderState(policy:IQueueLoadingPolicy, localMaxConnections:int):ILoaderState |
119 | |
{ |
120 | 1 | var queueLoadingStatus:QueueLoadingStatus = new QueueLoadingStatus(); |
121 | 1 | var state:ILoaderState = new QueuedQueueLoader(queueLoadingStatus, policy, localMaxConnections); |
122 | |
|
123 | 1 | return state; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
protected function createDefaultLoadingSettings():LoadingSettings |
130 | |
{ |
131 | 0 | var cache:LoadingCacheSettings = new LoadingCacheSettings(); |
132 | 0 | var extra:LoadingExtraSettings = new LoadingExtraSettings(); |
133 | 0 | var media:LoadingMediaSettings = new LoadingMediaSettings(); |
134 | 0 | var policy:LoadingPolicySettings = new LoadingPolicySettings(); |
135 | 0 | var security:LoadingSecuritySettings = new LoadingSecuritySettings(); |
136 | |
|
137 | 0 | cache.allowInternalCache = true; |
138 | 0 | cache.killExternalCache = false; |
139 | |
|
140 | 0 | media.autoCreateVideo = false; |
141 | 0 | media.autoResizeVideo = false; |
142 | 0 | media.autoStopStream = false; |
143 | 0 | media.bufferPercent = .1; |
144 | |
|
145 | 0 | policy.latencyTimeout = 12000; |
146 | 0 | policy.maxAttempts = 2; |
147 | 0 | policy.priority = LoadPriority.MEDIUM; |
148 | |
|
149 | 0 | var settings:LoadingSettings = new LoadingSettings(); |
150 | 0 | settings.cache = cache; |
151 | 0 | settings.extra = extra; |
152 | 0 | settings.media = media; |
153 | 0 | settings.policy = policy; |
154 | 0 | settings.security = security; |
155 | |
|
156 | 0 | return settings; |
157 | |
} |
158 | |
|
159 | |
protected function createLeafLoaderState(type:AssetType, url:String, settings:LoadingSettings):ILoaderState |
160 | |
{ |
161 | 1 | var killExternalCache:Boolean = settings.cache.killExternalCache; |
162 | 1 | var baseURL:String = settings.extra.baseURL; |
163 | |
|
164 | 1 | url = parseUrl(url, killExternalCache, baseURL); |
165 | 1 | var algorithm:IFileLoadingAlgorithm = _fileLoadingAlgorithmFactory.create(type, url, settings); |
166 | |
|
167 | 1 | return new QueuedFileLoader(algorithm); |
168 | |
} |
169 | |
|
170 | |
protected function instantiateComposite(identification:VostokIdentification, state:ILoaderState, priority:LoadPriority):ILoader |
171 | |
{ |
172 | 1 | return new VostokLoader(identification, state, priority); |
173 | |
} |
174 | |
|
175 | |
protected function instantiateLeaf(identification:VostokIdentification, state:ILoaderState, priority:LoadPriority):ILoader |
176 | |
{ |
177 | 1 | return new VostokLoader(identification, state, priority); |
178 | |
} |
179 | |
|
180 | |
protected function parseUrl(url:String, killExternalCache:Boolean, baseURL:String):String |
181 | |
{ |
182 | 1 | if (killExternalCache) url = URLUtil.appendVar(url, VostokFramework.KILL_CACHE_VAR_NAME, String(new Date().getTime())); |
183 | 1 | if (baseURL != null) url = baseURL + url; |
184 | |
|
185 | 1 | return url; |
186 | |
} |
187 | |
|
188 | |
protected function createPolicy(loaderRepository:LoaderRepository, globalLoadingSettings:GlobalLoadingSettings):IQueueLoadingPolicy |
189 | |
{ |
190 | 1 | var policy:IQueueLoadingPolicy = new SpecialHighestLowestQueueLoadingPolicy(loaderRepository, globalLoadingSettings); |
191 | 1 | return policy; |
192 | |
} |
193 | |
|
194 | |
} |
195 | |
|
196 | |
} |