This repository was archived by the owner on Aug 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringCommandParser.cs
More file actions
188 lines (165 loc) · 5.64 KB
/
Copy pathStringCommandParser.cs
File metadata and controls
188 lines (165 loc) · 5.64 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using InteractiveConsole.Commands;
using TaskFlux.Core;
using TaskFlux.Domain;
using TaskFlux.PriorityQueue;
using TaskFlux.Transport.Tcp.Client;
namespace InteractiveConsole;
public static class StringCommandParser
{
private delegate UserCommand CommandFactory(string[] args);
private static readonly Dictionary<string, CommandFactory> CommandToFactory = new()
{
{ "create", GetCreateQueueCommand },
{ "delete", GetDeleteQueueCommand },
{ "enqueue", GetEnqueueCommand },
{ "dequeue", GetDequeueCommand },
{ "count", GetCountCommand },
{ "list", GetListQueuesCommand },
};
public static UserCommand ParseCommand(string input)
{
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (args.Length == 0)
{
throw new ArgumentException("Команда пуста");
}
try
{
var userCommand = CommandToFactory[args[0].ToLower()](args);
userCommand = new ErrorPrinterUserCommandDecorator(userCommand);
return userCommand;
}
catch (KeyNotFoundException)
{
throw new KeyNotFoundException($"Неизвестная команда: {args[0]}");
}
}
private static UserCommand GetCreateQueueCommand(string[] args)
{
var queueName = QueueNameParser.Parse(args[1]);
int? maxQueueSize = null;
int? maxPayloadSize = null;
(long, long)? priorityRange = null;
PriorityQueueCode? queueCode = null;
for (var i = 2; i < args.Length; i++)
{
switch (args[i])
{
case "WITHMAXSIZE":
maxQueueSize = int.Parse(args[i + 1]);
i++;
break;
case "WITHMAXPAYLOAD":
maxPayloadSize = int.Parse(args[i + 1]);
i++;
break;
case "WITHPRIORITYRANGE":
priorityRange = (long.Parse(args[i + 1]), long.Parse(args[i + 2]));
i += 2;
break;
case "TYPE":
switch (args[i + 1].ToLower())
{
case "h4":
queueCode = PriorityQueueCode.Heap4Arity;
break;
case "qa":
queueCode = PriorityQueueCode.QueueArray;
break;
default:
throw new Exception($"Неизвестный код структуры очереди: {args[i + 1]}");
}
i++;
break;
default:
throw new Exception($"Неизвестный аргумент: {args[i]}");
}
}
var options = new CreateQueueOptions();
switch (queueCode)
{
case null:
break;
case PriorityQueueCode.Heap4Arity:
options = options.UseHeap();
break;
case PriorityQueueCode.QueueArray:
if (priorityRange is var (min, max))
{
options = options.UseQueueArray(min, max);
priorityRange = null; // Чтобы дальше не использовали
}
else
{
throw new Exception("Для реализации QueueArray необходимо указать диапазон допустимых ключей");
}
break;
}
if (priorityRange is var (minKey, maxKey))
{
options = options.WithPriorityRange(minKey, maxKey);
}
if (maxQueueSize is { } mqs)
{
options = options.WithMaxQueueSize(mqs);
}
if (maxPayloadSize is { } mps)
{
options = options.WithMaxMessageSize(mps);
}
return new CreateQueueUserCommand(queueName, options);
}
private static DeleteQueueUserCommand GetDeleteQueueCommand(string[] args)
{
var queueName = QueueNameParser.Parse(args[1]);
return new DeleteQueueUserCommand(queueName);
}
private static EnqueueUserCommand GetEnqueueCommand(string[] args)
{
QueueName queueName;
string[] data;
if (long.TryParse(args[1], out var key))
{
queueName = QueueName.Default;
data = args[2..];
}
else
{
queueName = QueueNameParser.Parse(args[1]);
key = long.Parse(args[2]);
data = args[3..];
}
var payload = PayloadHelpers.Serialize(string.Join(' ', data));
return new EnqueueUserCommand(queueName, key, payload);
}
private static DequeueUserCommand GetDequeueCommand(string[] args)
{
QueueName queueName;
try
{
queueName = QueueNameParser.Parse(args[1]);
}
catch (IndexOutOfRangeException)
{
queueName = QueueName.Default;
}
return new DequeueUserCommand(queueName);
}
private static GetCountUserCommand GetCountCommand(string[] args)
{
QueueName queueName;
try
{
queueName = QueueNameParser.Parse(args[1]);
}
catch (IndexOutOfRangeException)
{
queueName = QueueName.Default;
}
return new GetCountUserCommand(queueName);
}
private static ListQueuesUserCommand GetListQueuesCommand(string[] args)
{
return new ListQueuesUserCommand();
}
}