Exemple #1
0
        private void NetworkStatusStateMachine()
        {
            bool flag = false;

            switch (STATE_MACHINE_NETWORK)
            {
            case STATE_MACHINE_NETWORK_START:
                //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: BEGIN");
                serversAlive.Clear();

                for (int i = 0; i < Server.AllServers.Count; i++)
                {
                    if (Server.AllServers[i].ID == Server.My_Identification.ID)     //Avoid ping himself
                    {
                        continue;
                    }
                    try
                    {
                        ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[i].UID.AbsoluteUri + "MyRemoteObjectName");
                        Console.WriteLine("CHECK: {0}", Server.AllServers[i].UID.AbsoluteUri);

                        if (obj.isRoot() == true)
                        {
                            Root_id = i;     //ID of the current root node
                            STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_KEEP_ALIVE;
                            flag = true;
                            Console.WriteLine("ROOT is {0}", Server.AllServers[i].UID.AbsoluteUri);

                            object[] imageFromRoot = obj.getImage();
                            ServerService.SetTupleSpace(new TupleSpace((List <MyTuple>)imageFromRoot[0]));      //Novo tuplespace criado
                            ServerService.SetCommunicationLayer((Queue)imageFromRoot[1], (List <Command>)imageFromRoot[2]);


                            //ServerService.SetTupleSpace( obj.getImage() ); //get the image of the root
                            Console.WriteLine("Imagem: ");
                            Console.WriteLine(ServerService.GetTupleSpaceRepresentation());
                            break;
                        }
                        else
                        {
                            serversAlive.Add(i);     //Server ID
                            Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri);
                        }
                    }
                    catch (Exception e)
                    {
                        //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION");
                        Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri);
                        //Console.WriteLine(e);
                    }
                }
                //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: TEST_FLAG");
                if (flag == false)
                {
                    //Console.WriteLine("FLAG==FALSE: {0}", serversAlive.Count);
                    if (serversAlive.Count == 0)     //check if anyone is ROOT
                    {
                        //ROOT
                        ServerService.setRoot(true);
                        STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT;
                    }
                    else
                    {
                        //Console.WriteLine("MY_IDENTIFICATION: {0}, {1}", Server.My_Identification.ID, (int)serversAlive[0]);
                        if ((Server.My_Identification.ID - 1) < (int)serversAlive[0])
                        {
                            //ROOT
                            ServerService.setRoot(true);
                            STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT;
                        }
                    }
                }
                break;

            case STATE_MACHINE_NETWORK_KEEP_ALIVE:
                //Console.WriteLine("IN STATE_MACHINE_NETWORK_KEEP_ALIVE");
                //ping root
                try
                {
                    ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[Root_id].UID.AbsoluteUri + "MyRemoteObjectName");
                    obj.Ping();
                    //Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri);
                    Thread.Sleep(rand.Next(1000));     //keepalive
                }
                catch (Exception e)
                {
                    STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_START;

                    //Console.WriteLine("IN STATE_MACHINE_NETWORK_KEEP_ALIVE: EXCEPTION");

                    //Console.WriteLine("GOING TO STATE_MACHINE_NETWORK_START");
                    //Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri);
                    //Console.WriteLine(e);
                }
                break;

            case STATE_MACHINE_NETWORK_IM_ROOT:
                Console.WriteLine("I'M THE ROOT SERVER");
                STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_END;
                break;

            case STATE_MACHINE_NETWORK_END:
                break;
            }
        }
Exemple #2
0
        public static void CheckCommandsInQueue_thread()
        {
            while (true)
            {
                while (MustFreeze == true)
                {
                    ;                             //FREEZE ****************************
                }
                Thread.Sleep(50);                 //Min time to check commands

                if (commLayer.GetQueueSize() > 0) //if there is commands
                {
                    Command cmd     = commLayer.RemoveFromCommandQueue();
                    MyTuple payload = (MyTuple)cmd.GetPayload();
                    Object  tmp;



                    switch (cmd.GetCommand())
                    {
                    case "read":
                        tmp = ts.Read(payload);
                        MyTuple a = tmp as MyTuple;

                        Console.WriteLine("Imagem: ");
                        Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                        if (a == null)     //object does not exist in the tuple space so we put in backlog
                        {
                            commLayer.InsertInBackLog(cmd);
                            Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                        }
                        else
                        {
                            GiveBackResult(cmd.GetUriFromSender(), a);
                        }
                        break;

                    case "add":
                        ts.Add(payload);
                        //Console.WriteLine(" ================= UMA VEZ ================ ");

                        Console.WriteLine("Imagem: ");
                        Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                        GiveBackResult(cmd.GetUriFromSender(), null);
                        MyTuple a1;
                        //serach in the backlog
                        for (int i = 0; i < commLayer.GetBackLogSize(); ++i)
                        {
                            Command Command_tmp = commLayer.GetBackLogCommand(i);
                            if (Command_tmp.GetCommand().Equals("read"))
                            {
                                tmp = ts.Read((MyTuple)Command_tmp.GetPayload());
                                a1  = tmp as MyTuple;
                                if (a1 != null)
                                {
                                    commLayer.RemoveFromBackLog(i);
                                    i = -1;
                                    Console.WriteLine("(ServerService) Comando Atendido e Removido do Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                                    GiveBackResult(Command_tmp.GetUriFromSender(), a1);
                                }
                            }
                            else
                            {
                                if (Command_tmp.GetCommand().Equals("take"))
                                {
                                    tmp = ts.Take((MyTuple)Command_tmp.GetPayload());
                                    a1  = tmp as MyTuple;
                                    if (a1 != null)     //test if is a MyTuple
                                    {
                                        commLayer.RemoveFromBackLog(i);
                                        i = -1;
                                        GiveBackResult(Command_tmp.GetUriFromSender(), a1);
                                    }
                                }
                            }
                        }
                        break;

                    case "take":
                        tmp = ts.Take(payload);
                        MyTuple a2 = tmp as MyTuple;
                        if (a2 == null)     //object does not exist in the tuple space so we put in backlog
                        {
                            commLayer.InsertInBackLog(cmd);
                            Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                        }
                        else
                        {
                            Console.WriteLine("Imagem: ");
                            Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                            GiveBackResult(cmd.GetUriFromSender(), a2);
                        }
                        break;
                    }
                }
            }
        }