/// <summary>
        /// Runs the method, specified by method, on the server specified by i, with parameters given by parameters.
        /// Correctly handles exceptions thrown in the running of that method, and bool and BufferGenerationStatus
        /// type return values from the server.
        /// </summary>
        /// <param name="method"></param>
        /// <param name="i"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ServerActionStatus runMethodOnServer(MethodInfo method, ServerInfo i, object[] parameters, int msTimeout, EventHandler<MessageEvent> messageLog)
        {
            if (verifyConnection(i) == ServerActionStatus.Failed_No_Connection)
                return ServerActionStatus.Failed_No_Connection;

            ServerCommunicator communicator = communicators[i];

            Object returnValue;

            try
            {
                InvokeMethodOnServer_Parameters invoke_parameters = new InvokeMethodOnServer_Parameters(method, parameters, communicator);
                Thread invokeMethodThread = new Thread(new ParameterizedThreadStart(InvokeMethodOnServer_Proc));
                invokeMethodThread.Start(invoke_parameters);

               // invokeMethodThread.Join(msTimeout);
                invokeMethodThread.Join();

                if (invokeMethodThread.ThreadState == ThreadState.Running)
                {
                    invokeMethodThread.Abort();

                    throw new Exception("Method on server " + i.ToString() + "." + method.Name + " took longer than " + msTimeout + " ms timeout. Aborting.");
                }

                returnValue = invoke_parameters.returnValue;

                if (invoke_parameters.runException != null)
                {
                    throw new Exception("Caught an exception when attempting to run method " + i.ToString() + "." + method.Name+ ": " + invoke_parameters.runException.Message);
                }
            }
            catch (Exception e)
            {
                if (messageLog != null)
                    messageLog(this, new MessageEvent("Caught exception when attempting to run method on server " + i.ToString() + ": " + e.Message + e.StackTrace));

                serverGotDisconnectedInError(i);
                return ServerActionStatus.Failed_Loss_Of_Connection;
            }

            if (returnValue is bool)
            {
                bool attempt = (bool) returnValue;
                if (attempt)
                    return ServerActionStatus.Success;
                return ServerActionStatus.Failed_On_Server;
            }
            else if (returnValue is ServerCommunicator.BufferGenerationStatus)
            {
                ServerCommunicator.BufferGenerationStatus bufferStatus = (ServerCommunicator.BufferGenerationStatus) returnValue;
                if (bufferStatus == ServerCommunicator.BufferGenerationStatus.Success)
                    return ServerActionStatus.Success;
                return ServerActionStatus.Failed_On_Server;
            }

            return ServerActionStatus.Success;
        }
        public bool pingServer(ServerInfo server, EventHandler<MessageEvent> messageLog)
        {
            if (!communicators.ContainsKey(server))
            {
                if (messageLog != null)
                    messageLog(this, new MessageEvent("Attempted to ping unconnected server " + server.ToString()));
                return false;
            }

            ServerCommunicator comm = communicators[server];
            if (comm == null)
            {
                if (messageLog != null)
                    messageLog(this, new MessageEvent("Server communication object is null, unable to ping server " + server.ToString()));
                return false;
            }

            try
            {
                Thread pingThread = new Thread(new ParameterizedThreadStart(ping_server_proc));
                pingThread.Start(comm);
                pingThread.Join(1000);
                if (pingThread.ThreadState == ThreadState.Running)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent("Server ping took longer than 1000ms. Aborting."));

                    pingThread.Abort();

                    return false;
                }

            }
            catch (Exception e)
            {
                if (messageLog != null)
                    messageLog(this, new MessageEvent("Caught exception when attempting to ping server " + server.ToString() + ": " + e.Message + e.StackTrace));
                return false;
            }

            return true;
        }
        public bool connectServer(ServerInfo server, EventHandler<MessageEvent> messageLog)
        {
            if (messageLog != null)
                messageLog(this, new MessageEvent("Attempting to connect server " + server.ToString()));

            bool gotLock = Monitor.TryEnter(lockObj, 1000);
            try
            {
                if (!gotLock)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent("Unable to obtain lock on servermanager. Aborting attempt."));
                    return false;
                }

                // Cannot connect to a server that isn't is the servers list.
                if (!servers.Contains(server))
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is not on list of known servers. Aborting."));
                    return false;
                }

                // ensure that this server has a server connection status item
                if (!connections.ContainsKey(server))
                {
                    connections.Add(server, ConnectionStatus.Disconnected_Normal);
                }

                // if this server is disabled, then don't connect to it.
                if (!server.ServerEnabled)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is not enabled. Aborting."));
                    return false;
                }

                // set server status to connecting. If the server already was connecting, then return.
                if (connections[server] == ConnectionStatus.Connecting)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent(server.ToString() + " is already connecting. Aborting."));
                    return false;
                }

                connections[server] = ConnectionStatus.Connecting;

                // create an empty spot for the server communicator
                if (!communicators.ContainsKey(server))
                {
                    communicators.Add(server, null);
                }

                communicators[server] = null;

                // try to create the communicator
                try
                {

                    Thread connectThread = new Thread(new ParameterizedThreadStart(server_connect_proc));
                    connectThread.Start(server);

                    connectThread.Join(1000);
                    if (connectThread.ThreadState == ThreadState.Running)
                    {
                        connectThread.Abort();
                        if (messageLog != null)
                            messageLog(this, new MessageEvent("Connection took longer than 1000 ms, aborting."));

                        connectThread.Abort();
                        return false;
                    }

                }
                catch (Exception e)
                {
                    if (messageLog != null)
                        messageLog(this, new MessageEvent("Caught exception when attempting to connect to server " + server.ToString() + ":" + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return false;
                }

                try
                {

                    if (pingServer(server, messageLog))
                    {
                        server.setServerName(communicators[server].getServerName());

                        if (connectedServerNames.Contains(server.ServerName))
                        {
                            connections[server] = ConnectionStatus.Error_Name_Not_Unique;
                            communicators.Remove(server);
                            return false;
                        }

                        connectedServerNames.Add(server.ServerName);
                        connections[server] = ConnectionStatus.Connected;
                        messageLog(this, new MessageEvent("Server connected successfully."));
                        return true;
                    }
                    else
                    {
                        if (messageLog != null)
                            messageLog(this, new MessageEvent("Server ping failed. Aborting."));
                        connections[server] = ConnectionStatus.Unable_To_Connect;
                        return false;
                    }
                }
                catch (Exception e)
                {
                    messageLog(this, new MessageEvent("Exception when attempting to ping server: " + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return false;
                }
            }
            finally
            {
                if (gotLock)
                    Monitor.Exit(lockObj);
            }
            return true;
        }
示例#4
0
        /// <summary>
        /// Runs the method, specified by method, on the server specified by i, with parameters given by parameters.
        /// Correctly handles exceptions thrown in the running of that method, and bool and BufferGenerationStatus
        /// type return values from the server.
        /// </summary>
        /// <param name="method"></param>
        /// <param name="i"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ServerActionStatus runMethodOnServer(MethodInfo method, ServerInfo i, object[] parameters, int msTimeout, EventHandler <MessageEvent> messageLog)
        {
            if (verifyConnection(i) == ServerActionStatus.Failed_No_Connection)
            {
                return(ServerActionStatus.Failed_No_Connection);
            }

            ServerCommunicator communicator = communicators[i];

            Object returnValue;

            try
            {
                InvokeMethodOnServer_Parameters invoke_parameters = new InvokeMethodOnServer_Parameters(method, parameters, communicator);
                Thread invokeMethodThread = new Thread(new ParameterizedThreadStart(InvokeMethodOnServer_Proc));
                invokeMethodThread.Start(invoke_parameters);

                // invokeMethodThread.Join(msTimeout);
                invokeMethodThread.Join();

                if (invokeMethodThread.ThreadState == ThreadState.Running)
                {
                    invokeMethodThread.Abort();

                    throw new Exception("Method on server " + i.ToString() + "." + method.Name + " took longer than " + msTimeout + " ms timeout. Aborting.");
                }

                returnValue = invoke_parameters.returnValue;

                if (invoke_parameters.runException != null)
                {
                    throw new Exception("Caught an exception when attempting to run method " + i.ToString() + "." + method.Name + ": " + invoke_parameters.runException.Message);
                }
            }
            catch (Exception e)
            {
                if (messageLog != null)
                {
                    messageLog(this, new MessageEvent("Caught exception when attempting to run method on server " + i.ToString() + ": " + e.Message + e.StackTrace));
                }

                serverGotDisconnectedInError(i);
                return(ServerActionStatus.Failed_Loss_Of_Connection);
            }

            if (returnValue is bool)
            {
                bool attempt = (bool)returnValue;
                if (attempt)
                {
                    return(ServerActionStatus.Success);
                }
                return(ServerActionStatus.Failed_On_Server);
            }
            else if (returnValue is ServerCommunicator.BufferGenerationStatus)
            {
                ServerCommunicator.BufferGenerationStatus bufferStatus = (ServerCommunicator.BufferGenerationStatus)returnValue;
                if (bufferStatus == ServerCommunicator.BufferGenerationStatus.Success)
                {
                    return(ServerActionStatus.Success);
                }
                return(ServerActionStatus.Failed_On_Server);
            }

            return(ServerActionStatus.Success);
        }
示例#5
0
        public bool pingServer(ServerInfo server, EventHandler <MessageEvent> messageLog)
        {
            if (!communicators.ContainsKey(server))
            {
                if (messageLog != null)
                {
                    messageLog(this, new MessageEvent("Attempted to ping unconnected server " + server.ToString()));
                }
                return(false);
            }

            ServerCommunicator comm = communicators[server];

            if (comm == null)
            {
                if (messageLog != null)
                {
                    messageLog(this, new MessageEvent("Server communication object is null, unable to ping server " + server.ToString()));
                }
                return(false);
            }

            try
            {
                Thread pingThread = new Thread(new ParameterizedThreadStart(ping_server_proc));
                pingThread.Start(comm);
                pingThread.Join(3000);
                if (pingThread.ThreadState == ThreadState.Running)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent("Server ping took longer than 1000ms. Aborting."));
                    }

                    pingThread.Abort();

                    return(false);
                }
            }
            catch (Exception e)
            {
                if (messageLog != null)
                {
                    messageLog(this, new MessageEvent("Caught exception when attempting to ping server " + server.ToString() + ": " + e.Message + e.StackTrace));
                }
                return(false);
            }

            return(true);
        }
