Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FilesModule.cs
1using System;
3using System.IO;
4using System.Threading;
5using System.Threading.Tasks;
6using Waher.Events;
11
12namespace Waher.Things.Files
13{
17 [Singleton]
18 public class FilesModule : IModule
19 {
20 private static readonly Dictionary<string, KeyValuePair<FolderNode, FileSystemWatcher>> watchers = new Dictionary<string, KeyValuePair<FolderNode, FileSystemWatcher>>();
21 private static readonly SemaphoreSlim synchObj = new SemaphoreSlim(1);
22
26 public async Task Start()
27 {
28 try
29 {
30 await this.CheckNode(MeteringTopology.Root);
31 }
32 catch (Exception ex)
33 {
34 Log.Exception(ex);
35 }
36 }
37
38 private async Task CheckNode(INode Node)
39 {
40 foreach (INode Child in await Node.ChildNodes)
41 {
42 if (Child is FolderNode FolderNode)
43 {
44 try
45 {
46 await FolderNode.Synchronize();
47 }
48 catch (Exception ex)
49 {
50 Log.Exception(ex);
51 }
52 }
53 else if (Child is VirtualNode || Child is NodeCollection)
54 await this.CheckNode(Child);
55 }
56 }
57
61 public async Task Stop()
62 {
63 await synchObj.WaitAsync();
64 try
65 {
66 foreach (KeyValuePair<FolderNode, FileSystemWatcher> P in watchers.Values)
67 P.Value.Dispose();
68
69 watchers.Clear();
70 }
71 finally
72 {
73 synchObj.Release();
74 }
75 }
76
77 internal static async Task StopSynchronization(string FolderPath)
78 {
79 await synchObj.WaitAsync();
80 try
81 {
82 if (watchers.TryGetValue(FolderPath, out KeyValuePair<FolderNode, FileSystemWatcher> P))
83 {
84 watchers.Remove(FolderPath);
85 P.Value.Dispose();
86 }
87 }
88 finally
89 {
90 synchObj.Release();
91 }
92 }
93
94 internal static async Task CheckSynchronization(FolderNode Node)
95 {
96 try
97 {
98 FileSystemWatcher Watcher;
99
100 await synchObj.WaitAsync();
101 try
102 {
103 if (watchers.TryGetValue(Node.FolderPath, out KeyValuePair<FolderNode, FileSystemWatcher> P))
104 {
105 P.Value.Changed -= Node.Watcher_Changed;
106 P.Value.Created -= Node.Watcher_Created;
107 P.Value.Deleted -= Node.Watcher_Deleted;
108 P.Value.Renamed -= Node.Watcher_Renamed;
109 P.Value.Error -= Node.Watcher_Error;
110
111 P.Value.EnableRaisingEvents = false;
112
113 watchers.Remove(Node.FolderPath);
114 P.Value.Dispose();
115 }
116
117 if (!Directory.Exists(Node.FolderPath))
118 Directory.CreateDirectory(Node.FolderPath);
119
120 if (Node.SynchronizationOptions != SynchronizationOptions.NoSynchronization)
121 {
122 if (string.IsNullOrEmpty(Node.FileFilter))
123 Watcher = new FileSystemWatcher(Node.FolderPath);
124 else
125 Watcher = new FileSystemWatcher(Node.FolderPath, Node.FileFilter);
126
127 watchers[Node.FolderPath] = new KeyValuePair<FolderNode, FileSystemWatcher>(Node, Watcher);
128
129 Watcher.InternalBufferSize = 65536;
130 Watcher.NotifyFilter =
131 NotifyFilters.Attributes |
132 NotifyFilters.CreationTime |
133 NotifyFilters.DirectoryName |
134 NotifyFilters.FileName |
135 NotifyFilters.LastAccess |
136 NotifyFilters.LastWrite |
137 NotifyFilters.Security |
138 NotifyFilters.Size;
139
140 Watcher.Changed += Node.Watcher_Changed;
141 Watcher.Created += Node.Watcher_Created;
142 Watcher.Deleted += Node.Watcher_Deleted;
143 Watcher.Renamed += Node.Watcher_Renamed;
144 Watcher.Error += Node.Watcher_Error;
145
146 Watcher.IncludeSubdirectories = Node.SynchronizationOptions == SynchronizationOptions.IncludeSubfolders;
147 Watcher.EnableRaisingEvents = true;
148 }
149 }
150 finally
151 {
152 synchObj.Release();
153 }
154
155 await Node.SynchFolder();
156 await Node.RemoveErrorAsync("SynchError");
157 }
158 catch (Exception ex)
159 {
160 await Node.LogErrorAsync("SynchError", ex.Message);
161 }
162 }
163 }
164}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Module maintaining active file system watchers.
Definition: FilesModule.cs:19
async Task Stop()
Stops the module.
Definition: FilesModule.cs:61
async Task Start()
Starts the module.
Definition: FilesModule.cs:26
Represents a file folder in the file system.
Definition: FolderNode.cs:44
Task Synchronize()
Synchronizes folder, subfolders, files and nodes.
Definition: FolderNode.cs:179
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
Interface for late-bound modules loaded at runtime.
Definition: IModule.cs:9
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: INode.cs:140
SynchronizationOptions
How a folder will synchronize nodes with contents of folders.
Definition: FolderNode.cs:23