public static bool kill(string node) { CachedXmlRpcClient cl = clientForNode(node); if (cl == null) { return(false); } XmlRpcValue req = new XmlRpcValue(), resp = new XmlRpcValue(), payl = new XmlRpcValue(); req.Set(0, this_node.Name); req.Set(1, "Out of respect for Mrs. " + this_node.Name); if (!cl.Execute("shutdown", req, resp) || !XmlRpcManager.Instance.validateXmlrpcResponse("lookupNode", resp, ref payl)) { return(false); } payl.Dump(); XmlRpcManager.Instance.releaseXMLRPCClient(cl); return(true); }
public CachedXmlRpcClient getXMLRPCClient(string host, int port, string uri) { CachedXmlRpcClient c = null; lock (clients_mutex) { List <CachedXmlRpcClient> zombies = new List <CachedXmlRpcClient>(); foreach (CachedXmlRpcClient client in clients) { if (!client.in_use) { if (DateTime.Now.Subtract(client.last_use_time).TotalSeconds > 30 || client.dead) { zombies.Add(client); } else if (client.CheckIdentity(host, port, uri)) { c = client; break; } } } foreach (CachedXmlRpcClient C in zombies) { clients.Remove(C); C.Dispose(); } if (c == null) { c = new CachedXmlRpcClient(host, port, uri); clients.Add(c); } } c.AddRef(); return(c); }
/// <summary> /// </summary> /// <param name="method"></param> /// <param name="request">Full request to send to the master </param> /// <param name="response">Full response including status code and status message. Initially empty.</param> /// <param name="payload">Location to store the actual data requested, if any.</param> /// <param name="wait_for_master">If you recieve an unseccessful status code, keep retrying.</param> /// <returns></returns> public static bool execute(string method, XmlRpcValue request, ref XmlRpcValue response, ref XmlRpcValue payload, bool wait_for_master) { try { DateTime startTime = DateTime.Now; string master_host = host; int master_port = port; //EDB.WriteLine("Trying to connect to master @ " + master_host + ":" + master_port); CachedXmlRpcClient client = XmlRpcManager.Instance.getXMLRPCClient(master_host, master_port, "/"); bool printed = false; bool success = false; while (!success) { // Check if we are shutting down if (XmlRpcManager.Instance.shutting_down) { return(false); } // if the client is connected, execute the RPC call success = client.IsConnected && client.Execute(method, request, response); // Set success to false when response validation fails if (success && !XmlRpcManager.Instance.validateXmlrpcResponse(method, response, ref payload)) { success = false; } if (success) { XmlRpcManager.Instance.releaseXMLRPCClient(client); return(true); } else { if (!wait_for_master) { XmlRpcManager.Instance.releaseXMLRPCClient(client); return(false); } if (!printed) { EDB.WriteLine("[{0}] FAILED TO CONTACT MASTER AT [{1}:{2}]. {3}", method, master_host, master_port, (wait_for_master ? "Retrying..." : "")); printed = true; } if (retryTimeout.TotalSeconds > 0 && DateTime.Now.Subtract(startTime) > retryTimeout) { EDB.WriteLine("[{0}] Timed out trying to connect to the master after [{1}] seconds", method, retryTimeout.TotalSeconds); XmlRpcManager.Instance.releaseXMLRPCClient(client); return(false); } //recreate the client, thereby causing it to reinitiate its connection (gross, but effective -- should really be done in xmlrpcwin32) XmlRpcManager.Instance.releaseXMLRPCClient(client); client = null; Thread.Sleep(50); client = XmlRpcManager.Instance.getXMLRPCClient(master_host, master_port, "/"); } } } catch (Exception e) { Console.WriteLine(e); } return(false); }
public void releaseXMLRPCClient(CachedXmlRpcClient client) { client.DelRef(); client.Dispose(); }
/// <summary> /// </summary> /// <param name="method"></param> /// <param name="request">Full request to send to the master </param> /// <param name="response">Full response including status code and status message. Initially empty.</param> /// <param name="payload">Location to store the actual data requested, if any.</param> /// <param name="wait_for_master">If you recieve an unseccessful status code, keep retrying.</param> /// <returns></returns> public static bool execute(string method, XmlRpcValue request, XmlRpcValue response, XmlRpcValue payload, bool wait_for_master) { try { DateTime startTime = DateTime.Now; string master_host = host; int master_port = port; CachedXmlRpcClient client = XmlRpcManager.Instance.getXMLRPCClient(master_host, master_port, "/"); bool printed = false; bool success = false; // UnityEngine.Debug.LogWarning ( "Current thread ID " + Thread.CurrentThread.ManagedThreadId ); while (!success) { // Check if we are shutting down if (XmlRpcManager.Instance.shutting_down) { return(false); } // if the client is connected, execute the RPC call success = client.IsConnected && client.Execute(method, request, response); if (client.IsConnected && !success) { if (response != null && response.asArray != null && response.asArray.Length >= 2) { EDB.WriteLine("Execute failed: return={0}, desc={1}", response[0].asInt, response[1].asString); } else { EDB.WriteLine("response type == " + (response != null ? response.Type.ToString() : "null")); } } // Set success to false when response validation fails if (success && !XmlRpcManager.Instance.validateXmlrpcResponse(method, response, payload)) { success = false; } if (success) { XmlRpcManager.Instance.releaseXMLRPCClient(client); return(true); } if (!wait_for_master) { XmlRpcManager.Instance.releaseXMLRPCClient(client); return(false); } if (!printed) { EDB.WriteLine("[{0}] FAILED TO CONTACT MASTER AT [{1}:{2}]. {3}", method, master_host, master_port, (wait_for_master ? "Retrying for the next " + retryTimeout.TotalSeconds + " seconds..." : "")); printed = true; } if (retryTimeout.TotalSeconds > 0 && DateTime.Now.Subtract(startTime) > retryTimeout) { EDB.WriteLine("[{0}] Timed out trying to connect to the master after [{1}] seconds", method, retryTimeout.TotalSeconds); XmlRpcManager.Instance.releaseXMLRPCClient(client); return(false); } //recreate the client, thereby causing it to reinitiate its connection (gross, but effective -- should really be done in xmlrpcwin32) XmlRpcManager.Instance.releaseXMLRPCClient(client); client = null; Thread.Sleep(250); client = XmlRpcManager.Instance.getXMLRPCClient(master_host, master_port, "/"); } } catch (Exception e) { EDB.WriteLine(e); } EDB.WriteLine("Master API call: {0} failed!\n\tRequest:\n{1}", method, request); return(false); }
public CachedXmlRpcClient getXMLRPCClient(string host, int port, string uri) { CachedXmlRpcClient c = null; lock (clients_mutex) { List<CachedXmlRpcClient> zombies = new List<CachedXmlRpcClient>(); foreach (CachedXmlRpcClient client in clients) { if (!client.in_use) { if (DateTime.Now.Subtract(client.last_use_time).TotalSeconds > 30 || client.dead) { zombies.Add(client); } else if (client.CheckIdentity(host, port, uri)) { c = client; break; } } } foreach (CachedXmlRpcClient C in zombies) { clients.Remove(C); C.Dispose(); } if (c == null) { c = new CachedXmlRpcClient(host, port, uri); clients.Add(c); } } c.AddRef(); return c; }