QuickServer Development Guide (6) - Remote Management Support

xiaoxiao2021-03-06  43

Our EchoServer may need to modify several server configuration parameters, such as timeout messages, maximum verification, maximum verification time. QuickServer supports these features without changing the code.

1. Use QSADMINSERVER

When we need a management server to control our servers, we don't need to modify the code or even turn off the running server. The implementation of this service is:

Org.quickServer.Net.Qsadmin.qsadminServer

To use its feature We want to call the QuickServer's StartQsadminserver () method. The default port of QsadminServer is 9876, we can use one of the following two ways to modify it:

SetQsadminServerPort (4124);

GetQsadminserver (). get Server (). setport (4124);

Below is the code that can run QsadminServer at the 4124 port of EchoServer:

01 Package EchoServer;

02

03 Import Org.quickServer.net. *;

04 Import Org.quickServer.net.server. *;

05

06 import java.io. *;

07

08 Public Class EchoServer {

09 public static void main (string s []) {

10

11 string cmd = "echoserver.echocommandhandler";

12 string auth = "echoserver.echoserverquickauthenticator";

13 string data = "echoserver.echoserverPooLableData"; // poolaable

14

15 Quick Server MyServer = New QuickServer (CMD);

16 MyServer.SetAuthenThenticator (Auth);

17 MyServer.SetClientData (data);

18

19 MyServer SetPort (4123);

20 myserver.setname ("Echo Server V 1.0"); 21

22 // Config QsadminServer

23 MyServer.SetqsadminServerPort (4124);

24 myserver.getqsadminserver (). GetServer (). SetName ("echoadmin v 1.0");

25 try {

26 myserver.startqsadminserver ();

27 myserver.startServer ();

28} catches (APPEXCEPTION E) {

29 System.out.println ("Error IN Server:" E);

30}

31}

32}

33

In this way, we defined our QSADMINSERVER running at 4124 port. But how do it verify? If there is no external verifier, QsadminServer will use org.quickServer.net.Qsadmin.AuthenTicator as its validator. Check the API document We can find it very simple, username and password are hard coded as:

Username: admin

Password: QSADM1N

Now let's compile new programs and run, the console will output information to illustrate the server running on the 4124 port.

Running qsadmingui.bat in the bin directory in the QuickServer installation directory, Qsadmin's interface is now connected to the management server. Click on the "login" button in the upper right corner to pop up a login dialog box, enter the IP address and server running port: 127.0.0.1, 4124, because we use the default username password, click "Login" login.

Welcome and login success information will be displayed after logging in. You can now select the target server (TARGET) of the command and the operation shown in the left command button.

TARGET is based on the server that sends commands because we currently have two servers echoserver and echoadmin.

There is a text box that can be sent directly to the server directly to the server, and the command can refer to Qsadmin Command Handler:

Org.quickServer.Net.Qsadmin.commandhandler API documentation

To change the configuration of the server, you can click "Get / Set" bookmark, just select "Target", click "Reload Properties for the Target" to reload the configuration of the target server. Modify the parameters, then click "Save". The entry into force of some parameters requires restarting the server, but this does not disconnect the connection to the client.

2. Add your own command

As mentioned earlier If we want to change some of the properties of some servers, we do not need to modify existing code or restart the server, we can add your own command to manage the server.

