/// <summary> /// Retrieve information about connected Clients of the Server we are /// connected to. /// </summary> /// <param name="ARemote">Instance of remote ServerManager</param> /// <param name="ATotalConnectedClients">Total number of Clients that /// have been connected while the PetraServer has been running.</param> /// <param name="ACurrentlyConnectedClients">Number of currently /// connected Clients.</param> static void RetrieveConnectedClients(IServerAdminInterface ARemote, out int ATotalConnectedClients, out int ACurrentlyConnectedClients) { ATotalConnectedClients = -1; ACurrentlyConnectedClients = -1; try { ATotalConnectedClients = ARemote.ClientsConnectedTotal; ACurrentlyConnectedClients = ARemote.ClientsConnected; } catch (RemotingException remexp) { HandleConnectionError(remexp); Environment.Exit(0); // PetraServerAdminConsole application stops here !!! } catch (System.Net.Sockets.SocketException remexp) { HandleConnectionError(remexp); Environment.Exit(0); // PetraServerAdminConsole application stops here !!! } catch (System.Exception) { throw; } }
/// <summary> /// disconnect a client /// </summary> /// <param name="TRemote"></param> /// <param name="ConsoleInput"></param> public static void DisconnectClient(IServerAdminInterface TRemote, String ConsoleInput) { Int16 ClientID; String CantDisconnectReason; try { ClientID = System.Int16.Parse(ConsoleInput); if (TRemote.DisconnectClient(ClientID, out CantDisconnectReason)) { TLogging.Log("Client #" + ClientID.ToString() + ": disconnection will take place shortly."); } else { TLogging.Log("Client #" + ClientID.ToString() + " could not be disconnected on admin request. Reason: " + CantDisconnectReason); } } catch (System.FormatException) { Console.WriteLine(" Entered ClientID is not numeric!"); } catch (Exception exp) { TLogging.Log( Environment.NewLine + "Exception occured while trying to disconnect a Client on admin request:" + Environment.NewLine + exp.ToString()); } }
/// <summary> /// shut down the server (gets all connected clients to disconnect) /// </summary> /// <param name="TRemote"></param> /// <param name="AWithUserInteraction"></param> /// <returns>true if shutdown was completed</returns> public static bool ShutDownControlled(IServerAdminInterface TRemote, bool AWithUserInteraction) { bool ReturnValue; bool ack; ack = false; if (AWithUserInteraction == true) { Console.WriteLine(Environment.NewLine + "-> CONTROLLED SHUTDOWN (gets all connected clients to disconnect) <-"); Console.Write(" Enter YES to perform shutdown (anything else to leave command): "); if (Console.ReadLine() == "YES") { Console.WriteLine(); ack = true; } } else { ack = true; } if (ack == true) { TLogging.Log("CONTROLLED SHUTDOWN PROCEDURE INITIATED..."); try { if (!TRemote.StopServerControlled(true)) { Console.WriteLine(" Shutdown cancelled!"); Console.Write(ServerAdminPrompt); ReturnValue = false; } } catch (SocketException) { if (AWithUserInteraction == true) { Console.WriteLine(); TLogging.Log("SERVER STOPPED!"); Console.WriteLine(); Console.Write("Press ENTER to end PETRAServerADMIN..."); Console.ReadLine(); } } ReturnValue = true; } else { Console.WriteLine(" Shutdown cancelled!"); Console.Write(ServerAdminPrompt); ReturnValue = false; } return(ReturnValue); }
/// <summary> /// shut down the server (gets all connected clients to disconnect) /// </summary> /// <param name="TRemote"></param> /// <param name="AWithUserInteraction"></param> /// <returns>true if shutdown was completed</returns> public static bool ShutDownControlled(IServerAdminInterface TRemote, bool AWithUserInteraction) { bool ReturnValue; bool ack; ack = false; if (AWithUserInteraction == true) { Console.WriteLine(Environment.NewLine + "-> CONTROLLED SHUTDOWN (gets all connected clients to disconnect) <-"); Console.Write(" Enter YES to perform shutdown (anything else to leave command): "); if (Console.ReadLine() == "YES") { Console.WriteLine(); ack = true; } } else { ack = true; } if (ack == true) { TLogging.Log("CONTROLLED SHUTDOWN PROCEDURE INITIATED..."); try { if (!TRemote.StopServerControlled(true)) { Console.WriteLine(" Shutdown cancelled!"); Console.Write(ServerAdminPrompt); ReturnValue = false; } } catch (SocketException) { if (AWithUserInteraction == true) { Console.WriteLine(); TLogging.Log("SERVER STOPPED!"); Console.WriteLine(); Console.Write("Press ENTER to end PETRAServerADMIN..."); Console.ReadLine(); } } ReturnValue = true; } else { Console.WriteLine(" Shutdown cancelled!"); Console.Write(ServerAdminPrompt); ReturnValue = false; } return ReturnValue; }
/// <summary> /// Displays information about the Server we are connected to. /// </summary> /// <param name="AServerManager">Instance of remote ServerManager</param> void DisplayPetraServerInformation(IServerAdminInterface AServerManager) { Console.WriteLine(AServerManager.ServerInfoVersion); Console.WriteLine(Catalog.GetString("Configuration file: " + AServerManager.ConfigurationFileName)); Console.WriteLine(" " + String.Format(Catalog.GetString("Client connections since Server start: {0}"), AServerManager.ClientsConnectedTotal)); Console.WriteLine(" " + String.Format(Catalog.GetString("Clients currently connected: "), AServerManager.ClientsConnected)); Console.WriteLine(AServerManager.ServerInfoState); }
/// <summary> /// todoComment /// </summary> /// <param name="ConfigFile"></param> /// <param name="iRemote"></param> public void GetServerConnection(string ConfigFile, out IServerAdminInterface iRemote) { iRemote = null; try { if (TAppSettingsManager.HasValue("Server.Port")) { DetermineServerIPAddress(); IClientChannelSinkProvider TCPSink = new BinaryClientFormatterSinkProvider(); if (TAppSettingsManager.HasValue("Server.ChannelEncryption.PublicKeyfile")) { Hashtable properties = new Hashtable(); properties.Add("FilePublicKeyXml", TAppSettingsManager.GetValue("Server.ChannelEncryption.PublicKeyfile")); TCPSink.Next = new EncryptionClientSinkProvider(properties); } Hashtable ChannelProperties = new Hashtable(); TcpChannel Channel = new TcpChannel(ChannelProperties, TCPSink, null); ChannelServices.RegisterChannel(Channel, false); RemotingConfiguration.RegisterWellKnownClientType( typeof(IServerAdminInterface), String.Format("tcp://{0}:{1}/Servermanager", FServerIPAddr, FServerPort)); } else { RemotingConfiguration.Configure(ConfigFile, false); DetermineServerIPAddress(); } iRemote = (IServerAdminInterface) Activator.GetObject(typeof(IServerAdminInterface), String.Format("tcp://{0}:{1}/Servermanager", FServerIPAddr, FServerPort)); if ((iRemote != null) && (TLogging.DebugLevel > 0)) { TLogging.Log(("GetServerConnection: connected.")); } } catch (Exception exp) { TLogging.Log(("Error in GetServerConnection(), Possible reasons :-" + exp.ToString())); throw; } }
/// <summary> /// Displays information about the Server we are connected to. /// </summary> /// <param name="AServerManager">Instance of remote ServerManager</param> static void DisplayPetraServerInformation(IServerAdminInterface AServerManager) { int TotalConnectedClients; int CurrentlyConnectedClients; RetrieveConnectedClients(AServerManager, out TotalConnectedClients, out CurrentlyConnectedClients); TLogging.Log(AServerManager.ServerInfoVersion); TLogging.Log(Catalog.GetString("Configuration file: " + AServerManager.ConfigurationFileName)); TLogging.Log(" Clients connections since Server start: " + TotalConnectedClients.ToString()); TLogging.Log(" Clients currently connected: " + CurrentlyConnectedClients.ToString()); TLogging.Log(AServerManager.ServerInfoState); }
private static void RestoreDatabase(IServerAdminInterface TRemote) { Console.WriteLine(Environment.NewLine + "-> DELETING YOUR DATABASE <-"); Console.Write(" Enter YES to import the new database (anything else to leave command): "); if (Console.ReadLine() == "YES") { Console.Write(" Please enter filename of yml.gz file: "); string restoreFile = Console.ReadLine(); RestoreDatabase(TRemote, restoreFile); } else { Console.WriteLine(" Reset of database cancelled!"); } }
private static void ExportDatabase(IServerAdminInterface TRemote) { Console.Write(" Please enter filename of yml.gz file: "); string backupFile = Path.GetFullPath(Console.ReadLine()); if (!backupFile.EndsWith(".yml.gz")) { Console.WriteLine("filename has to end with .yml.gz. Please try again"); return; } string YmlGZData = TRemote.BackupDatabaseToYmlGZ(); FileStream fs = new FileStream(backupFile, FileMode.Create); byte[] buffer = Convert.FromBase64String(YmlGZData); fs.Write(buffer, 0, buffer.Length); fs.Close(); TLogging.Log("backup has been written to " + backupFile); }
private static bool RestoreDatabase(IServerAdminInterface TRemote, string ARestoreFile) { string restoreFile = Path.GetFullPath(ARestoreFile); if (!File.Exists(restoreFile) || !restoreFile.EndsWith(".yml.gz")) { Console.WriteLine("invalid filename, please try again"); return(false); } string YmlGZData = string.Empty; try { FileStream fs = new FileStream(restoreFile, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); YmlGZData = Convert.ToBase64String(buffer); } catch (Exception e) { TLogging.Log("cannot open file " + restoreFile); TLogging.Log(e.ToString()); return(false); } if (TRemote.RestoreDatabaseFromYmlGZ(YmlGZData)) { TLogging.Log("backup has been restored from " + restoreFile); return(true); } else { TLogging.Log("there have been problems with the restore"); return(false); } }
/// reset the static variables for each Web Request call. public static void ResetStaticVariables() { TheServerManager = null; FDBReconnectionAttemptsCounter = -1; FServerTimedProcessingSetup = false; }
private static void AddUser(IServerAdminInterface TRemote, string AUserId) { TRemote.AddUser(AUserId); }
/// <summary> /// shows the menu and processes the selections of the administrator /// </summary> /// <param name="TRemote"></param> public static void Menu(IServerAdminInterface TRemote) { bool ReadLineLoopEnd; bool EntryParsedOK; String ServerAdminCommand; String ConsoleInput; String ClientTaskCode; String ClientTaskGroup; System.Int16 ClientID = 0; // assignment only to make code compile; has no functional implication System.Int16 ClientTaskPriority = 1; // assignment only to make code compile; has no functional implication // label // ReadClientID, used only for repeating invalid command line input // ReadClientTaskPriority; used only for repeating invalid command line input DisplayPetraServerInformation(TRemote); // // Startup done. // From now on just listen on menu commands... // Console.WriteLine(Environment.NewLine + "-> Press \"m\" for menu."); Console.Write(ServerAdminPrompt); // ServerAdmin stops after leaving the following loop...! ReadLineLoopEnd = false; do { ServerAdminCommand = (Console.ReadLine()); if (ServerAdminCommand.Length > 0) { ServerAdminCommand = ServerAdminCommand.Substring(0, 1); switch (Convert.ToChar(ServerAdminCommand)) { case 'm': case 'M': Console.WriteLine(Environment.NewLine + "-> Available commands <-"); Console.WriteLine(" c: list connected Clients / C: list disconnected Clients"); Console.WriteLine(" d: disconnect a certain Client"); Console.WriteLine(" p: perform timed server processing manually now"); Console.WriteLine(" q: queue a Client Task for a certain Client"); Console.WriteLine(" s: Server Status"); if (TLogging.DebugLevel > 0) { Console.WriteLine(" y: show Server memory"); Console.WriteLine(" g: perform Server garbage collection (for debugging purposes only!)"); } Console.WriteLine(" e: export the database to yml.gz"); Console.WriteLine(" i: import a yml.gz, which will overwrite the database"); #if DEBUG Console.WriteLine(" r: Mark all Cached Tables for Refreshing"); #endif Console.WriteLine(" o: controlled Server shutdown (gets all connected clients to disconnect)"); Console.WriteLine(" u: unconditional Server shutdown (forces 'hard' disconnection of all Clients!)"); Console.WriteLine(" x: exit PETRAServerADMIN"); Console.Write(ServerAdminPrompt); break; case 'c': Console.WriteLine(Environment.NewLine + "-> Connected Clients <-"); Console.WriteLine(TRemote.FormatClientList(false)); Console.Write(ServerAdminPrompt); break; case 'C': Console.WriteLine(Environment.NewLine + "-> Disconnected Clients <-"); Console.WriteLine(TRemote.FormatClientList(true)); Console.Write(ServerAdminPrompt); break; case 'd': case 'D': Console.WriteLine(Environment.NewLine + "-> Disconnect a certain Client <-"); if (TRemote.ClientList.Count > 0) { Console.WriteLine(TRemote.FormatClientList(false)); Console.Write(" Enter ClientID: "); ConsoleInput = Console.ReadLine(); DisconnectClient(TRemote, ConsoleInput); } else { Console.WriteLine(" * no Clients connected *"); } Console.Write(ServerAdminPrompt); // queue a Client Task for a certain Client break; case 'e': case 'E': Console.WriteLine(Environment.NewLine + "-> Export the database to yml.gz file <-"); ExportDatabase(TRemote); Console.Write(ServerAdminPrompt); // queue a Client Task for a certain Client break; case 'i': case 'I': Console.WriteLine(Environment.NewLine + "-> Restore the database from yml.gz file <-"); RestoreDatabase(TRemote); Console.Write(ServerAdminPrompt); // queue a Client Task for a certain Client break; case 'r': case 'R': Console.WriteLine(Environment.NewLine + "-> Marking all Cached Tables for Refreshing... <-"); RefreshAllCachedTables(TRemote); Console.Write(ServerAdminPrompt); break; case 'p': case 'P': string resp = ""; Console.WriteLine(" Server Timed Processing Status: " + "runs daily at " + TRemote.TimedProcessingDailyStartTime24Hrs + "."); Console.WriteLine(" Partner Reminders: " + (TRemote.TimedProcessingJobEnabled("TProcessPartnerReminders") ? "On" : "Off")); Console.WriteLine(" Automatic Intranet Export: " + (TRemote.TimedProcessingJobEnabled("TProcessAutomatedIntranetExport") ? "On" : "Off")); Console.WriteLine(" Data Checks: " + (TRemote.TimedProcessingJobEnabled("TProcessDataChecks") ? "On" : "Off")); Console.WriteLine(" SMTP Server used for sending e-mails: " + TRemote.SMTPServer); if (TRemote.TimedProcessingJobEnabled("TProcessPartnerReminders")) { Console.WriteLine(""); Console.WriteLine("Do you want to run Reminder Processing now?"); Console.Write("Type YES to continue, anything else to skip:"); resp = Console.ReadLine(); if (resp == "YES") { TRemote.PerformTimedProcessingNow("TProcessPartnerReminders"); } } if (TRemote.TimedProcessingJobEnabled("TProcessAutomatedIntranetExport")) { Console.WriteLine(""); Console.WriteLine("Do you want to run Intranet Export Processing now?"); Console.Write("Type YES to continue, anything else to skip:"); resp = Console.ReadLine(); if (resp == "YES") { TRemote.PerformTimedProcessingNow("TProcessAutomatedIntranetExport"); } } if (TRemote.TimedProcessingJobEnabled("TProcessDataChecks")) { Console.WriteLine(""); Console.WriteLine("Do you want to run Data Checks Processing now?"); Console.Write("Type YES to continue, anything else to skip:"); resp = Console.ReadLine(); if (resp == "YES") { TRemote.PerformTimedProcessingNow("TProcessDataChecks"); } } Console.Write(ServerAdminPrompt); break; case 's': case 'S': Console.WriteLine(Environment.NewLine + "-> Server Status <-"); Console.WriteLine(); DisplayPetraServerInformation(TRemote); Console.Write(ServerAdminPrompt); break; case 'q': case 'Q': Console.WriteLine(Environment.NewLine + "-> Queue a Client Task for a certain Client <-"); if (TRemote.ClientList.Count > 0) { Console.WriteLine(TRemote.FormatClientList(false)); // ReadClientID: Console.Write(" Enter ClientID: "); ConsoleInput = Console.ReadLine(); EntryParsedOK = false; try { ClientID = System.Int16.Parse(ConsoleInput); EntryParsedOK = true; } catch (System.FormatException) { Console.WriteLine(" Entered ClientID is not numeric!"); } if (!EntryParsedOK) { } // goto ReadClientID; Console.Write(" Enter Client Task Group: "); ClientTaskGroup = Console.ReadLine(); Console.Write(" Enter Client Task Code: "); ClientTaskCode = Console.ReadLine(); // ReadClientTaskPriority: Console.Write(" Enter Client Task Priority: "); ConsoleInput = Console.ReadLine(); ClientTaskPriority = -1; try { ClientTaskPriority = System.Int16.Parse(ConsoleInput); EntryParsedOK = true; } catch (System.FormatException) { Console.WriteLine(" Entered Client Task Priority is not numeric!"); EntryParsedOK = false; } if (!EntryParsedOK) { } // goto ReadClientTaskPriority; try { if (TRemote.QueueClientTask(ClientID, ClientTaskGroup, ClientTaskCode, ClientTaskPriority)) { TLogging.Log("Client Task queued for Client #" + ClientID.ToString() + " on admin request."); } else { TLogging.Log("Client Task for Client #" + ClientID.ToString() + " could not be queued on admin request."); } } catch (Exception exp) { TLogging.Log( Environment.NewLine + "Exception occured while queueing a Client Task on admin request:" + Environment.NewLine + exp.ToString()); } } else { Console.WriteLine(" * no Clients connected *"); } Console.Write(ServerAdminPrompt); break; case 'y': case 'Y': Console.WriteLine("Server memory: " + TRemote.ServerInfoMemory.ToString()); Console.Write(ServerAdminPrompt); break; case 'g': case 'G': GC.Collect(); Console.WriteLine("GarbageCollection performed. Server memory: " + TRemote.PerformGC().ToString()); Console.Write(ServerAdminPrompt); break; case 'o': case 'O': ReadLineLoopEnd = ShutDownControlled(TRemote, true); break; case 'u': case 'U': ReadLineLoopEnd = ShutDown(TRemote, true); break; case 'x': case 'X': // exit loop, ServerAdmin stops then ReadLineLoopEnd = true; break; default: Console.WriteLine(Environment.NewLine + "-> Unrecognised command '" + ServerAdminCommand + "' <- (Press 'm' for menu)"); Console.Write(ServerAdminPrompt); break; } // case Convert.ToChar( ServerAdminCommand ) } // if ServerAdminCommand.Length > 0 else { Console.Write(ServerAdminPrompt); } } while (!(ReadLineLoopEnd == true)); }
private static void RefreshAllCachedTables(IServerAdminInterface TRemote) { TRemote.RefreshAllCachedTables(); }
private static bool RestoreDatabase(IServerAdminInterface TRemote, string ARestoreFile) { string restoreFile = Path.GetFullPath(ARestoreFile); if (!File.Exists(restoreFile) || !restoreFile.EndsWith(".yml.gz")) { Console.WriteLine("invalid filename, please try again"); return false; } string YmlGZData = string.Empty; try { FileStream fs = new FileStream(restoreFile, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); fs.Close(); YmlGZData = Convert.ToBase64String(buffer); } catch (Exception e) { TLogging.Log("cannot open file " + restoreFile); TLogging.Log(e.ToString()); return false; } if (TRemote.RestoreDatabaseFromYmlGZ(YmlGZData)) { TLogging.Log("backup has been restored from " + restoreFile); return true; } else { TLogging.Log("there have been problems with the restore"); return false; } }