Exemple #1
0
        public IDIDATuple Add(IDIDATuple didatuple, bool fromXLBroadcast)
        {
            bool      ackFromServers = false;
            DIDATuple didaAdd        = null;

            if (!fromXLBroadcast)
            {
                //broadCast to all servers
                ackFromServers = BroadcastAdd((DIDATuple)didatuple);
            }

            if (ackFromServers || fromXLBroadcast)
            {
                didaAdd = (DIDATuple)didatuple;
                tupleSpace.Add(didaAdd);
                size++;
                Console.Write("Client added new tuple : ");
                this.PrintTuple(didaAdd);
            }
            else
            {
                Add(didatuple, false);
            }

            return(didaAdd);
        }
Exemple #2
0
 public IDIDATuple Read(IDIDATuple didatuple, string name, int port)
 {
     if (MaxDelay == -1)
     {
         this.SimulateDelay(didatuple);
     }
     return(Read(didatuple, name, port, false));
 }
Exemple #3
0
        public IDIDATuple Take(IDIDATuple didatuple, string name, int port)
        {
            if (MaxDelay == -1)
            {
                this.SimulateDelay(didatuple);
            }

            if (name == null)
            {
                numOps++;
            }
            this.RequestMostRecent();
            string    url = "tcp://localhost:" + port + "/" + name;
            DIDATuple didaTake;

            //lock (this)
            //{
            didaTake = (DIDATuple)didatuple;

            int count = 0;

            if (pending.Count == 0 && isfrozen == false)
            {
                foreach (DIDATuple didaTuple in tupleSpace)
                {
                    if (didaTuple.CompareParams(didaTake))
                    {
                        //remove element from tuple space
                        Console.Write("Client removing tuple  : ");

                        tupleSpace.RemoveAt(count);
                        this.PrintTuple(didaTake);
                        if (didaTake.getTakeme())
                        {
                            numOps++;
                            didaTake.setTakeme(false);
                            this.BroadcastTake(didaTake);
                        }
                        return(didatuple);
                    }

                    count++;
                }
            }
            else
            {
                Console.Write("Client removing tuple inserted to the queue  : ");
                this.PrintTuple(didaTake);

                pending.Enqueue(new KeyValuePair <string, DIDATuple>("take", didaTake));
            }
            //  }
            if (name != null)
            {
                queuedClients.Add(url, didaTake);
            }
            return(null);
        }
Exemple #4
0
        public IDIDATuple Add(IDIDATuple didatuple)
        {
            if (MaxDelay == -1)
            {
                this.SimulateDelay(didatuple);
            }

            return(Add(didatuple, false));
        }
Exemple #5
0
 public IDIDATuple Take(IDIDATuple didatuple, string name, int port)
 {
     if (MaxDelay == -1)
     {
         this.SimulateDelay(didatuple);
     }
     //broadCast to all servers
     return(BroadcastTake((DIDATuple)didatuple));
 }
Exemple #6
0
        public bool XLBroadcastRemove(IDIDATuple didaTuple)
        {
            bool result = tupleSpace.Remove((DIDATuple)didaTuple);

            if (result)
            {
                Console.Write("Client removed  tuple : ");
                this.PrintTuple((DIDATuple)didaTuple);
            }
            return(result);
        }
Exemple #7
0
        private IDIDATuple Add(IDIDATuple didatuple, bool fromUnfreeze)
        {
            DIDATuple didaAdd = (DIDATuple)didatuple;

            if ((pending.Count == 0 && isfrozen == false) || fromUnfreeze)
            {
                tupleSpace.Add(didaAdd);
                numOps++;
                size++;
                Console.Write("Client added new tuple : ");
                this.PrintTuple(didaAdd);

                //se tiver algum client a espera deste tuplo
                foreach (KeyValuePair <string, DIDATuple> elem in queuedClients)
                {
                    if (elem.Value.CompareParams(didaAdd))
                    {
                        notifyClient(elem.Key);
                    }
                }

                if (didaAdd.getBroadme())
                {
                    didaAdd.setBroadme(false);
                    this.BroadcastAdd(didaAdd);
                }

                return(didaAdd);
            }
            else
            {
                Console.Write("Client added new tuple inserted to the queue : ");
                this.PrintTuple(didaAdd);

                pending.Enqueue(new KeyValuePair <string, DIDATuple>("add", didaAdd));
                return(null);
            }
        }
Exemple #8
0
        private IDIDATuple Read(IDIDATuple didatuple, string name, int port, bool fromXLBroadcast)
        {
            //MULTICAST TO ALL SERVERS - TODO
            //
            lock (this)
            {
                DIDATuple didaResult = tupleSpace.Read((DIDATuple)didatuple);

                if (didaResult != null)
                {
                    return(didaResult);
                }
                else
                {
                    if (!fromXLBroadcast)
                    {
                        //broadCast to all servers
                        return(BroadcastRead((DIDATuple)didatuple));
                    }
                }
            }
            return(null);
        }
Exemple #9
0
        public IDIDATuple Read(IDIDATuple didatuple, string name, int port)
        {
            if (MaxDelay == -1)
            {
                this.SimulateDelay(didatuple);
            }
            this.RequestMostRecent();
            string    url = "tcp://localhost:" + port + "/" + name;
            DIDATuple didaRead;

            lock (this)
            {
                didaRead = (DIDATuple)didatuple;
                if (pending.Count == 0 && isfrozen == false)
                {
                    foreach (DIDATuple didaTuple in tupleSpace)
                    {
                        if (didaTuple.CompareParams(didaRead))
                        {
                            Console.Write("Client reading tuple   : ");
                            this.PrintTuple(didaRead);
                            return(didatuple);
                        }
                    }
                }
                else
                {
                    Console.Write("Client reading tuple  inserted to the queue : ");
                    this.PrintTuple(didaRead);

                    pending.Enqueue(new KeyValuePair <string, DIDATuple>("read", didaRead));
                }
            }
            queuedClients.Add(url, didaRead);
            return(null);
        }