Let us use an example to demonstrate this feature. When the user sends "What's Interest" (how much interest is, we will display the current interest rate. This number is stored in an object. If necessary, you can change it if you need to restart the server. We said it is variable and we need to change it when the server is running.

Let's implement Command Handler to show interest rates. The first thing to change Echoserver and Command Handler. Below is the code:

01 Package EchoServer;

02

03 Import Org.quickServer.net. *;

04 Import Org.quickServer.net.server. *;

05

06 import java.io. *;

07

08 Public Class EchoServer {

09 public static void main (string s []) {

10

11 string cmd = "echoserver.echocommandhandler";

12 string auth = "echoserver.echoserverquickauthenticator";

13 string data = "echoserver.echoserverPooLableData"; // poolaable

14

15 Quick Server MyServer = New QuickServer (CMD);

16 MyServer.SetAuthenThenticator (Auth);

17 MyServer.SetClientData (data);

18

19 MyServer.Setport (4123);

20 MyServer.setName ("Echo Server V 1.0");

twenty one

22 // Store Data Needed to Be Changd by qsadminserver

23 Object [] store = new object [] {"12.00"}; 24 MyServer setStoreObjects (store);

25

26 // Config QsadminServer

27 MyServer.SetqsadminServerport (4124);

28 myserver.getqsadminserver (). GetServer (). SetName ("echoadmin v 1.0");

29 try {

30 myserver.startqsadminserver ();

31 myServer.startServer ();

32} catches (APPEXCEPTION E) {

33 System.out.println ("Error In Server:" E);

34} 35}

36}

37

01 // echocommandhandler.java

02 Package EchoServer;

03

04 Import java.net. *;

05 import java.io. *;

06 Import Org.quick Server.Net.server.clientCommandHandler;

07 import org.quickServer.net.server.clienthandler;

08

09 Public Class Echocommandhandler Implements ClientCommandhandler {

10

11 public void gotconnected (ClientHandler Handler)

12 throws sockettimeOutexception, ioException {

13 Handler.sendClientmsg ( ");

14 Handler.sendClientmsg ("| Welcome to EchoServer V 1.0 |");

15 Handler.sendClientmsg ("| NOTE: Password = UserName |);

16 Handler.sendClientmsg ("| send 'quit' to exit |");

17 Handler.sendClientmsg ( ");

18}

19 Public void Lostconnection (ClientHandler Handler)

20 thrtows oException {

21 Handler.sendsystemmsg ("Connection Lost:"

22 Handler.getsocket (). Getinetaddress ());

twenty three }

24 Public Void ClosingConnection (ClientHandler Handler)

25 thrtows oException {

26 Handler.Sendsystemmsg ("Closing Connection:" 27 handler.getsocket (). Getinetaddress ());

28}

29

30 Public Void HandleCommand (ClientHandler Handler, String Command)

31 throws sockettimeoutException, ioException {

32 IF (Command.equals ("Quit")) {

33 Handler.sendClientmsg ("bye ;-)");

34 Handler.CloseConnection ();

35 return;

36}

37 IF (Command.equals ("What's Interest?")) {

38 Handler.sendClientmsg ("Interest IS:"

39 (String) Handler.getServer (). GetStoreObjects () [0]

40 "%"); 41} else IF (command.equalsignorecase ("hello")) {

42 EchoserverData Data = (EchoServerData) Handler.getClientData ();

43 Data.SetHellocount (data.gethellocount () 1);

44 IF (data.gethellocount () == 1) {

45 Handler.sendClientmsg ("Hello" Data.getuserName ());

46} else {

47 Handler.sendClientmsg ("You Told Hello" Data.GetHellocount ()

48 "Times");

49}

50} else {

51 Handler.sendClientmsg ("Echo:" Command;

52}

53}

54}

Next to add a Command plugin for QSADMINSERVER

01 Package EchoServer;

02

03

04 Import java.io. *;

05 import java.net.sockettimeoutexception;

06 Import Org.quickServer.Net.server. *;

07 Import Org.quickServer.Net.Qsadmin. *;

08

09 Public Class QsadminCommandPlugin ImtribrndPlugin {

10 / **

11 * eChoServer Commands

12 * ----------------------------------

13 * SET Interest Value

14 * GET Interest

15 * /

16 Public Boolean HandleCommand (ClientHandler Handler, String Command)

17 throws sockettimeoutException, ioException {

18

19 QuickServer EchoServer = (Quick Server)

20 Handler.getServer (). GetStoreObjects () [0];

21 Object Obj [] = echoserver.getstoreObjects ();

twenty two

23 IF (Command.tolowercase (). StartSwith ("SET Interest")) {24 string temp = "

25 Temp = command.substring ("set interest" .length ());

26 obj [0] = TEMP;

27 echoserver.setstoreObjects (OBJ);

28 Handler.sendClientmsg (" OK Interest Changed");

29 return true;

30} else if (Command.tolowerCase (). Equals ("Get Interest")) {

31 Handler.sendClientmsg (" OK" (String) Obj [0]);

32 return true;

33}

34 // ask qsadminserver to process the commit

35 returnaf false;

36}

37} In the above code, we use the following code to call the storage objects in the qsadminserver of EchoServer. (Refer to the QsadminServer and CommandPlugin section in the Quick Server API document).

QuickServer EchoServer = (Quick Server) Handler.getServer (). GetStoreObjects () [0];

If the process gets a command, returns true, otherwise returns false and indicates that the default command processor of QsadminServer will be used to handle the command. The same technique will be used to overwrite the default command of the QSADMISERVER command processor.

Let us tell QuickServer this class is a QSADMINSERVER's Command plugin. The modified EchoServer.java code is as follows:

01 Package EchoServer;

02

03 Import Org.quickServer.net. *;

04 Import Org.quickServer.net.server. *;

05

06 import java.io. *;

07

08 Public Class EchoServer {

09 public static void main (string s []) {

10

11 string cmd = "echoserver.echocommandhandler";

12 string auth = "echoserver.echoserverquickauthenticator";

13 string data = "echoserver.echoserverPooLableData"; // poolaable

14

15 Quick Server MyServer = New QuickServer (CMD);

16 MyServer.SetAuthenThenticator (Auth);

17 MyServer.SetClientData (data);

18

19 MyServer.Setport (4123);

20 MyServer.setName ("Echo Server V 1.0");

twenty one

22 // Store Data Needed to Be Changd by qsadminserver

23 Object [] store = new object [] {"12.00"};

24 MyServer.SetStoreObjects (Store);

25

26 // Config QsadminServer

27 MyServer.SetqsadminServerport (4124);

28 myserver.getqsadminserver (). GetServer (). SetName ("echoadmin v 1.0"); 29 try {

30 // Add Command Plugin

31 myserver.getqsadminserver (). Setcommandplugin

32 "EchoServer.QsadminCommandPlugin");

33 myserver startqsadminserver ();

34 myserver.startServer ();

35} catch (APPEXCEPTION E) {

36 System.out.println ("Error In Server:" E);

37} catch (exception e) {

38 System.out.println ("Error:" E);

39}

40}

41}

Compile code and run. Open the client, such as Sockettest, send command "what's interterest?", Will display "Interest IS: 12.00%".

Run Qsadmingui, type the following command in the Send Message box, send:

GET Interest

System output " ok 12.00"

Now modify this parameter, send the following command

Set Interest 15.00

System Output " OK Interest Changed"

Send command "What INTEREST?" Again at the client, and the client displays "Interest IS: 15.00%".

转载请注明原文地址:https://www.9cbs.com/read-52770.html

New Post(0)