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;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading;
5using System.Threading.Tasks;
6using Waher.Events;
10
11namespace Waher.Things.Files
12{
16 [Singleton]
17 public class FilesModule : IModule
18 {
19 private static readonly Dictionary<string, KeyValuePair<FolderNode, FileSystemWatcher>> watchers = new Dictionary<string, KeyValuePair<FolderNode, FileSystemWatcher>>();
20 private static readonly SemaphoreSlim synchObj = new SemaphoreSlim(1);
21
25 public async Task Start()
26 {
27 try
28 {
29 await this.CheckNode(MeteringTopology.Root);
30 }
31 catch (Exception ex)
32 {
33 Log.Exception(ex);
34 }
35 }
36
37 private async Task CheckNode(INode Node)
38 {
39 foreach (INode Child in await Node.ChildNodes)
40 {
41 if (Child is FolderNode FolderNode)
42 {
43 try
44 {
45 await FolderNode.Synchronize();
46 }
47 catch (Exception ex)
48 {
49 Log.Exception(ex);
50 }
51 }
52 else if (Child is VirtualNode VirtualNode)
53 await this.CheckNode(VirtualNode);
54 }
55 }
56
60 public async Task Stop()
61 {
62 await synchObj.WaitAsync();
63 try
64 {
65 foreach (KeyValuePair<FolderNode, FileSystemWatcher> P in watchers.Values)
66 P.Value.Dispose();
67
68 watchers.Clear();
69 }
70 finally
71 {
72 synchObj.Release();
73 }
74 }
75
76 internal static async Task StopSynchronization(string FolderPath)
77 {
78 await synchObj.WaitAsync();
79 try
80 {
81 if (watchers.TryGetValue(FolderPath, out KeyValuePair<FolderNode, FileSystemWatcher> P))
82 {
83 watchers.Remove(FolderPath);
84 P.Value.Dispose();
85 }
86 }
87 finally
88 {
89 synchObj.Release();
90 }
91 }
92
93 internal static async Task CheckSynchronization(FolderNode Node)
94 {
95 try
96 {
97 FileSystemWatcher Watcher;
98
99 await synchObj.WaitAsync();
100 try
101 {
102 if (watchers.TryGetValue(Node.FolderPath, out KeyValuePair<FolderNode, FileSystemWatcher> P))
103 {
104 P.Value.Changed -= Node.Watcher_Changed;
105 P.Value.Created -= Node.Watcher_Created;
106 P.Value.Deleted -= Node.Watcher_Deleted;
107 P.Value.Renamed -= Node.Watcher_Renamed;
108 P.Value.Error -= Node.Watcher_Error;
109
110 P.Value.EnableRaisingEvents = false;
111
112 watchers.Remove(Node.FolderPath);
113 P.Value.Dispose();
114 }
115
116 if (!Directory.Exists(Node.FolderPath))
117 Directory.CreateDirectory(Node.FolderPath);
118
119 if (Node.SynchronizationOptions != SynchronizationOptions.NoSynchronization)
120 {
121 if (string.IsNullOrEmpty(Node.FileFilter))
122 Watcher = new FileSystemWatcher(Node.FolderPath);
123 else
124 Watcher = new FileSystemWatcher(Node.FolderPath, Node.FileFilter);
125
126 watchers[Node.FolderPath] = new KeyValuePair<FolderNode, FileSystemWatcher>(Node, Watcher);
127
128 Watcher.NotifyFilter =
129 NotifyFilters.Attributes |
130 NotifyFilters.CreationTime |
131 NotifyFilters.DirectoryName |
132 NotifyFilters.FileName |
133 NotifyFilters.LastAccess |
134 NotifyFilters.LastWrite |
135 NotifyFilters.Security |
136 NotifyFilters.Size;
137
138 Watcher.Changed += Node.Watcher_Changed;
139 Watcher.Created += Node.Watcher_Created;
140 Watcher.Deleted += Node.Watcher_Deleted;
141 Watcher.Renamed += Node.Watcher_Renamed;
142 Watcher.Error += Node.Watcher_Error;
143
144 Watcher.IncludeSubdirectories = Node.SynchronizationOptions == SynchronizationOptions.IncludeSubfolders;
145 Watcher.EnableRaisingEvents = true;
146 }
147 }
148 finally
149 {
150 synchObj.Release();
151 }
152
153 await Node.SynchFolder();
154 await Node.RemoveErrorAsync("SynchError");
155 }
156 catch (Exception ex)
157 {
158 await Node.LogErrorAsync("SynchError", ex.Message);
159 }
160 }
161 }
162}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
Module maintaining active file system watchers.
Definition: FilesModule.cs:18
async Task Stop()
Stops the module.
Definition: FilesModule.cs:60
async Task Start()
Starts the module.
Definition: FilesModule.cs:25
Represents a file folder in the file system.
Definition: FolderNode.cs:44
Task Synchronize()
Synchronizes folder, subfolders, files and nodes.
Definition: FolderNode.cs:174
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