Exemplo n.º 1
0
        public void connectToReplicas()
        {
            int i = 1;

            while (i < Int32.Parse(previousRepFact))
            {
                int connectedPort2 = 8080 + Int32.Parse((input_ops[2]).ToString()) + i * 100;
                Console.WriteLine("Im going to connect to port: " + connectedPort2);
                OperatorInterface replicaOp = (OperatorInterface)Activator.GetObject(
                    typeof(OperatorInterface),
                    "tcp://localhost:" + connectedPort2 + "/op");
                connectedReplicas.Add(replicaOp);
                i++;
            }
        }
Exemplo n.º 2
0
 public void PreviousOpInterfaceConnection()
 {
     //---------------------------------Next OP connection------------------------------------------------------//
     if (input_ops.Contains("OP"))
     {
         connectedPort = 8080 + Int32.Parse((input_ops[2]).ToString());
         Console.WriteLine("Im going to connect to port: " + connectedPort);
         previousOp = (OperatorInterface)Activator.GetObject(
             typeof(OperatorInterface),
             "tcp://localhost:" + connectedPort + "/op");
         connectedReplicas.Add(previousOp);
         if (previousRepFact != "only_one")
         {
             ThreadStart td  = new ThreadStart(connectToReplicas);
             Thread      t_d = new Thread(td);
             t_d.Start();
         }
     }
 }
Exemplo n.º 3
0
        public OperatorInterface connectToOP(String name)
        {
            int port;

            if (name.Contains('R'))
            {
                port = 8080 + Int32.Parse((name[2]).ToString()) + Int32.Parse(name[name.Length - 1].ToString()) * 100;
            }
            else
            {
                port = 8080 + Int32.Parse((name[2]).ToString());
            }
            OperatorInterface remoteOperator = (OperatorInterface)Activator.GetObject(typeof(OperatorInterface), "tcp://localhost:" + port + "/op");

            if (!connectedOperators.Keys.Contains(name))
            {
                connectedOperators.Add(name, remoteOperator);
            }
            return(remoteOperator);
        }
Exemplo n.º 4
0
        public void stopReplicas(string name, string time)
        {
            OperatorInterface remoteReplica = connectToOP(name);

            remoteReplica.checkCommand("Interval" + " " + time);
        }
Exemplo n.º 5
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]);
                }
            }
        }