Exemplo n.º 1
0
        public void stopReplicas(string name, string time)
        {
            OperatorInterface remoteReplica = connectToOP(name);

            remoteReplica.checkCommand("Interval" + " " + time);
        }
Exemplo n.º 2
0
        public void CommandReceived(string command)
        {
            string[] words = command.Split(' ');
            int      count = 0;

            if (words[0] == "Wait")
            {
                var date = DateTime.Now;
                addMsgToLog(date.Hour + "h:" + date.Minute + "m:" + date.Second + "s" + ": " + "PuppetMaster will wait " + words[1] + " ms");
                Thread.Sleep(Int32.Parse(words[1]));
                date = DateTime.Now;
                addMsgToLog(date.Hour + "h:" + date.Minute + "m:" + date.Second + "s" + ": " + "PuppetMaster waited " + words[1] + " ms!");
            }
            if ((words[0] == "Status"))
            {
                foreach (var i in pcs)
                {
                    if (!crashList.Contains(i.name))
                    {
                        OperatorInterface l = connectToOP(i.name);
                        l.checkCommand(words[0]);
                    }
                }
            }
            foreach (ConfigFileLine op in primaryReplicas)
            {
                if (words[0] == "Crash")
                {
                    try
                    {
                        if (words[2] == "0")
                        {
                            OperatorInterface remoteOP = connectToOP(words[1]);
                            connectedOperators.Remove(words[1]);
                            if (op.name == words[1])
                            {
                                primaryReplicas.Remove(op);
                            }
                            crashList.Add(op.name);
                            remoteOP.ProcessCrash();
                            break;
                        }
                        if (words[2] != "0")
                        {
                            List <ConfigFileLine> lista = new List <ConfigFileLine>();
                            bool hasValue = opReplicas.TryGetValue(words[1], out lista);
                            if (hasValue)
                            {
                                for (int i = 0; i < lista.Count; i++)
                                {
                                    if (Int32.Parse(words[2]) - 1 == i)
                                    {
                                        OperatorInterface remoteOP = connectToOP(lista[i].name);
                                        connectedOperators.Remove(lista[i].name);
                                        crashList.Add(lista[i].name);
                                        remoteOP.ProcessCrash();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
                if (words[0] == "Freeze")
                {
                    if (words[2] == "0")
                    {
                        OperatorInterface remoteOP = connectToOP(words[1]);
                        remoteOP.ProcessFreeze();
                        break;
                    }
                    if (words[2] != "0")
                    {
                        List <ConfigFileLine> lista = new List <ConfigFileLine>();
                        bool hasValue = opReplicas.TryGetValue(words[1], out lista);
                        if (hasValue)
                        {
                            for (int i = 0; i < lista.Count; i++)
                            {
                                if (Int32.Parse(words[2]) - 1 == i)
                                {
                                    OperatorInterface remoteOP = connectToOP(lista[i].name);
                                    remoteOP.ProcessFreeze();
                                    break;
                                }
                            }
                        }

                        break;
                    }
                }
                if (words[0] == "Unfreeze")
                {
                    if (words[2] == "0")
                    {
                        OperatorInterface remoteOP = connectToOP(words[1]);
                        remoteOP.ProcessUnreeze();
                        try
                        {
                            remoteOP.ProcessList();
                        }
                        catch (Exception e) //util para quando a lista do outro lado esta vazia
                        {}
                    }
                    if (words[2] != "0")
                    {
                        List <ConfigFileLine> lista = new List <ConfigFileLine>();
                        bool hasValue = opReplicas.TryGetValue(words[1], out lista);
                        if (hasValue)
                        {
                            for (int i = 0; i < lista.Count; i++)
                            {
                                if (Int32.Parse(words[2]) - 1 == i)
                                {
                                    OperatorInterface remoteOP = connectToOP(lista[i].name);
                                    remoteOP.ProcessUnreeze();

                                    try
                                    {
                                        remoteOP.ProcessList();
                                    }
                                    catch (Exception e)
                                    {}
                                }
                            }
                        }
                    }

                    break;
                }
                if ((words[0] == "Start"))
                {
                    if (op.name == words[1] && (op.input_ops == previousCommand || op.input_ops.Contains(".dat"))) //previousCommand verifica que o start so funciona depois do start do op anterior
                    {
                        if (op.routing == "random")
                        {
                            List <ConfigFileLine> value;
                            bool hasValue = opReplicas.TryGetValue(op.name, out value);
                            if (hasValue)
                            {
                                int number = rnd.Next(0, value.Count + 1);
                                if (number == 0)
                                {
                                    OperatorInterface remoteOperator = connectToOP(op.name);
                                    remoteOperator.checkCommand(words[0]);
                                    previousCommand = words[1];
                                    break;
                                }
                                else
                                {
                                    for (int i = 0; i < value.Count; i++)
                                    {
                                        if (number - 1 == i)
                                        {
                                            OperatorInterface remoteOperator = connectToOP(value[i].name);
                                            remoteOperator.checkCommand(words[0]);
                                            previousCommand = words[1];
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (op.routing == "primary")
                        {
                            OperatorInterface remoteOperator = connectToOP(op.name);
                            remoteOperator.checkCommand(words[0]);
                            previousCommand = words[1];
                            break;
                        }
                        if (op.routing.Contains("hashing"))
                        {
                            List <ConfigFileLine> value;
                            OperatorInterface     remoteOperator = connectToOP(op.name);
                            remoteOperator.checkCommand(words[0]);
                            bool hasValue = opReplicas.TryGetValue(op.name, out value);
                            if (hasValue)
                            {
                                foreach (var element in value)
                                {
                                    remoteOperator = connectToOP(element.name);
                                    remoteOperator.checkCommand(words[0]);
                                    previousCommand = words[1];
                                }
                            }
                        }
                    }
                    else
                    {
                        count++;
                        if (count == primaryReplicas.Count)
                        {
                            addMsgToLog("wrong operator");
                        }
                    }
                }

                if (words[0] == "Interval" && op.name == words[1]) //THREAD AQUI
                {
                    //timeToStop = words[2];
                    List <ConfigFileLine> value;

                    bool hasValue = opReplicas.TryGetValue(words[1], out value);
                    if (hasValue)
                    {
                        foreach (var element in value)
                        {
                            Task.Factory.StartNew(() => stopReplicas(element.name, words[2]));
                        }
                    }
                    OperatorInterface remoteOperator = connectToOP(op.name);
                    remoteOperator.checkCommand(words[0] + " " + words[2]);
                }
            }
        }