示例#6
0
        public bool connectServer(ServerInfo server, EventHandler <MessageEvent> messageLog)
        {
            if (messageLog != null)
            {
                messageLog(this, new MessageEvent("Attempting to connect server " + server.ToString()));
            }

            bool gotLock = Monitor.TryEnter(lockObj, 1000);

            try
            {
                if (!gotLock)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent("Unable to obtain lock on servermanager. Aborting attempt."));
                    }
                    return(false);
                }

                // Cannot connect to a server that isn't is the servers list.
                if (!servers.Contains(server))
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is not on list of known servers. Aborting."));
                    }
                    return(false);
                }

                // ensure that this server has a server connection status item
                if (!connections.ContainsKey(server))
                {
                    connections.Add(server, ConnectionStatus.Disconnected_Normal);
                }

                // if this server is disabled, then don't connect to it.
                if (!server.ServerEnabled)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is not enabled. Aborting."));
                    }
                    return(false);
                }

                // set server status to connecting. If the server already was connecting, then return.
                if (connections[server] == ConnectionStatus.Connecting)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent(server.ToString() + " is already connecting. Aborting."));
                    }
                    return(false);
                }

                connections[server] = ConnectionStatus.Connecting;


                // create an empty spot for the server communicator
                if (!communicators.ContainsKey(server))
                {
                    communicators.Add(server, null);
                }

                communicators[server] = null;


                // try to create the communicator
                try
                {
                    Thread connectThread = new Thread(new ParameterizedThreadStart(server_connect_proc));
                    connectThread.Start(server);

                    connectThread.Join(1000);
                    if (connectThread.ThreadState == ThreadState.Running)
                    {
                        connectThread.Abort();
                        if (messageLog != null)
                        {
                            messageLog(this, new MessageEvent("Connection took longer than 1000 ms, aborting."));
                        }

                        connectThread.Abort();
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    if (messageLog != null)
                    {
                        messageLog(this, new MessageEvent("Caught exception when attempting to connect to server " + server.ToString() + ":" + e.Message + e.StackTrace));
                    }
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return(false);
                }

                try
                {
                    if (pingServer(server, messageLog))
                    {
                        server.setServerName(communicators[server].getServerName());

                        if (connectedServerNames.Contains(server.ServerName))
                        {
                            connections[server] = ConnectionStatus.Error_Name_Not_Unique;
                            communicators.Remove(server);
                            return(false);
                        }

                        connectedServerNames.Add(server.ServerName);
                        connections[server] = ConnectionStatus.Connected;
                        messageLog(this, new MessageEvent("Server connected successfully."));
                        return(true);
                    }
                    else
                    {
                        if (messageLog != null)
                        {
                            messageLog(this, new MessageEvent("Server ping failed. Aborting."));
                        }
                        connections[server] = ConnectionStatus.Unable_To_Connect;
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    messageLog(this, new MessageEvent("Exception when attempting to ping server: " + e.Message + e.StackTrace));
                    connections[server] = ConnectionStatus.Unable_To_Connect;
                    return(false);
                }
            }
            finally
            {
                if (gotLock)
                {
                    Monitor.Exit(lockObj);
                }
            }
            return(true);
        }