Exemplo n.º 1
0
        // Gossip
        public List <string> suggestClientsForGossip(MeetingProposal meeting)
        {
            List <string> result   = new List <string>();
            List <string> invitees = meeting.getInvitees();

            if (invitees != null)
            {
                foreach (string client in this.clients)
                {
                    foreach (string invitee in invitees)
                    {
                        if (client.StartsWith(invitee))
                        {
                            result.Add(client);
                        }
                    }
                }
            }
            else
            {
                foreach (string client in this.clients)
                {
                    char[]   delimiter  = { '-' };
                    string[] clientInfo = client.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                    result.Add(clientInfo[0]);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public int gossip(MeetingProposal meeting, List <string> clients)
        {
            int    len = clients.Count;
            Random rnd = new Random();

            int numberOfGossipMembers = 3;

            SortedSet <int> gossiped_members = new SortedSet <int>();

            for (int i = 0; i < numberOfGossipMembers; i++)
            {
                int r = rnd.Next(0, len);

                int nr_tries = 0;
                while (gossiped_members.Contains(r))
                {
                    r = rnd.Next(0, len);
                    nr_tries++;
                    if (nr_tries == len)
                    {
                        break;
                    }
                }

                this.push(meeting, clients[r]);
                gossiped_members.Add(r);
            }

            return(0);
        }
Exemplo n.º 3
0
        public int push(MeetingProposal meeting, string node)
        {
            // Console.WriteLine(node + " pushed  " + command.commandId());
            ClientInterface client = null;

            try
            {
                client = (ClientInterface)Activator.GetObject(typeof(ClientInterface), node);
                if (client != null)
                {
                    PullRemoteAsyncDelegate RemoteDel = new PullRemoteAsyncDelegate(client.pull);
                    IAsyncResult            RemAr     = RemoteDel.BeginInvoke(meeting, null, null);
                    RemAr.AsyncWaitHandle.WaitOne();
                    int status = RemoteDel.EndInvoke(RemAr);
                    return(status);
                }
                else
                {
                    return(0);
                }
            }
            catch (Exception)
            {
                return(0);
            }
        }
Exemplo n.º 4
0
        public int pull(MeetingProposal meeting)
        {
            if (this.gossipedMeetings.Contains(meeting.getTopic()))
            {
                // Console.WriteLine(this.url + " stopped " + command.commandId());
                return(0);
            }

            List <string> invitees = meeting.getInvitees();

            if (invitees != null)
            {
                if (invitees.Contains(this.username))
                {
                    Console.WriteLine("You are invited to the meeting " + meeting.getTopic());
                }
            }
            else
            {
                Console.WriteLine("A meeting " + meeting.getTopic() + " was created");
            }

            this.gossipedMeetings.Add(meeting.getTopic());

            List <string> clients = this.getClientsForGossip();

            this.gossip(meeting, clients);

            return(1);
        }
Exemplo n.º 5
0
        public int execute(JoinCommand command)
        {
            // delay
            this.delay();

            if (this.isFrozen)
            {
                this.frozenCommands.Add(command);

                while (true)
                {
                    // freeze
                }

                return(0);
            }
            else
            {
                Console.WriteLine("Recieved " + command.getType() + "-" +
                                  command.getSequenceNumber() + " command from " + command.getIssuerId());

                MeetingProposal meeting = this.getMeetingByTopic(command.getTopic());

                if (meeting == null)
                {
                    Console.WriteLine("Meeting {0} not found, Contacting other servers ...", command.getTopic());
                    meeting = this.restoreLostMeeting(command.getTopic());

                    // Console.WriteLine("Meeting {0} not found, Waiting ...");
                    // Thread.Sleep(2000);

                    if (meeting == null)
                    {
                        Console.WriteLine("Meeting {0} not found", command.getTopic());
                        return(0);
                    }
                }

                if (meeting.isClosed() || meeting.isCancelled())
                {
                    // a client can join a closed meeting if it was joined before the closing momment
                    if (command.getTimestamp().CompareTo(meeting.getClosingTimestamp()) < 0)
                    {
                        meeting.open();
                        this.joinMeeting(command.getIssuerId(), meeting, command.getDesiredSlots());
                        meeting.close();
                    }
                }
                else
                {
                    this.joinMeeting(command.getIssuerId(), meeting, command.getDesiredSlots());
                }

                this.informOtherServers(command);

                return(1);
            }
        }
Exemplo n.º 6
0
 public CreateCommand(string topic, int min_attendees, int nr_slots, int nr_invitees, List <Slot> slots, List <string> invitees)
 {
     this.topic         = topic;
     this.min_attendees = min_attendees;
     this.nr_slots      = nr_slots;
     this.nr_invitees   = nr_invitees;
     this.slots         = slots;
     this.invitees      = invitees;
     this.meeting       = new MeetingProposal(this.getIssuerId(), this.topic, this.min_attendees, this.slots, this.invitees);
 }
Exemplo n.º 7
0
 private void joinMeeting(string client_id, MeetingProposal proposal, List <Slot> desiredSlots)
 {
     Monitor.Enter(this.meetings);
     // client cannot join a meeting if it is closed or cancelled
     if (proposal.isClosed() || proposal.isCancelled())
     {
         return;
     }
     proposal.addParticipant(client_id, desiredSlots);
     Console.WriteLine(client_id + " joined " + proposal.getTopic());
     Monitor.Exit(this.meetings);
 }
Exemplo n.º 8
0
 // Private Methods
 private MeetingProposal mergeTwoMeetings(MeetingProposal proposal1, MeetingProposal proposal2)
 {
     Console.WriteLine("merging meetings");
     foreach (string participant in proposal1.getParticipants().Keys)
     {
         if (!proposal2.getParticipants().ContainsKey(participant))
         {
             proposal2.addParticipant(participant, proposal1.getParticipants()[participant]);
         }
     }
     return(proposal2);
 }
Exemplo n.º 9
0
        private MeetingProposal closeMeeting(string client_id, string topic)
        {
            Monitor.Enter(this.meetings);
            MeetingProposal proposal = this.getMeetingByTopic(topic);

            if (proposal == null)
            {
                return(null);
            }

            proposal.close();
            Monitor.Exit(this.meetings);
            return(proposal);
        }
Exemplo n.º 10
0
 public MeetingProposal(MeetingProposal proposal)
 {
     this.coordinator       = proposal.getCoordinator();
     this.topic             = proposal.getTopic();
     this.min_attendees     = proposal.min_attendees;
     this.slots             = proposal.getSlots();
     this.invitees          = proposal.invitees;
     this.closed            = proposal.isClosed();
     this.cancelled         = proposal.isCancelled();
     this.participants      = proposal.getParticipants();
     this.finalParticipants = proposal.getFinalParticipants();
     this.closingTimestamp  = proposal.getClosingTimestamp();
     this.selectedRoom      = proposal.selectedRoom;
     this.selectedSlot      = proposal.selectedSlot;
     this.roomsManager      = proposal.roomsManager;
 }
Exemplo n.º 11
0
        private void createMeeting(string client_id, MeetingProposal proposal)
        {
            Monitor.Enter(this.meetings);
            proposal.setRoomsManager(this.roomsManager);

            if (!this.meetings.ContainsKey(client_id))
            {
                this.meetings[client_id] = new HashSet <MeetingProposal>();
            }

            bool already_exists = false;

            foreach (MeetingProposal meetingProposal in this.meetings[client_id])
            {
                if (meetingProposal.getTopic() == proposal.getTopic())
                {
                    already_exists = true;
                    break;
                }
            }

            if (!already_exists)
            {
                this.meetings[client_id].Add(proposal);
            }
            else
            {
                // merge two meetings
                MeetingProposal original = null;
                foreach (MeetingProposal meetingProposal in this.meetings[client_id])
                {
                    if (meetingProposal.getTopic() == proposal.getTopic())
                    {
                        original = meetingProposal;
                        this.meetings[client_id].Remove(meetingProposal);
                        break;
                    }
                }
                original = this.mergeTwoMeetings(original, proposal);
                this.meetings[client_id].Add(original);
            }
            Monitor.Exit(this.meetings);
        }
Exemplo n.º 12
0
        public int execute(CloseCommand command)
        {
            //TODO: check causal consistency and total order

            // delay
            this.delay();

            if (this.isFrozen)
            {
                this.frozenCommands.Add(command);

                while (true)
                {
                    // freeze
                }

                return(0);
            }
            else
            {
                Console.WriteLine("Recieved " + command.getType() + " command from " + command.getIssuerId());
                MeetingProposal meeting = this.closeMeeting(command.getIssuerId(), command.getTopic());

                if (meeting == null)
                {
                    meeting = this.restoreLostMeeting(command.getTopic());

                    if (meeting == null)
                    {
                        Console.WriteLine("Meeting not found");
                        return(0);
                    }
                }

                meeting.setClosingTimestamp(command.getTimestamp());
                meeting.close();

                this.informOtherServers(command);

                return(1);
            }
        }
Exemplo n.º 13
0
        private MeetingProposal restoreLostMeeting(string topic)
        {
            Console.WriteLine("Restoring " + topic);
            string serversList = "servers.txt";

            string[] serversURLs = File.ReadAllLines(serversList);
            foreach (string url in serversURLs)
            {
                ServerObject server = (ServerObject)Activator.GetObject(typeof(ServerObject), url);
                if (server != null)
                {
                    MeetingProposal proposal      = server.getMeetingByTopic(topic);
                    MeetingProposal proposal_copy = new MeetingProposal(proposal);

                    if (proposal_copy != null)
                    {
                        this.createMeeting(proposal_copy.getCoordinator(), proposal_copy);
                        return(proposal_copy);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 14
0
        public override bool Equals(object obj)
        {
            MeetingProposal proposal = (MeetingProposal)obj;

            return(proposal.getTopic() == this.getTopic());
        }