/// <summary> /// Thread principal do servidor, cria as outras threads e é responsável por receber os comandos e os escrever em filaComandos. /// </summary> /// <param name="args"></param> static void Main(string[] args) { //Socket int receivedDataLength; byte[] data = new byte[1400]; if (!File.Exists("portas.txt")) { var stream = File.Create("portas.txt"); stream.Close(); StreamWriter arq = new StreamWriter("portas.txt"); arq.WriteLine("1500"); arq.WriteLine("1600"); arq.WriteLine("1700"); arq.WriteLine("127.0.0.1"); arq.Close(); } StreamReader file = new StreamReader("portas.txt"); portaRecebe = int.Parse(file.ReadLine()); portaEnvia = int.Parse(file.ReadLine()); portaGRPC = int.Parse(file.ReadLine()); endereco = file.ReadLine(); IPEndPoint ip = new IPEndPoint(IPAddress.Parse(endereco), portaRecebe); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Bind(ip); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = (EndPoint)(sender); RecuperaMapa(); //Threads Task threadComandos = new Task(ThreadComandos); Task threadProcessaComando = new Task(ThreadProcessaComando); Task threadLogaDisco = new Task(ThreadLogaDisco); Task threadGRPC = new Task(ThreadGRPC); Task threadSnapshot = new Task(ThreadSnapshot); threadComandos.Start(); threadProcessaComando.Start(); threadLogaDisco.Start(); threadGRPC.Start(); threadSnapshot.Start(); while (!desliga) { data = new byte[1500]; receivedDataLength = socket.ReceiveFrom(data, ref Remote); string json = Encoding.ASCII.GetString(data, 0, receivedDataLength); Comando comando = JsonConvert.DeserializeObject <Comando>(json); Requisicao req = new Requisicao(Remote, comando); lock (blockComandos) { filaComandos.Enqueue(req); } } }
static public void ProcessaComando(Comando comando, ref string resposta) { switch (comando.comand) { case (int)Comandos.ADD: if (Mapa.ContainsKey(comando.Chave)) { resposta = "Não foi possível inserir o item, chave já existente."; } else { resposta = "Inserido com sucesso."; Mapa.Add(comando.Chave, comando.Valor); } break; case (int)Comandos.UPDATE: if (Mapa.ContainsKey(comando.Chave)) { resposta = "Atualizacao efetuada com sucesso."; Mapa[comando.Chave] = comando.Valor; } else { resposta = "Não foi possível atualizar, elemento inexistente."; } break; case (int)Comandos.READ: string data; if (!Mapa.TryGetValue(comando.Chave, out data)) { resposta = "Chave nao encontrada, elemento inexistente."; } else { resposta = data; } break; case (int)Comandos.DELETE: if (Mapa.ContainsKey(comando.Chave)) { Mapa.Remove(comando.Chave); resposta = "Remocao efetuada com sucesso."; } else { resposta = "Não foi possível remover, elemento inexistente."; } break; case (int)Comandos.DESLIGAR: desliga = true; break; case (int)Comandos.SNAPSHOT: lock (blockSegundos) { if (comando.Chave >= 10) { segundos = comando.Chave; resposta = "Tempo do snapshot atualizado com sucesso para o valor de: " + comando.Chave; } else { resposta = "Não é possível gravar o snapshot com tempo menor de 10 segundos."; } } break; } }