Пример #1
0
        static void Main(string[] args)
        {
            if (!File.Exists("portas.txt"))
            {
                var stream = File.Create("portas.txt");
                stream.Close();
                StreamWriter arq = new StreamWriter("portas.txt");
                arq.WriteLine("1500");
                arq.Close();
            }

            StreamReader file = new StreamReader("portas.txt");

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            porta = int.Parse(file.ReadLine());

            IPAddress  server = IPAddress.Parse("127.0.0.1");
            IPEndPoint ep     = new IPEndPoint(server, porta);

            String  data;
            int     comand;
            int     chave;
            Comando comando;

            byte[] sendbuf = new byte[1450];

            Task threadReceive;

            while (true)
            {
                Console.WriteLine("Qual comando(ADD-1/READ-2/UPDATE-3/DELETE-4/LISTAR-5/DESLIGAR-6/SNAPSHOT-8)");
                if (!int.TryParse(Console.ReadLine(), out comand) || comand <= 0 || comand > 8)
                {
                    Console.WriteLine("Comando Inválido!");
                    continue;
                }

                switch (comand)
                {
                case (int)Comandos.ADD:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }

                    Console.WriteLine("Insira dado");
                    data = Console.ReadLine();

                    comando = new Comando(Comandos.ADD, chave, data);
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    if (sendbuf.Length > 1450)
                    {
                        Console.WriteLine("Tamanho de string inválida!");
                        continue;
                    }
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.READ:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }

                    comando = new Comando(Comandos.READ, chave, "a");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.UPDATE:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }
                    Console.WriteLine("Insira dado");
                    data    = Console.ReadLine();
                    comando = new Comando(Comandos.UPDATE, chave, data);
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    if (sendbuf.Length > 1450)
                    {
                        Console.WriteLine("Tamanho de string inválida!");
                        continue;
                    }
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.DELETE:
                    Console.WriteLine("Insira chave");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Chave inválida");
                        continue;
                    }
                    comando = new Comando(Comandos.DELETE, chave, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.DESLIGAR:
                    comando = new Comando(Comandos.DESLIGAR, 0, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.LISTAR:
                    comando = new Comando(Comandos.LISTAR, 0, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;

                case (int)Comandos.SNAPSHOT:
                    Console.WriteLine("Insira o tempo(segundos): ");
                    if (!int.TryParse(Console.ReadLine(), out chave))
                    {
                        Console.WriteLine("Tempo inválido");
                        continue;
                    }
                    comando = new Comando(Comandos.SNAPSHOT, chave, "c");
                    sendbuf = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(comando));
                    s.SendTo(sendbuf, ep);
                    break;
                }
                lock (threadBlock)
                {
                    if (!threadOn)
                    {
                        threadOn      = true;
                        threadReceive = new Task(() => ThreadReceive(s));
                        threadReceive.Start();
                    }
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            if (!File.Exists("Offset.txt"))
            {
                var stream = File.Create("Offset.txt");
                stream.Close();
                StreamWriter arq = new StreamWriter("Offset.txt");
                arq.WriteLine("0");
                arq.Close();
            }
            StreamReader file2 = new StreamReader("Offset.txt");

            offint = int.Parse(file2.ReadLine());
            file2.Close();

            ////////////////////////////////////////////////////////////////////////////////

            string topic   = "ComandoTopic4";
            Uri    uri     = new Uri("http://localhost:9092");
            var    options = new KafkaOptions(uri);
            var    router  = new BrokerRouter(options);
            var    client  = new Producer(router);

            /////////////////////////////////////////////////////////////////////////////////

            String  data;
            int     comand;
            int     chave;
            Comando comando;
            Message msg2;

            Task threadReceive;

            threadReceive = new Task(ThreadReceive);
            threadReceive.Start();

            while (true)
            {
                if (!Receive)
                {
                    Console.WriteLine("\nQual comando(ADD-1/READ-2/UPDATE-3/DELETE-4/LISTAR-5/DESLIGAR-6/SNAPSHOT-8)");
                    if (!int.TryParse(Console.ReadLine(), out comand) || comand <= 0 || comand > 8)
                    {
                        Console.WriteLine("Comando Inválido!");
                        continue;
                    }

                    switch (comand)
                    {
                    case (int)Comandos.ADD:

                        Console.WriteLine("Insira chave");
                        if (!int.TryParse(Console.ReadLine(), out chave))
                        {
                            Console.WriteLine("Chave inválida");
                            continue;
                        }

                        Console.WriteLine("Insira dado");
                        data = Console.ReadLine();

                        comando = new Comando(Comandos.ADD, chave, data);
                        msg2    = new Message(JsonConvert.SerializeObject(comando));

                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;

                    case (int)Comandos.READ:

                        Console.WriteLine("Insira chave");
                        if (!int.TryParse(Console.ReadLine(), out chave))
                        {
                            Console.WriteLine("Chave inválida");
                            continue;
                        }

                        comando = new Comando(Comandos.READ, chave, "a");
                        msg2    = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;

                    case (int)Comandos.UPDATE:

                        Console.WriteLine("Insira chave");
                        if (!int.TryParse(Console.ReadLine(), out chave))
                        {
                            Console.WriteLine("Chave inválida");
                            continue;
                        }
                        Console.WriteLine("Insira dado");
                        data    = Console.ReadLine();
                        comando = new Comando(Comandos.UPDATE, chave, data);

                        msg2 = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;

                    case (int)Comandos.DELETE:

                        Console.WriteLine("Insira chave");
                        if (!int.TryParse(Console.ReadLine(), out chave))
                        {
                            Console.WriteLine("Chave inválida");
                            continue;
                        }
                        comando = new Comando(Comandos.DELETE, chave, "c");
                        msg2    = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;

                    case (int)Comandos.DESLIGAR:

                        comando = new Comando(Comandos.DESLIGAR, 0, "c");
                        msg2    = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        }).Wait();
                        Receive = true;

                        break;

                    case (int)Comandos.LISTAR:

                        comando = new Comando(Comandos.LISTAR, 0, "c");
                        msg2    = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;

                    case (int)Comandos.SNAPSHOT:

                        Console.WriteLine("Insira o tempo(segundos): ");
                        if (!int.TryParse(Console.ReadLine(), out chave))
                        {
                            Console.WriteLine("Tempo inválido");
                            continue;
                        }
                        comando = new Comando(Comandos.SNAPSHOT, chave, "c");
                        msg2    = new Message(JsonConvert.SerializeObject(comando));
                        client.SendMessageAsync(topic, new List <Message> {
                            msg2
                        });
                        Receive = true;

                        break;
                    }
                }
            }
        }