public void addServerToRKM(ServerInfo serverInfo) { String serviceAddress = serverInfo.Channel + "://" + serverInfo.ServerAddress + ":" + serverInfo.ServerPort + "/" + serverInfo.ServiceName; IServer svc = (IServer)Activator.GetObject( typeof(IServer), serviceAddress); Console.WriteLine("New server added to RKM: {0}", serviceAddress); //if we don't have servers in the ring we don't need to set the last server pointer to the new one if (m_ring.Count > 0) { ServerInfo lastServerInfo; IServer lastServerProxy; bool notException = false; do { try { lastServerInfo = m_ring.Last<ServerInfo>(); serviceAddress = lastServerInfo.Channel + "://" + lastServerInfo.ServerAddress + ":" + lastServerInfo.ServerPort + "/" + lastServerInfo.ServiceName; lastServerProxy = (IServer)Activator.GetObject(typeof(IServer), serviceAddress); lastServerProxy.setNextRingServer(serverInfo); notException = false; }catch(Exception) { lock (lockThis) { m_ring.Remove(m_ring.Last<ServerInfo>()); } notException = true; } } while (notException && m_ring.Count > 0); } lock (lockThis) { m_ring.Add(serverInfo); } //the new server in the ring will have it's next server pointed to the first in the rings list of servers svc.setNextRingServer(m_ring.First<ServerInfo>()); printRingConfiguration(); }
public void setNextRingServer(ServerInfo nextServer) { m_nextServer = nextServer; Console.WriteLine("Next ring server: {0}:{1}", nextServer.ServerAddress, nextServer.ServerPort); }
public void removeServerFromRKM(ServerInfo oldServer) { lock (lockThis) { m_ring.Remove(oldServer); reconstructRing(); } }
public void disconnectFromServer(ServerInfo server) { try { lock (lockThis) { m_ring[m_ring.IndexOf(server)].ClientCount--; } }catch { removeServerFromRKM(server); } }
public ServerInfo connectToServer(ServerInfo oldServer = null) { //if oldServer is different of null means that that server is down. if(oldServer != null) { removeServerFromRKM(oldServer); printRingConfiguration(); } if (m_ring.Count <= 0) return null; //connect to the server with less clients int serverPos = getServerWithLessClients(); lock (lockThis) { m_ring[serverPos].ClientCount++; return m_ring[serverPos]; } }
public bool Equals(ServerInfo si) { // If parameter is null return false: if ((object)si == null) { return false; } // Return true if the fields match: return (ServerAddress == si.ServerAddress && ServerPort == si.ServerPort); }