public Room getAvailableRoom(DateLocation finalDateLocation, MeetingProposal proposal) { if (finalDateLocation.Invitees < proposal.MinAttendees) { return(null); } Location location = _locations[finalDateLocation.LocationName]; //the final location SortedDictionary <int, Room> possibleRooms = new SortedDictionary <int, Room>(); //rooms not booked int maxCapacity = 0; foreach (KeyValuePair <string, Room> room in location.Rooms) { //checks if the room is available for that day if (!room.Value.BookedDays.Contains(finalDateLocation)) { possibleRooms.Add(room.Value.Capacity, room.Value); if (maxCapacity < room.Value.Capacity) { maxCapacity = room.Value.Capacity; // room with the biggest capacity } } } Room finalRoom = new Room(); //if there's no room for everybody the final room is the one with the biggest capacity if (maxCapacity < finalDateLocation.Invitees && possibleRooms.Count != 0) { finalRoom = possibleRooms[maxCapacity]; } //else choose the the first room that fits everyone else { foreach (KeyValuePair <int, Room> room in possibleRooms) { if (room.Key >= finalDateLocation.Invitees) { finalRoom = room.Value; break; } } } if (possibleRooms.Count > 0) { //changes the room availability to booked if it doesn't get cancelled _locations[finalDateLocation.LocationName].Rooms[finalRoom.Name].AddBookedDay(finalDateLocation); BroadcastUpdateLocation(location); return(finalRoom); } return(null); }
public void Close(string topic, string urlFailed = null) { while (_isFrozen) { } Thread.Sleep(RandomIncomingMessageDelay()); if (_currentMeetingProposals[topic].MeetingStatus == MeetingStatus.OPEN) { if (_serverLocation == null) { List <string> aliveUrls = new List <string>(); foreach (KeyValuePair <string, bool> url in _serversStatus) { if (url.Value == false) { aliveUrls.Add(url.Key); } } aliveUrls.Sort(); _serverLocation = aliveUrls[0]; Console.WriteLine("server location" + _serverLocation); } MeetingProposal proposal = _currentMeetingProposals[topic]; DateLocation finalDateLocation = new DateLocation(); // each dateLocation has the number of invitees that can go to that slot foreach (DateLocation dateLocation in proposal.DateLocationSlots) { if (dateLocation.Invitees > finalDateLocation.Invitees) //chooses the dateLocation with more invitees { finalDateLocation = dateLocation; } } Room finalRoom = _servers[_serverLocation].getAvailableRoom(finalDateLocation, proposal); if (finalDateLocation.Invitees < proposal.MinAttendees || finalRoom == null) { proposal.MeetingStatus = MeetingStatus.CANCELLED; } else { int countInvitees = 0; proposal.MeetingStatus = MeetingStatus.CLOSED; proposal.FinalRoom = finalRoom; proposal.FinalDateLocation = finalDateLocation; // sort records by VectorClock proposal.Records.Sort(); foreach (MeetingRecord record in proposal.Records) { Console.WriteLine(record.ToString()); if (record.DateLocationSlots.Contains(finalDateLocation)) { countInvitees++; Console.WriteLine("record " + record); //if there's more invitees than the room capacity they go to a special list if (countInvitees > proposal.FinalRoom.Capacity)//maxCapacity) { proposal.AddFullRecord(record); } else { proposal.Participants.Add(record.Name); } } } } Console.WriteLine(proposal.Coordinator + " closed meeting proposal " + proposal.Topic + "."); // we update the respective vector clock incrementVectorClock(topic); // we update the respective log updateLog(topic); BroadcastClose(proposal); } }