public bool CreateReservation(ReservationRequest reservation) { //initiate the reservation //setup callbacks //return; return true; }
//Verify calendar slots and create reservation states private List<ReservationSlot> CreateReservationSlots(ReservationRequest req, int resID) { List<ReservationSlot> reservationSlots = new List<ReservationSlot>(); foreach (int slot in req.Slots) { ReservationSlotState state = ReservationSlotState.INITIATED; CalendarSlot calendarSlot; if (_calendar.TryGetValue(slot, out calendarSlot)) { if (calendarSlot.State == CalendarSlotState.ASSIGNED) { state = ReservationSlotState.ABORTED; } } else { calendarSlot = new CalendarSlot(); calendarSlot.SlotNum = slot; calendarSlot.State = CalendarSlotState.FREE; _calendar[slot] = calendarSlot; Log.Debug(_userName, "Creating new calendar entry. Slot: " + calendarSlot.SlotNum + ". State: " + calendarSlot.State); } Monitor.Enter(calendarSlot); if (calendarSlot.State == CalendarSlotState.FREE) { calendarSlot.State = CalendarSlotState.ACKNOWLEDGED; } Monitor.Exit(calendarSlot); calendarSlot.WaitingBook.Add(resID); ReservationSlot rs = new ReservationSlot(resID, slot, state); reservationSlots.Add(rs); } return reservationSlots; }
private Reservation CreateReservation(ReservationRequest req, int resID, string initiatorID, string initiatorIP, int initiatorPort) { Reservation thisRes = new Reservation(); thisRes.ReservationID = resID; thisRes.Description = req.Description; thisRes.Participants = req.Users; thisRes.InitiatorID = initiatorID; thisRes.InitiatorIP = initiatorIP; thisRes.InitiatorPort = initiatorPort; return thisRes; }
/* * SLOT MANAGER METHODS */ public bool StartReservation(ReservationRequest req) { //Updates request with sequence number int resID = RetrieveSequenceNumber(); //Create and populate local reservation Reservation res = CreateReservation(req, resID, _userName, Helper.GetIPAddress(), _port); //Mark slots initial states List<ReservationSlot> reservationSlots = CreateReservationSlots(req, resID); //Update reservation request, removing aborted slots foreach (ReservationSlot slot in new List<ReservationSlot>(reservationSlots)) { if (slot.State == ReservationSlotState.ABORTED) { Log.Show(_userName, "Slot " + slot + " not available on initiator. Removing from reservation."); //removing slot of original request, since it will be passed to participants reservationSlots.Remove(slot); req.Slots.Remove(slot.SlotID); } } res.Slots = reservationSlots; Log.Show(_userName, "Starting reservation " + res.ReservationID + ". With participants " + string.Join(",", res.Participants) + ". Slots: " + string.Join(",", res.Slots)); //If no slots are available, cancel reservation if (res.Slots.Count == 0) { Log.Show(_userName, "No available slots on initiator, aborting reservation."); return false; } //just the initiator is on the reservation if (req.Users.Count == 1) { foreach (ReservationSlot slot in res.Slots) { Monitor.Enter(_calendar[slot.SlotID]); try { CalendarSlot cSlot = _calendar[slot.SlotID]; if (slot.State != ReservationSlotState.ABORTED && !cSlot.Locked && cSlot.State != CalendarSlotState.ASSIGNED) { AssignCalendarSlot(res, slot, true); return true; } else if (slot.State != ReservationSlotState.ABORTED) { AbortReservationSlot(slot, true); } } finally { Monitor.Exit(_calendar[slot.SlotID]); } } return false; } _clientMonitor.MonitorReservation(res); //Add reservation to map of active reservations _activeReservations[res.ReservationID] = res; foreach (string participantID in res.Participants) { if (!participantID.Equals(_userName)) { _msgDispatcher.SendMessage(MessageType.INIT_RESERVATION, resID, participantID, req, _userName, Helper.GetIPAddress(), _port); } } return true; }
/* * BOOKING SERVICE PATICIPANT */ void IBookingService.InitReservation(ReservationRequest req, int resID, string initiatorID, string initiatorIP, int initiatorPort) { //Create and populate local reservation Reservation res = CreateReservation(req, resID, _userName, initiatorIP, initiatorPort); //Mark slots initial states res.Slots = CreateReservationSlots(req, resID); Log.Show(_userName, "Initializing reservation " + res.ReservationID + ". Initiator: " + res.InitiatorID + ". Slots: " + string.Join(", ", res.Slots)); bool abortedAll = true; foreach (ReservationSlot slot in res.Slots) { if (slot.State != ReservationSlotState.ABORTED) { abortedAll = false; break; } } //If no slots are available, don't store reservation if (abortedAll) { Log.Show(_userName, "No available slots on this participant. Reservation will not be persisted."); } else { //Add reservation to map of active reservations _activeReservations[res.ReservationID] = res; } //Reply to initiator String connectionString = "tcp://" + initiatorIP + ":" + initiatorPort + "/" + initiatorID + "/" + Common.Constants.BOOKING_SERVICE_NAME; try { IBookingService initiator = (IBookingService)Activator.GetObject( typeof(ILookupService), connectionString); res.InitiatorStub = initiator; initiator.InitReservationReply(res.Slots, _userName); } catch (SocketException) { Log.Show(_userName, "ERROR: Initiator is not online."); } }
bool IClientFacade.CreateReservation(ReservationRequest reservation) { return _slotManager.StartReservation(reservation); }
private void readEventFileAndDispatchEvents() { string line = string.Empty; System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); while ((line = sr.ReadLine()) != null) { show(line); string opcode = line.Split(' ')[0]; string username; IClientFacade icf; IServerFacade sf; List<CalendarSlot> clientCalendar; //TODO: fetch initiator from Users list switch (opcode) { case "disconnect": username = line.Split(' ')[1]; if (username.StartsWith("central")) { ServerMetadata sm = (ServerMetadata)pms.getServersList()[username]; IServerFacade isf = (IServerFacade)pms.getServerFacadeList()[username]; while (isf == null) { show("Trying to disconnect server: " + username); isf = (IServerFacade)pms.getServerFacadeList()[username]; Thread.Sleep(100); } try { isf.Disconnect(); changeIconToDisconnected(username, null); } catch (Exception ioe) { MessageBox.Show(ioe.ToString()); } } else { icf = (IClientFacade)pms.getClientFacadeList()[username]; while (icf == null) { show("Trying to disconnect client: " + username); icf = (IClientFacade)pms.getClientFacadeList()[username]; Thread.Sleep(1000); } try { icf.Disconnect(); } catch (Exception ee) { show("Exception: " + ee.Message); } changeIconToDisconnected(username, null); } break; case "connect": username = line.Split(' ')[1].Trim(); string ip = line.Split(' ')[2].Split(':')[0].Trim(); int port = Convert.ToInt32(line.Split(' ')[2].Split(':')[1]); icf = (IClientFacade)pms.getClientFacadeList()[username]; sf = (IServerFacade)pms.getServerFacadeList()[username]; if (icf != null) //means the client has already been created { show("Trying to reconnect to client: " + username); icf.Connect(); changeIconToConnected(username, null); } else if (sf != null) { show("Reconnecting to server: " + username); sf.Connect(); changeIconToConnected(username, null); } else //means a new process needs to be started { if (username.StartsWith("central")) { string path = server_dir; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = path + "Server.exe"; startInfo.Arguments = username + " " + port; startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process process = new Process(); process.StartInfo = startInfo; process.Start(); processes.Add(process); //TODO: save the PIDS of all processes and kill them on exit //if server is online, we move on to the next event. If not, we wait until the server is online IServerFacade isf = (IServerFacade)pms.getServerFacadeList()[username]; while (isf == null) { Thread.Sleep(500); isf = (IServerFacade)pms.getServerFacadeList()[username]; } } else //means its a client { string path = client_dir; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = path + "Client.exe"; startInfo.Arguments = username + " " + port; startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process process = new Process(); process.StartInfo = startInfo; process.Start(); processes.Add(process); //if client is online, we move on to the next event. If not, we wait until the client is online IClientFacade cf = (IClientFacade)pms.getClientFacadeList()[username]; while (cf == null) { Thread.Sleep(500); cf = (IClientFacade)pms.getClientFacadeList()[username]; } } } break; case "readCalendar": username = line.Split(' ')[1].Trim(); icf = (IClientFacade)pms.getClientFacadeList()[username]; try { clientCalendar = icf.ReadCalendar(); show("Calendar for " + username + ":"); foreach (CalendarSlot slot in clientCalendar) { show(slot); } } catch (Exception ee) { show("Exception: " + ee.Message); } break; case "reservation": //reservation {GroupMeeting; user1, user2; 13, 25 } string data = line.Split('{')[1].Split('}')[0].Trim(); string desc = data.Split(';')[0].Trim(); string initiator = data.Split(';')[1].Split(',')[0].Trim(); List<string> usersList = data.Split(';')[1].Trim().Split(',').Select(s => s.Trim()).ToList<string>(); List<int> slotsList = data.Split(';')[2].Trim().Split(',').Select(s => s.Trim()).ToList<string>().ConvertAll<int>(Convert.ToInt32); ReservationRequest rr = new ReservationRequest(); rr.Description = desc; rr.Users = usersList; rr.Slots = slotsList; icf = (IClientFacade)pms.getClientFacadeList()[initiator]; while (icf == null) { show("Trying to reconnect to initiator: " + initiator); icf = (IClientFacade)pms.getClientFacadeList()[initiator]; Thread.Sleep(100); } try { icf.CreateReservation(rr); } catch (Exception e) { show("Error on create reservation: " + e.Message); } Thread.Sleep(1000); break; case "wait": int seconds = Convert.ToInt32(line.Split(' ')[1].Trim()); show("Sleeping for " + seconds + " second(s)"); Thread.Sleep(seconds * 1000); break; case "shutdown": show("Shutting down all processes in 2 seconds"); Thread.Sleep(2000); //killing all clients foreach (Process pp in processes) { string userName = pp.StartInfo.Arguments.Split(' ')[0].Trim(); if (!userName.StartsWith("central")) { ((IClientFacade)pms.getClientFacadeList()[userName]).Disconnect(); pp.Kill(); removeNodeFromTreeView(userName, null); show("Killed " + pp.StartInfo.Arguments); } } //killing all servers foreach (Process p in processes) { string userName = p.StartInfo.Arguments.Split(' ')[0].Trim(); if (userName.StartsWith("central")) { try { ((IServerFacade)pms.getServerFacadeList()[userName]).Disconnect(); } catch (Exception ee) { show(ee.Message); } p.Kill(); removeNodeFromTreeView(userName, null); show("Killed " + p.StartInfo.Arguments); } } //cleaning up pms.cleanUp(); //RemotingServices.Unmarshal(objRef); //objRef = null; //pms = null; processes.Clear(); //unregistering channels UnregisterChannels(); show(" "); show("----------------------SHUTDOWN COMPLETE--------------------"); show(" "); break; } } sr.Close(); }
private void createRes_Click(object sender, EventArgs e) { string initiator = usersBox.Text.Split(',')[0]; List<string> usersList = usersBox.Text.Split(',').ToList<string>(); List<int> slotsList = slotsBox.Text.Split(',').ToList().ConvertAll<int>(Convert.ToInt32); string desc = descBox.Text; ReservationRequest rr = new ReservationRequest(); rr.Description = desc; rr.Users = usersList; rr.Slots = slotsList; IClientFacade icf = (IClientFacade)pms.getClientFacadeList()[initiator]; try { if (icf != null) icf.CreateReservation(rr); //There is an exception unhandled here. else show("Unable to get Client Facade of Initiator"); } catch (Exception ex) { Console.WriteLine("[PMS-EXCEPTION] " + ex.ToString()); } }