Initial networking support.
[delight/dlt-lib.git] / dlt / _externals.dlt
blobb0a5bdb48f64145f7083a1f30646124e1b322b93
1 module dlt._externals
3 # Do not import this module. It is used internally when instantiating the Main class.
5 # TODO: this is a mess. It creates dependencies on all kinds of modules. The system
6 # get these from all over the place.
8 import tango.core.Vararg
9 import tango.text.convert.Layout
10 import tango.io.Console
11 import tango.io.Path
12 import tango.io.File
13 import tango.io.Print
14 import tango.io.Console
15 import tango.net.ServerSocket: ServerSocket
16 import tangoFC = tango.io.FileConduit
17 import tangoFS = tango.io.FileSystem
18 import tango.time.Time
19 import tango.util.log.Log
20 import tango.util.log.AppendConsole
21 import tango.util.log.LayoutDate
22 import dlt.net
23 import dlt.io
24 import dlt.time: Clock
25 import tango.time.WallClock: WallClock
26 static import tango.time.Clock
27 static import tango.stdc.stdlib
29 class Externals:
30         FileSystem? _fs
31         Logger rootLogger
32         Printer? _stdout, _stderr
33         Clock? _systemClock
34         Network? _network
36         this():
37                 __log = &log_impl
38                 auto layout = new tango.util.log.LayoutDate.LayoutDate()
39                 rootLogger = cast(Logger) Log.root
40                 rootLogger.add(new AppendConsole(layout));
41                 rootLogger.level = Level.Warn
43         public Logger logger():
44                 return rootLogger
46         public Console.Input cin():
47                 return cast(Console.Input) Cin
49         public Console.Output cout():
50                 return cast(Console.Output) Cout
52         public Console.Output cerr():
53                 return cast(Console.Output) Cerr
54         
55         private void ensureStdout():
56                 if not _stdout:
57                         assert not _stderr
58                         auto layout = new Layout!(char)
59                         _stdout = new Printer(layout, cout().stream)
60                         _stderr = new Printer(layout, cerr().stream)
62         public Printer stdout():
63                 ensureStdout()
64                 return cast(Printer) _stdout
66         public Printer stderr():
67                 ensureStdout()
68                 return cast(Printer) _stderr
70         public FileSystem fs():
71                 if not _fs: _fs = new HostFS()
72                 return cast(FileSystem) _fs
74         public Clock systemClock():
75                 if not _systemClock: _systemClock = new HostClock()
76                 return cast(Clock) _systemClock
78         public void SystemExit(dlt.core.SystemExit ex):
79                 Printer err = stderr()
80                 err(ex.toString())
81                 err.newline
82                 err()
83                 tango.stdc.stdlib.exit(ex.exitCode)
85         public Network network():
86                 if not _network:
87                         _network = new HostNetwork()
88                 return notnull(_network)
90 void log_impl(int level, char[] src, char[] msg, ...):
91         auto layout = new Layout!(char)()
92         Logger logger = cast(Logger) Log.getLogger(src)
93         logger.append(cast(Level) level, layout(_arguments, _argptr, msg))
95 class HostFilePathEnumerator extends FilePathEnumerator:
96         in HostFilePath path
97         in char[] attributes
98         in QueryInfoFlags flags
100         int opApply(int delegate(ref FileInfo) dg):
101                 int result = 0
102                 FileInfo fi
103                 for tfi in .children(path._path):
104                         fi.strings["standard::name"] = tfi.name
105                         result = dg(fi)
106                         if result: break
107                 return result
109 class HostFilePath extends FilePath:
110         private char[] _path
112         this(char[] path):
113                 assert path
114                 this._path = path
115                 
116         # Information about this path
117         char[]          basename        (): throw new Exception("Not implemented")
118         char[]          path            (): return _path
119         char[]          uri             (): throw new Exception("Not implemented")
120         char[]          parseName       (): throw new Exception("Not implemented")
121         bool            hasPrefix       (): throw new Exception("Not implemented")
122         bool            isNative        (): throw new Exception("Not implemented")
123         char[]          uriScheme       (): throw new Exception("Not implemented")
124         bool            hasUriScheme    (char[] uriScheme): throw new Exception("Not implemented")
126         # Navigating to other paths
127         FilePath        parent          (): throw new Exception("Not implemented")
128         FilePath        child           (char[] name): throw new Exception("Not implemented")
129         char[]          relativePath    (FilePath descendant): throw new Exception("Not implemented")
130         FilePath        resolveRelativePath     (char[] relativePath): throw new Exception("Not implemented")
132         FilePathEnumerator children(char[] attributes, QueryInfoFlags flags):
133                 return new HostFilePathEnumerator(this, attributes, flags)
135         # Querying file meta-data
136         FileInfo        queryInfo       (char[] attributes, QueryInfoFlags flags): throw new Exception("Not implemented")
137         bool            queryExists     (): throw new Exception("Not implemented")
138         FileType        queryFileType   (QueryInfoFlags flags): throw new Exception("Not implemented")
139         FileInfo        queryFilesystemInfo     (char[] attributes): throw new Exception("Not implemented")
140         AttributeInfo[] querySettableAttributes(): throw new Exception("Not implemented")
141         AttributeInfo[] queryWritableNamespaces(): throw new Exception("Not implemented")
143         void            setAttribute    (char[] attribute, bool value, QueryInfoFlags): throw new Exception("Not implemented")
144         void            setAttribute    (char[] attribute, int value, QueryInfoFlags): throw new Exception("Not implemented")
145         void            setAttribute    (char[] attribute, long value, QueryInfoFlags): throw new Exception("Not implemented")
146         void            setAttribute    (char[] attribute, char[] value, QueryInfoFlags): throw new Exception("Not implemented")
147         void            copyAttributes  (FilePath destination, CopyFlags flags): throw new Exception("Not implemented")
149         Mount           findEnclosingMount      (): throw new Exception("Not implemented")
151         # File operations
152         void remove(): .remove(path)
154         void            trash   (): throw new Exception("Not implemented")
156         void copy(FilePath destination, CopyFlags flags):
157                 if flags: throw new Exception("Unknown flag")   # TODO
158                 .copy(_path, destination.path)
160         void move(FilePath destination, CopyFlags flags):
161                 if flags: throw new Exception("Unknown flag")   # TODO
162                 .rename(_path, destination.path)
164         void makeDirectory(): .createPath(_path)
166         void            makeSymbolicLink        (char[] symlinkValue): throw new Exception("Not implemented")
168         # Mount points
169         void            mountMountable          (MountFlags flags, MountOperation mountOperation): throw new Exception("Not implemented")
170         void            unmountMountable        (UnmountFlags flags): throw new Exception("Not implemented")
171         void            ejectMountable          (UnmountFlags flags): throw new Exception("Not implemented")
172         void            mountEnclosingVolume    (MountFlags flags, MountOperation mountOperation): throw new Exception("Not implemented")
174         # Monitoring
175         FSMonitor       monitorDirectory        (MonitorFlags flags): throw new Exception("Not implemented")
176         FSMonitor       monitorFile             (MonitorFlags flags): throw new Exception("Not implemented")
177         FSMonitor       monitor                 (MonitorFlags flags): throw new Exception("Not implemented")
179         # File contents
180         InputStream read():
181                 return cast(InputStream) (new tangoFC.FileConduit(_path)).input
183         OutputStream    appendTo        (CreateFlags flags): throw new Exception("Not implemented")
185         OutputStream    create          (CreateFlags flags = CreateFlags.None):
186                 if flags: throw new Exception("Unknown flag")   # TODO
187                 # TODO: make sure file doesn't already exist
188                 return cast(OutputStream) (new tangoFC.FileConduit(_path, tangoFC.FileConduit.WriteCreate)).output
190         OutputStream    replace         (char[] etag, bool makeBackup, CreateFlags flags): throw new Exception("Not implemented")
192         char[] loadContents(out char[] etag): throw new Exception("Not implemented")
194         char[] loadContents():
195                 return cast(char[]) (new File(_path)).read()
197         void            replaceContents(char[] contents, size_t length, char[] etag,
198                                          bool makeBackup, CreateFlags flags, char[] newEtag): throw new Exception("Not implemented")
199         
200         override char[] toString():
201                 return _path
203 class HostFS extends FileSystem:
204         FilePath path(char[] pathname):
205                 return new HostFilePath(pathname)
207         FilePath cwd():
208                 return new HostFilePath(tangoFS.FileSystem.getDirectory)
210         void cwd(FilePath path):
211                 tangoFS.FileSystem.setDirectory(path.path)
213 class HostNetwork extends Network:
214         ServerSocket newServerSocket(InternetAddress address,
215                                      int backlog,
216                                      bool reuse):
217                 return new ServerSocket(address, backlog, reuse)
219         Socket newClientSocket(AddressFamily family, SocketType type, ProtocolType protocol):
220                 return new Socket(family, type, protocol)
222 class HostClock extends Clock:
223         Time now(): return tango.time.Clock.Clock.now()
225         TimeSpan zone(): return WallClock.zone()
227         DateTime toLocalTime(Time utc): return WallClock.toDate(utc)
229         override char[] toString():
230                 auto layout = new Layout!(char)
231                 TimeOfDay tod = toLocalTime(now).time
232                 return layout.convert("{}:{:d02}:{:d02} local time",
233                                         tod.hours, tod.minutes, tod.seconds)