-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
105 lines (96 loc) · 4.1 KB
/
Program.cs
File metadata and controls
105 lines (96 loc) · 4.1 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace NetworkFileSync
{
class Program
{
private static IConfigurationRoot _configuration;
private static TcpTransceiver<FileChange> _transceiver;
private static TcpClient _connection;
private static string _clientServer;
private static string _blueprintsFolder;
private static IPAddress _remoteAddress;
private static int _remotePort;
private static FileSystemWatcher _localWatcher;
static void Main(string[] args)
{
_configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false)
.Build();
_clientServer = _configuration["Settings:clientServer"];
_blueprintsFolder = _configuration["Settings:blueprintsFolder"];
_remoteAddress = Dns.GetHostEntry(_configuration["Settings:remoteAddress"]).AddressList[0];
_remotePort = int.Parse(_configuration["Settings:remotePort"]);
var encoder = new FileChangeEncoder();
_transceiver = new(encoder);
switch (_clientServer.ToUpper())
{
case "SERVER":
var server = new ConnectionListener(_remoteAddress, _remotePort);
System.Console.WriteLine("Listening...");
_connection = server.Listen();
System.Console.WriteLine("Connected!");
_transceiver.BeginReceive(_connection, UpdateLocal);
break;
case "CLIENT":
var client = new ClientConnector(_remoteAddress, _remotePort);
System.Console.WriteLine("Connecting...");
_connection = client.Connect();
System.Console.WriteLine("Connected!");
_transceiver.BeginReceive(_connection, UpdateLocal);
break;
}
CreateLocalWatcher();
Console.ReadLine();
}
private static void OnLocalWrite(object sender, FileSystemEventArgs e)
{
var fullPath = e.FullPath;
var fileName = fullPath.Replace(_blueprintsFolder, "");
var changeType = e.ChangeType;
var fileChange = new FileChange { Name = fileName, Type = changeType };
Task.Run(() => UpdateRemote(fileChange));
}
private static void UpdateLocal(FileChange fileChange)
{
_localWatcher.Dispose();
System.Console.WriteLine($"Update local: \t{fileChange.Type} \t{fileChange.Name}");
var filePath = _blueprintsFolder + fileChange.Name;
switch (fileChange.Type)
{
case WatcherChangeTypes.Created:
case WatcherChangeTypes.Changed:
File.WriteAllText(filePath, fileChange.Content);
break;
case WatcherChangeTypes.Deleted:
File.Delete(filePath);
break;
case WatcherChangeTypes.Renamed:
// Rename file: Rename-Item -Path $filePath -NewName $Content
break;
}
CreateLocalWatcher();
}
private static void CreateLocalWatcher() {
_localWatcher = new FileSystemWatcher();
_localWatcher.Path = _blueprintsFolder;
_localWatcher.Filter = "*.txt";
_localWatcher.IncludeSubdirectories = true;
_localWatcher.EnableRaisingEvents = true;
_localWatcher.Created += OnLocalWrite;
_localWatcher.Changed += OnLocalWrite;
_localWatcher.Deleted += OnLocalWrite;
_localWatcher.Renamed += OnLocalWrite;
}
private static async Task UpdateRemote(FileChange fileChange)
{
System.Console.WriteLine($"Update remote: \t{fileChange.Type} \t{fileChange.Name}");
await _transceiver.BeginSend(_connection, fileChange);
}
}
}