1 /**
2 Copyright 2018 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.app.platform;
23 
24 private import std.exception;
25 
26 //private import aermicioi.aedi;
27 private import hunt.cache;
28 
29 private import dxx.util;
30 
31 private import dxx.app;
32 
33 private import dxx.app.resource;
34 private import dxx.app.document;
35 private import dxx.app.component;
36 private import dxx.app.workbench;
37 private import dxx.app.extension;
38 
39 //interface DocumentResourceAdaptor {
40     //DocumentType getDocType(FileResource);
41 //}
42 
43 interface PlatformComponents {
44     URIResolver getURIResolver();
45     ResourceValidator getResourceValidator();
46     ResourceContentProvider getResourceContentProvider();
47     //DocumentResourceAdaptor getDocumentResourceAdaptor();
48 
49     public WorkflowRunner getWorkflowRunner();
50     public PluginLoader getPluginLoader();
51     public ExtensionsManager getExtensionsManager();
52 }
53 
54 class Platform
55 {
56     static class ThreadLocal :
57         URIResolver,
58         ResourceValidator,
59         ResourceContentProvider
60         //DocumentResourceAdaptor
61     {
62         static WorkflowRunner workflowRunner;
63 
64         Resource resolveURI(string uri,ResourceSet owner) {
65             return null;
66         }
67         bool isValid(ResourceSet set) {
68             return true;
69         }
70         bool isValidResource(Resource res) {
71             return true;
72         }
73         void* getContent(Resource) {
74             return null;
75         }
76         override ubyte[] getContent(Resource) {
77             return [];
78         }
79         override void putContent(ubyte[],Resource) {
80         }
81         DocumentType getDocType(FileResource) {
82             return null;
83         }
84 
85         WorkflowRunner getWorkflowRunner() {
86             if(workflowRunner is null) {
87                 workflowRunner = new WorkflowRunner;
88             }
89             return workflowRunner;
90         }
91 
92     }
93     static ThreadLocal _local;
94 
95     static auto getLocals() {
96         if(_local is null) {
97             _local = new ThreadLocal;
98         }
99         return _local;
100     }
101 
102     static __gshared Platform INSTANCE;
103 
104     static auto getInstance() {
105         static bool instantiated = false;
106         if(!instantiated) {
107             synchronized(Platform.classinfo) {
108                 if(!INSTANCE) {
109                     INSTANCE=new Platform;
110                 }
111             }
112             instantiated = true;
113         }
114         return INSTANCE;
115     }
116 
117     //static Workspace getDefaultWorkspace() {
118     //    return resolveInjector!(Workspace)("app.workspace");
119     //}
120     static Workbench getDefaultWorkbench() {
121         return resolveInjector!(Workbench)("app.workbench");
122     }
123 
124     ExtensionsManager extensionsManager;
125 
126     private this() {
127         extensionsManager = new ExtensionsManager;
128     }
129 
130     _PluginLoader[string] plugins;
131 
132     class _PluginLoader : PluginLoader {
133       override void load(string path) {
134           super.load(path);
135           plugins[desc.id] = this;
136       }
137     }
138     PluginLoader newPluginLoader() {
139       return new _PluginLoader();
140     }
141 }
142 
143 interface PlatformJob {
144     T getProperty(T)(string id);
145     void setProperty(T)(T t,string id);
146 }
147 
148 class PlatformJobBase : JobBase,PlatformJob {
149     Workspace workspace;
150     UCache cache;
151 
152     this(Workspace w = Platform.getInstance.getDefaultWorkbench.getWorkspace) {
153         super();
154         cache = UCache.CreateUCache();
155         workspace = w;
156     }
157 
158     //@property nothrow
159     //ref inout (Variant[string]) param() inout {
160     //    return _param;
161     //}
162     override void setup() {
163         //Workspace.lock;
164     }
165     override void terminate() {
166         //Workspace.unlock;
167     }
168 
169     override void process() {
170         processPlatformJob();
171     }
172 
173     void processPlatformJob() {
174     }
175 
176     T getProperty(T)(string id) {
177         return cache.put!T(id);
178     }
179     void setProperty(T)(T t,string id) {
180         cache.put!T(id,t);
181     }
182 
183 }
184 
185 struct PluginDef {
186     public string name;
187     public string pluginVersion;
188     //public string[string] properties;
189 }
190 
191 struct PlatformParam {
192   PluginDef[] plugins;
193 }
194 
195 alias DXXParam = Tuple!(
196   PlatformParam,"dxx"
197 );
198 
199 @component
200 class PlatformRuntime(Param...) :
201             RuntimeComponents!(Param,PlatformParam),
202             PlatformComponents {
203 
204     @component
205     override URIResolver getURIResolver() {
206         return Platform.getLocals();
207     }
208 
209     @component
210     override ResourceValidator getResourceValidator() {
211         return Platform.getLocals;
212     }
213 
214     @component
215     override ResourceContentProvider getResourceContentProvider() {
216         return Platform.getLocals;
217     }
218 
219     //@component
220     //override DocumentResourceAdaptor getDocumentResourceAdaptor() {
221     //    return Platform.getInstance();
222     //}
223 
224     @component
225     override PluginLoader getPluginLoader() {
226         return Platform.getInstance.newPluginLoader;
227     }
228 
229     @component
230     public ExtensionsManager getExtensionsManager() {
231         return Platform.getInstance.extensionsManager;
232     }
233 
234     @component
235     public WorkflowRunner getWorkflowRunner() {
236         return Platform.getLocals.getWorkflowRunner;
237     }
238 
239     void registerPlatformDependencies(InjectionContainer injector) {
240         Workspace w = new WorkspaceDefault;
241         injector.register!(Workspace)(w,"app.workspace");
242     }
243 
244     override void registerAppDependencies(InjectionContainer injector) {
245         super.registerAppDependencies(injector);
246         registerPlatformDependencies(injector);
247     }
248 }