public static PadInt AccessPadInt(int uid) { if (!InTransaction) { throw new TxException("Not in a transaction"); } //check if PadInt is locally known PadInt padInt = null; if (padIntsMap.TryGetValue(uid, out padInt)) return padInt; //check if the server for that PadInt is known bool padintServerKnown = KnownPadInts.TryGetValue(uid, out server); if (!padintServerKnown) { string serverURL = master.findPadIntServer(uid); bool serverFound = !serverURL.Equals(Constants.NOT_FOUND); if (serverFound) { server = (iServer)Activator.GetObject(typeof(iServer), serverURL); KnownPadInts.Add(uid, server); padintServerKnown = true; } } if (!padintServerKnown) return null; bool serverKnowsPadInt = false; try { ThreadStart del = () => serverKnowsPadInt = server.hasPadInt(uid, currTransaction); bool succeeded = SharedMethods.tryRemoteCall(del); if (!succeeded) { //FIXME retry to get server iServer s = refreshServerForPadInt(uid); ThreadStart secDel = () => serverKnowsPadInt = s.hasPadInt(uid, currTransaction); bool suc = SharedMethods.tryRemoteCall(secDel); if (!suc) { throw new TxException("Server given by master was failed."); } } //serverKnowsPadInt = server.hasPadInt(uid, currTransaction); } catch (NetworkException netException) { throw new TxException(netException.CustomMessage); } if (!serverKnowsPadInt) { throw new TxException("Inconsistency. Server should know about it's padints."); } PadInt newPadInt = new PadInt(uid); padIntsMap.Add(uid, newPadInt); return newPadInt; }
internal static iServer refreshServerForPadInt(int uid) { iServer server; KnownPadInts.Remove(uid); string serverURL = master.findPadIntServer(uid); server = (iServer)Activator.GetObject(typeof(iServer), serverURL); KnownPadInts.Add(uid,server); return server; }
public static bool Init() { masterURL = "tcp://localhost:"+Constants.MASTER_PORT+"/"+Constants.MASTER_NAME; master = (iMaster)Activator.GetObject(typeof(iMaster), masterURL); currTransaction = null; KnownPadInts = new Dictionary<int, iServer>(); padIntsMap = new Dictionary<int, PadInt>(); temporaryPadInts = new Dictionary<int, PadInt>(); server = null; return true; }
internal static iServer getServerForPadInt(int uid) { iServer server; if (!KnownPadInts.TryGetValue(uid, out server)) { string serverURL = master.findPadIntServer(uid); server = (iServer)Activator.GetObject(typeof(iServer), serverURL); } return server; }
public ServerInformation(iServer server, String status) { this.server = server; this.status = status; ocupation = 0; transactions = 0; }
public static PadInt CreatePadInt(int uid) { if (!InTransaction) { throw new TxException("Not in a transaction"); } PadInt padInt = null; if (padIntsMap.TryGetValue(uid, out padInt) || temporaryPadInts.TryGetValue(uid, out padInt)) { return null; //already exists (cf.enunciado) } string serverURL = master.findPadIntServer(uid); bool serverNotFound = serverURL.Equals(Constants.NOT_FOUND); if (serverNotFound) { try { serverURL = master.getServerLessCharged(currTransaction); server = (iServer)Activator.GetObject(typeof(iServer), serverURL); KnownPadInts.Add(uid, server); ThreadStart myDel = () => server.createPadInt(uid, currTransaction); bool succeeded = SharedMethods.tryRemoteCall(myDel); if (!succeeded) { //FIXME retry to get server iServer s = refreshServerForPadInt(uid); ThreadStart secDel = () => s.createPadInt(uid, currTransaction); bool suc = SharedMethods.tryRemoteCall(secDel); if (!suc) { throw new TxException("Server given by master was failed."); } } //server.createPadInt(uid, currTransaction); padInt = new PadInt(uid); temporaryPadInts.Add(uid, padInt); return padInt; } catch (NetworkException netException) { throw new TxException(netException.CustomMessage); } } return null; //already exists }
// Default constructor is disabled because I specified a constructor // Do not create a default constructor public Connection(iSocket.iSocket temp, iServer server) { this.sock = temp; this.server = server; }
// Default constructor is disabled because I specified a constructor // Do not create a default constructor public Connection(iSocket.iSocket temp, iServer server) { this.sock = temp; this.server = server; }
public void setSecundaryURL(string url) { secundary = (iServer)Activator.GetObject(typeof(iServer), url); secundaryURL = url; sendAllData(); if (pingTimer != null) { pingTimer.Abort(); pingTimer = null; } }
public void setPrimaryURL(string url) { primary = (iServer)Activator.GetObject(typeof(iServer), url); primaryURL = url; pingTimer = new Thread(delegate(){ while (primary != null) { checkPing(); } }); pingTimer.Start(); }
public void onSecundaryServerFailure() { checkFreezed(); if (getStatus().Equals(Constants.STATUS_FAILED)) { master.removeServer(serverURL); new Thread(killServer).Start(); return; } if (!failed && secundary != null) { //No master: update do estado do secundario + primario sem secundario master.failSecundaryServer(secundaryURL, this.serverURL); Monitor.Enter(this); this.secundary = null; lastPingId = 0; Monitor.Exit(this); //novo secundario: lançar. (faz actualizar-se no master) //isto tb cria URL, e actualiza servidor secundario no primario. launchNewServer(); } }
//int actualNumberConections; public Server(string serverURL) { masterURL = "tcp://localhost:"+Constants.MASTER_PORT+"/"+Constants.MASTER_NAME; this.serverURL = serverURL; master = (iMaster)Activator.GetObject(typeof(iMaster), masterURL); padIntsMap = new Dictionary<int, ServerPadInt>(); tIdPadIntMap = new Dictionary<IdTransaction, ISet<int>>(); createdPadIntsMap = new Dictionary<int, ServerPadInt>(); freezed = false; freezedLock = new Object(); notFreezed = new AutoResetEvent(true); failed = false; primary = null; secundary = null; primaryURL = ""; secundaryURL = ""; pingData = new UpdateData(); allPingData = new UpdateData(); lastPingId = 0; pendingUpdates = new Dictionary<int,UpdateData>(); pingTimer = null; receivedPing = true; }
private void checkPing() { Thread.Sleep(TimeSpan.FromSeconds(Constants.TAKEOVER_TIMEOUT)); if (failed) { return; } checkFreezed(); if (getStatus().Equals(Constants.STATUS_FAILED)) { killServer(); return; } if (receivedPing) { receivedPing = false; } else { Console.WriteLine("Secundary takeover..."); master.failPrimaryServer(primaryURL, this.serverURL); primaryURL = ""; secundaryURL = ""; primary = null; secundary = null; lastPingId = 0; //novo secundario: lançar. (faz actualizar-se no master) launchNewServer(); } }