Exemple #10
0
        public void SimulateDelay(IDIDATuple x)
        {
            DIDATuple didat       = (DIDATuple)x;
            string    name        = didat.getName();
            string    mypath      = System.Reflection.Assembly.GetEntryAssembly().Location;
            string    finalpath   = mypath.Substring(0, mypath.Length - 10);
            string    newpath     = Path.GetFullPath(Path.Combine(finalpath, @"..\..\"));
            string    pathToDelay = newpath + name + "Delay.txt";

            string[] lines = System.IO.File.ReadAllLines(pathToDelay);
            foreach (string line in lines)
            {
                //min : max
                string[] args = line.Split(':');

                MinDelay = Int32.Parse(args[0]);
                MaxDelay = Int32.Parse(args[1]);
            }

            Random r     = new Random();
            int    delay = r.Next(MinDelay, MaxDelay);

            this.Wait(delay);
        }
Exemple #11
0
 private IDIDATuple Add(IDIDATuple didatuple)
 {
     return(server.Add(didatuple));
 }
Exemple #12
0
 public bool XLBroadcastRemove(IDIDATuple didaTuple)
 {
     throw new NotImplementedException();
 }
Exemple #13
0
 public bool XLBroadcastAdd(IDIDATuple didaTuple)
 {
     return(Add(didaTuple, true) != null);
 }
Exemple #14
0
 private IDIDATuple Take(IDIDATuple didatuple, string name, int port)
 {
     return(server.Take(didatuple, name, port));
 }
Exemple #15
0
 private IDIDATuple Read(IDIDATuple didatuple, string name, int port)
 {
     return(server.Read(didatuple, name, port));
 }
Exemple #16
0
 public IDIDATuple XLBroadcastRead(IDIDATuple didaTuple)
 {
     throw new NotImplementedException();
 }
Exemple #17
0
 public List <IDIDATuple> XLBroadcastTake(IDIDATuple didaTuple)
 {
     throw new NotImplementedException();
 }
Exemple #18
0
        public IDIDATuple XLBroadcastRead(IDIDATuple didaTuple)
        {
            DIDATuple didaRead = (DIDATuple)didaTuple;

            return(Read(didaTuple, didaRead.getName(), didaRead.getPort(), true));
        }
Exemple #19
0
 public List <IDIDATuple> XLBroadcastTake(IDIDATuple didaTuple)
 {
     return(tupleSpace.Take((DIDATuple)didaTuple));
 }
Exemple #20
0
        public void Parser(List <string> command, string server_name, string server_port)
        {
            Console.WriteLine();
            Random r = new Random();

            switch (command[0].ToLower())
            {
            case "add":
                Console.Write("Adding new tuple : ");
                List <string> fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                DIDATuple didatuple = new DIDATuple(fieldsList);
                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));
                Add(didatuple);
                this.PrintTuple(didatuple);
                break;

            case "read":
                Console.Write("Reading tuple    : ");
                fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                didatuple = new DIDATuple(fieldsList);

                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));

                int    channel_port = r.Next(7000, 8000);
                string channel_name = "client" + channel_port;

                IDIDATuple didatuplefinal = (DIDATuple)Read(didatuple, channel_name, channel_port);
                this.PrintTuple(didatuple);

                if (didatuplefinal == null)
                {
                    Console.WriteLine("-> Unsuccesful search! Waiting..");

                    TcpServerChannel channel = new TcpServerChannel(channel_name, channel_port);
                    ChannelServices.RegisterChannel(channel, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(
                        typeof(Client), channel_name, WellKnownObjectMode.Singleton);

                    BlockAndWait();
                    Read(didatuple, channel_name, channel_port);
                    Console.WriteLine("-> Succesful Read!");
                    ChannelServices.UnregisterChannel(channel);
                }
                else
                {
                    Console.WriteLine("-> Succesful Read!");
                }
                break;

            case "take":
                Console.Write("Removing tuple   : ");
                fieldsList = command.ToList();
                fieldsList.RemoveAt(0);
                didatuple = new DIDATuple(fieldsList);

                didatuple.setName(server_name);
                didatuple.setPort(Int32.Parse(server_port));

                int    channel_port2 = r.Next(7000, 8000);
                string channel_name2 = "client" + channel_port2;

                IDIDATuple takeFromServer = (DIDATuple)Take(didatuple, channel_name2, channel_port2);
                this.PrintTuple(didatuple);

                if (takeFromServer == null)
                {
                    Console.WriteLine("-> Unsuccesful take! Waiting..");

                    TcpServerChannel channel2 = new TcpServerChannel(channel_name2, channel_port2);
                    ChannelServices.RegisterChannel(channel2, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(
                        typeof(Client), channel_name2, WellKnownObjectMode.Singleton);

                    BlockAndWait();
                    Take(didatuple, channel_name2, channel_port2);
                    Console.WriteLine("-> Succesful take!");
                    ChannelServices.UnregisterChannel(channel2);
                }
                else
                {
                    Console.WriteLine("-> Succesful take!");
                }
                break;

            case "wait":
                Console.WriteLine("Waiting");
                Wait(Int32.Parse(command[1]));
                break;

            case "begin-repeat":
                Console.WriteLine("Start Repeat");
                BeginRepeat(command[1]);
                break;

            case "end-repeat":
                EndRepeat(server_name, server_port);
                break;
            }
        }