public BackupWorker(object mutex, Func <INode?> getLastTree, FilePathBuilder pathBuilder, Func <Timestamp?> getTimestamp) { _mutex = mutex; _getLastTree = getLastTree; _pathBuilder = pathBuilder; _getTimestamp = getTimestamp; }
public CommandHandleVisitor(INode root, FilePathBuilder pathBuilder) { _root = root; _pathBuilder = pathBuilder; }
private static void Main(string[] args) { HandleFallbackArgs(ref args); var(localPort, backupPort) = ParseLocalPorts(args); lock (Mutex) { _pathBuilder = new FilePathBuilder($"fs{localPort}_"); } if (TryParseRemoteBackupAddress(args, out var remoteBackupIp, out var remoteBackupPort)) { HandleBackup(localPort, remoteBackupIp, remoteBackupPort); } lock (Mutex) { new BackupWorker(Mutex, () => _lastTree, _pathBuilder, () => _timestamp).LaunchOn(backupPort); } var ip = IpAddressUtils.GetLocal(); var local = new IPEndPoint(ip, localPort); using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); socket.Bind(local); Console.WriteLine($"File server is listening on {local}."); socket.Listen(1); using var client = socket.Accept(); var buffer = new byte[32000]; while (true) { var command = client.ReceiveUntilEof(buffer).To <ICommand>(); Console.WriteLine($"Received {command}."); lock (Mutex) { var statefulCommand = command as StatefulCommand; if (statefulCommand != null) { _lastTree = statefulCommand.Root; } if (_lastTree == null) { continue; } Console.WriteLine(_lastTree); var visitor = new CommandHandleVisitor(_lastTree, _pathBuilder); command.Accept(visitor); Console.WriteLine($"Handled {command}."); ICommand response = visitor.Payload != null ? new PayloadResponseCommand(command, visitor.Payload, visitor.PayloadPath, _lastTree, _timestamp) : new ResponseCommand(command); client.SendCompletelyWithEof(response.ToBytes()); if (statefulCommand != null && _timestamp.Value >= statefulCommand.Timestamp.Value - 1) { _timestamp = statefulCommand.Timestamp; } Console.WriteLine($"Sent {response}."); } } }