/* * Implements IFacadeService */ public bool Connect() { if (!_isOnline) { _isOnline = true; StartServices(); Log.Show(_username, "Attempting to Register user on central server\n"); _slotManager.Connect(); while (true) { try { Helper.GetRandomServer(_servers).RegisterUser(_username, Helper.GetIPAddress(), _port); Log.Show(_username, "Succesfully Registered\n"); break; } catch (Exception e) { Log.Debug(_username, "\nEXCEPTION: " + e.Message); } } return(true); } return(false); }
private IBookingService LookupClientStub(string userID) { ClientMetadata clientMd = null; bool contacted = false; while (!contacted) { try { //TODO: Navaneeth: here Log.Show(_userName, "#1"); clientMd = _activeServer.Lookup(userID); Log.Show(_userName, "#2"); contacted = true; } catch (Exception) { //server has failed //update server reference _activeServer = Helper.GetRandomServer(_servers); } } IBookingService client = null; if (clientMd != null) { String connectionString = "tcp://" + clientMd.IP_Addr + ":" + clientMd.Port + "/" + clientMd.Username + "/" + Common.Constants.BOOKING_SERVICE_NAME; //Log.Debug(_userName, "Looking up participant stub: " + connectionString); try { client = (IBookingService)Activator.GetObject( typeof(ILookupService), connectionString); } catch (SocketException e) { Log.Show(_userName, "ERROR: Could not connect to client: " + clientMd.Username + ". Exception: " + e); //TODO: do something? } } return(client); }
private int RetrieveSequenceNumber() { int seqNumber = -1; while (seqNumber == -1) { try { seqNumber = Helper.GetRandomServer(_servers).NextSequenceNumber(); } catch (Exception) { Console.WriteLine("\nCaught Exception Here!!\n"); //server has failed //will try to get another server in next iteration } } return(seqNumber); }
void IClientMonitor.StartMonitoring() { _activeServer = Helper.GetRandomServer(_servers); while (true) { //Monitor all monitored reservations foreach (Reservation res in _monitoredReservations.Values) { //Only check a reservation if some participant is not online if (res.ClientStubs.Count < (res.Participants.Count - 1)) { //copy client stubs dictionary (to avoid unecessary synchronization) Dictionary <string, IBookingService> clientStubs = new Dictionary <string, IBookingService>(res.ClientStubs); foreach (string userID in res.Participants) { //If we do not have the stub of a given participant, check for it if (!userID.Equals(_userName) && !clientStubs.ContainsKey(userID)) { Log.Debug(_userName, "Verifying if offline participant " + userID + " of reservation " + res.ReservationID + " came online."); IBookingService client = LookupClientStub(userID); if (client != null) { Log.Show(_userName, "Participant " + userID + " of reservation " + res.ReservationID + " has just came online."); _msgDispatch.ClientConnected(userID, client); //Lock here res.ClientStubs[userID] = client; //Unlock here } } } } } Thread.Sleep(SERVER_QUERY_TIME); } }
bool IClientFacade.Disconnect() { //pms.show(" ("+_username+")"+" I have received a Disconnect message"); if (_isOnline) { _isOnline = false; _slotManager.Disconnect(); Console.WriteLine("\nAttempting to Unregister user on central server\n"); StopServices(); while (true) { try { // Helper.GetRandomServer(_servers).Lookup("Paulo"); // Log.Show(_username, "[DEBUG] Lookup for Paulo"); Helper.GetRandomServer(_servers).UnregisterUser(_username); Log.Show(_username, "Succesfully Unregistered."); break; } catch (Exception e) { Log.Debug(_username, "\nEXCEPTION: " + e.Message); } } //Broadcast offline information to initiators of ongoing reservations Log.Show(_username, "Client is disconnected."); return(true); } return(false); }