-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedCoordinatorServer.cs
More file actions
169 lines (155 loc) · 5.95 KB
/
DistributedCoordinatorServer.cs
File metadata and controls
169 lines (155 loc) · 5.95 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using BitcoinFinder.Distributed;
namespace BitcoinFinder
{
public class DistributedCoordinatorServer
{
private readonly int _port;
private TcpListener? _listener;
public ConcurrentQueue<BlockTask> PendingTasks { get; } = new ConcurrentQueue<BlockTask>();
public ConcurrentDictionary<string, AgentInfo> ConnectedAgents { get; } = new ConcurrentDictionary<string, AgentInfo>();
public DistributedCoordinatorServer(int port = 5000)
{
_port = port;
}
public async Task StartServerAsync(CancellationToken token = default)
{
_listener = new TcpListener(IPAddress.Any, _port);
_listener.Start();
Console.WriteLine($"[SERVER] Started on port {_port}");
try
{
while (!token.IsCancellationRequested)
{
var client = await _listener.AcceptTcpClientAsync(token);
_ = HandleClientAsync(client);
}
}
catch (OperationCanceledException)
{
// graceful shutdown
}
catch (ObjectDisposedException)
{
// listener stopped
}
}
public void StopServer()
{
_listener?.Stop();
}
private async Task HandleClientAsync(TcpClient client)
{
using (client)
{
var endpoint = client.Client.RemoteEndPoint?.ToString() ?? "unknown";
Console.WriteLine($"[SERVER] Client connected {endpoint}");
using var stream = client.GetStream();
using var reader = new StreamReader(stream, Encoding.UTF8);
using var writer = new StreamWriter(stream, Encoding.UTF8) { AutoFlush = true };
while (client.Connected)
{
string? line;
try
{
line = await reader.ReadLineAsync();
}
catch
{
break;
}
if (line == null) break;
if (string.IsNullOrWhiteSpace(line)) continue;
BitcoinFinder.Distributed.Message msg;
try
{
msg = BitcoinFinder.Distributed.Message.FromJson(line);
}
catch (Exception ex)
{
Console.WriteLine($"[SERVER] Invalid message: {ex.Message}\nRaw: {line}");
continue;
}
var response = await ProcessMessageAsync(msg, endpoint);
if (response != null)
{
await writer.WriteLineAsync(response.ToJson());
}
else
{
// Логируем нераспознанное сообщение
Console.WriteLine($"[SERVER] Unhandled message type: {msg.Type} | Full: {System.Text.Json.JsonSerializer.Serialize(msg)}");
}
}
}
}
private async Task<BitcoinFinder.Distributed.Message?> ProcessMessageAsync(BitcoinFinder.Distributed.Message msg, string ip)
{
switch (msg.Type)
{
case MessageType.AGENT_REGISTER:
var agent = new AgentInfo
{
AgentId = msg.AgentId,
IpAddress = ip,
Status = "Idle",
LastHeartbeat = DateTime.UtcNow
};
ConnectedAgents[msg.AgentId] = agent;
Console.WriteLine($"[SERVER] Registered agent {msg.AgentId}");
return new BitcoinFinder.Distributed.Message
{
Type = MessageType.AGENT_REGISTER,
AgentId = msg.AgentId,
Data = JsonSerializer.SerializeToElement(new { status = "OK" }),
Timestamp = DateTime.UtcNow
};
case MessageType.AGENT_REQUEST_TASK:
if (PendingTasks.TryDequeue(out var task))
{
task.AssignedToAgent = msg.AgentId;
agent = ConnectedAgents[msg.AgentId];
agent.CurrentBlockId = task.BlockId;
ConnectedAgents[msg.AgentId] = agent;
return new BitcoinFinder.Distributed.Message
{
Type = MessageType.SERVER_TASK_ASSIGNED,
AgentId = msg.AgentId,
Data = JsonSerializer.SerializeToElement(task),
Timestamp = DateTime.UtcNow
};
}
else
{
return new BitcoinFinder.Distributed.Message
{
Type = MessageType.SERVER_NO_TASKS,
AgentId = msg.AgentId,
Timestamp = DateTime.UtcNow
};
}
}
await Task.Yield();
return null;
}
public void CreateTask(int blockId, long start, long end)
{
var task = new BlockTask
{
BlockId = blockId,
StartIndex = start,
EndIndex = end,
Status = "Pending"
};
PendingTasks.Enqueue(task);
}
}
}