|
| 1 | +using System; |
| 2 | + |
| 3 | + |
| 4 | +internal class ConsoleInputHandler |
| 5 | +{ |
| 6 | + public readonly ref struct ConfigInputs |
| 7 | + { |
| 8 | + public readonly bool runAsServer; |
| 9 | + public readonly bool isFixedMessage; |
| 10 | + public readonly int fixedMessageSize; |
| 11 | + public readonly bool runAsClient; |
| 12 | + public readonly int numClients; |
| 13 | + public readonly int numMessages; |
| 14 | + public readonly int messageSize; |
| 15 | + |
| 16 | + public ConfigInputs(bool runAsServer, bool runAsClient, bool isFixedMessage, int fixedMessageSize, int numClients, int numMessages, int messageSize) |
| 17 | + { |
| 18 | + this.runAsServer = runAsServer; |
| 19 | + this.runAsClient = runAsClient; |
| 20 | + this.isFixedMessage = isFixedMessage; |
| 21 | + this.fixedMessageSize = fixedMessageSize; |
| 22 | + this.numClients = numClients; |
| 23 | + this.numMessages = numMessages; |
| 24 | + this.messageSize = messageSize; |
| 25 | + } |
| 26 | + } |
| 27 | + enum InputState |
| 28 | + { |
| 29 | + ObtainModeServer, |
| 30 | + ServerStaticResponse, |
| 31 | + ServerStaticResponseSize, |
| 32 | + ObtainModeClient, |
| 33 | + NumClients, |
| 34 | + NumMessages, |
| 35 | + MessageSize, |
| 36 | + Done |
| 37 | + } |
| 38 | + static bool runAsServer; |
| 39 | + static bool isFixedMessage; |
| 40 | + static int fixedMessageSize; |
| 41 | + |
| 42 | + static bool runAsClient; |
| 43 | + static int numClients; |
| 44 | + static int numMessages; |
| 45 | + static int messageSize; |
| 46 | + private static InputState inputState = InputState.ObtainModeServer; |
| 47 | + |
| 48 | + public static ConfigInputs ObtainConfig() |
| 49 | + { |
| 50 | + |
| 51 | + Console.WriteLine("[Hint] You can enter a command for quick configure"); |
| 52 | + Console.WriteLine("'xx' Server Client on same application"); |
| 53 | + Console.WriteLine("'s0' is Server with fixed response"); |
| 54 | + Console.WriteLine("'s1' is Server with dynamic echo response"); |
| 55 | + Console.WriteLine("'c0' is 100 Client, 1000 Message, 32 byte payload"); |
| 56 | + Console.WriteLine(); |
| 57 | + |
| 58 | + string input = ""; |
| 59 | + bool isYes() => input.Equals("y", StringComparison.OrdinalIgnoreCase); |
| 60 | + bool isNo() => input.Equals("n", StringComparison.OrdinalIgnoreCase); |
| 61 | + |
| 62 | + while (true) |
| 63 | + { |
| 64 | + switch (inputState) |
| 65 | + { |
| 66 | + case InputState.ObtainModeServer: |
| 67 | + |
| 68 | + Console.WriteLine("-> Run As Echo Server? <y/n>?"); |
| 69 | + input = Console.ReadLine() ?? ""; |
| 70 | + if (CheckQuickConfig(input)) |
| 71 | + continue; |
| 72 | + |
| 73 | + if (!isYes() && !isNo()) |
| 74 | + continue; |
| 75 | + if (isYes()) |
| 76 | + { |
| 77 | + runAsServer = true; |
| 78 | + inputState = InputState.ServerStaticResponse; |
| 79 | + } |
| 80 | + else |
| 81 | + inputState = InputState.ObtainModeClient; |
| 82 | + |
| 83 | + continue; |
| 84 | + |
| 85 | + case InputState.ServerStaticResponse: |
| 86 | + Console.WriteLine("-> Respond Fixed Static Message? <y/n>?"); |
| 87 | + input = Console.ReadLine() ?? ""; |
| 88 | + if (!isYes() && !isNo()) |
| 89 | + continue; |
| 90 | + if (isYes()) |
| 91 | + { |
| 92 | + isFixedMessage = isYes(); |
| 93 | + inputState = InputState.ServerStaticResponseSize; |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + inputState = InputState.ObtainModeClient; |
| 98 | + Console.WriteLine("Server will reply dynamic echo messages same size as incoming"); |
| 99 | + } |
| 100 | + continue; |
| 101 | + |
| 102 | + case InputState.ServerStaticResponseSize: |
| 103 | + Console.WriteLine("-> Enter Fixed Response Message Size In Bytes:"); |
| 104 | + input = Console.ReadLine() ?? ""; |
| 105 | + if (int.TryParse(input, out var byteSize)) |
| 106 | + { |
| 107 | + fixedMessageSize = byteSize; |
| 108 | + inputState = InputState.ObtainModeClient; |
| 109 | + } |
| 110 | + else |
| 111 | + Console.WriteLine("Enter a valid number"); |
| 112 | + continue; |
| 113 | + |
| 114 | + case InputState.ObtainModeClient: |
| 115 | + |
| 116 | + Console.WriteLine("-> Run As Client <y/n>?"); |
| 117 | + input = Console.ReadLine() ?? ""; |
| 118 | + |
| 119 | + if (!isYes() && !isNo()) |
| 120 | + continue; |
| 121 | + |
| 122 | + runAsClient = isYes(); |
| 123 | + if (!runAsClient && !runAsServer) |
| 124 | + inputState = InputState.ObtainModeServer; |
| 125 | + else if (runAsClient) |
| 126 | + inputState = InputState.NumClients; |
| 127 | + else |
| 128 | + inputState = InputState.Done; ; |
| 129 | + |
| 130 | + continue; |
| 131 | + |
| 132 | + case InputState.NumClients: |
| 133 | + Console.WriteLine("-> Enter Number of Clients:"); |
| 134 | + input = Console.ReadLine() ?? ""; |
| 135 | + if (ushort.TryParse(input, out var num)) |
| 136 | + { |
| 137 | + numClients = num; |
| 138 | + inputState = InputState.NumMessages; |
| 139 | + } |
| 140 | + else |
| 141 | + Console.WriteLine("Enter a valid number"); |
| 142 | + continue; |
| 143 | + |
| 144 | + |
| 145 | + case InputState.NumMessages: |
| 146 | + Console.WriteLine("-> Enter Number of Messages:"); |
| 147 | + input = Console.ReadLine() ?? ""; |
| 148 | + if (int.TryParse(input, out var nummsg)) |
| 149 | + { |
| 150 | + numMessages = nummsg; |
| 151 | + inputState = InputState.MessageSize; |
| 152 | + } |
| 153 | + else |
| 154 | + Console.WriteLine("Enter a valid number"); |
| 155 | + continue; |
| 156 | + |
| 157 | + case InputState.MessageSize: |
| 158 | + Console.WriteLine("-> Enter Message Size In Bytes:"); |
| 159 | + input = Console.ReadLine() ?? ""; |
| 160 | + if (int.TryParse(input, out var byteSizefm)) |
| 161 | + { |
| 162 | + messageSize = byteSizefm; |
| 163 | + inputState = InputState.Done; |
| 164 | + } |
| 165 | + else |
| 166 | + Console.WriteLine("Enter a valid number"); |
| 167 | + continue; |
| 168 | + |
| 169 | + |
| 170 | + case InputState.Done: |
| 171 | + Console.WriteLine("Configuration Complete\n"); |
| 172 | + string validate = ""; |
| 173 | + if (runAsServer) |
| 174 | + { |
| 175 | + validate += "Running as Server" + "\n"; |
| 176 | + validate += isFixedMessage ? "Server will Reply Fixed Message with size:" + fixedMessageSize : |
| 177 | + "Server will Reply Dynamic Echo Messages" + "\n"; |
| 178 | + } |
| 179 | + if (runAsClient) |
| 180 | + { |
| 181 | + validate += "\nRunning as Client" + "\n"; |
| 182 | + validate += "Number of Echo Clients: " + numClients.ToString() + "\n"; |
| 183 | + validate += "Number of Messages: " + numMessages.ToString() + "\n"; |
| 184 | + validate += "Message size in bytes: " + messageSize.ToString(); |
| 185 | + |
| 186 | + } |
| 187 | + Console.WriteLine(validate); |
| 188 | + Console.WriteLine("\n-> Confirm <y/n>?"); |
| 189 | + input = Console.ReadLine() ?? ""; |
| 190 | + |
| 191 | + if (!isYes() && !isNo()) |
| 192 | + continue; |
| 193 | + if (isYes()) |
| 194 | + break; |
| 195 | + else |
| 196 | + Reset(); |
| 197 | + continue; |
| 198 | + } |
| 199 | + break; |
| 200 | + |
| 201 | + } |
| 202 | + return new ConfigInputs(runAsServer: runAsServer, |
| 203 | + runAsClient: runAsClient, |
| 204 | + isFixedMessage: isFixedMessage, |
| 205 | + fixedMessageSize: fixedMessageSize, |
| 206 | + numClients: numClients, |
| 207 | + numMessages: numMessages, |
| 208 | + messageSize: messageSize); |
| 209 | + |
| 210 | + void Reset() |
| 211 | + { |
| 212 | + runAsServer = false; |
| 213 | + runAsClient = false; |
| 214 | + isFixedMessage = false; |
| 215 | + fixedMessageSize = 0; |
| 216 | + numClients = 0; |
| 217 | + numMessages = 0; |
| 218 | + messageSize = 0; |
| 219 | + inputState = InputState.ObtainModeServer; |
| 220 | + } |
| 221 | + |
| 222 | + } |
| 223 | + |
| 224 | + private static bool CheckQuickConfig(string input) |
| 225 | + { |
| 226 | + if (input == "xx") |
| 227 | + { |
| 228 | + runAsServer = true; |
| 229 | + runAsClient = true; |
| 230 | + isFixedMessage = true; |
| 231 | + fixedMessageSize = 32; |
| 232 | + numClients = 100; |
| 233 | + numMessages = 1000; |
| 234 | + messageSize = 32; |
| 235 | + inputState = InputState.Done; |
| 236 | + return true; |
| 237 | + } |
| 238 | + if (input == "s0") |
| 239 | + { |
| 240 | + runAsServer = true; |
| 241 | + runAsClient = false; |
| 242 | + isFixedMessage = true; |
| 243 | + fixedMessageSize = 32; |
| 244 | + inputState = InputState.Done; |
| 245 | + return true; |
| 246 | + } |
| 247 | + if (input == "s1") |
| 248 | + { |
| 249 | + runAsServer = true; |
| 250 | + runAsClient = false; |
| 251 | + isFixedMessage = false; |
| 252 | + inputState = InputState.Done; |
| 253 | + return true; |
| 254 | + } |
| 255 | + if (input == "c0") |
| 256 | + { |
| 257 | + runAsServer = false; |
| 258 | + runAsClient = true; |
| 259 | + isFixedMessage = false; |
| 260 | + numClients = 100; |
| 261 | + numMessages = 1000; |
| 262 | + messageSize = 32; |
| 263 | + inputState = InputState.Done; |
| 264 | + return true; |
| 265 | + } |
| 266 | + return false; |
| 267 | + } |
| 268 | + |
| 269 | + |
| 270 | +} |
0 commit comments