QuickServer Development Guide (5) - Customer Data

xiaoxiao2021-03-06  41

You cannot save customer data in the ClientCommandrandler and ServeraThenticator classes, we use the ClientData class's HandleCommand () or AskAuthorisation () method to store all client information.

Demonstration This feature is used. Or use eChoServer as an example, when the user sends "Hello", we give him a greeting. If the user sends "Hello", we remind him that it has sent N times "Hello". Next, the ClientData class will store the username and the number of times he sends "Hello" to the server.

Code

1. Create an EchoServerData class in Echoserver

01 / / ---- EchoserverData.java ------

02 Package EchoServer;

03

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

05 import java.io. *;

06 07 Public Class EchoServerData Implements ClientData {

08 Private Int hellocount;

09 private string username;

10

11 public void sethellocount (int count) {

12 hellocount = count;

13}

14 public int getHellocount () {

15 returnhemou;

16}

In one

18 public void setusername (String username) {

19 this.username = username;

20}

21 public string getusername () {

22 Return UserName;

twenty three }

twenty four }

25 // --- end of code ---

2. Tell QuickServer to use this EchoserverData to do its CLIENTDATA class.

Modify the echoserver.java created in front, the 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.echoserverdata";

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 try {

22 myserver.startServer ();

23} catch (APPEXCEPTION E) {24 System.Out.println ("Error In Server:" E); 25}

26}

27}

In the above code, we write the configuration information to the String object to set QuickServer.

3. Modify the Authenticator class, which is the EchoServerAuthenTicator class, allowing it to store usernames in the ClientData object. Below is the modified code:

01 Package EchoServer;

02

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

04 Import java.io. *;

05

06 Public Class EchoServerquickAuthenticator Extends QuickAuthenTicator {

07

08 Public Boolean Askauthorisation (ClientHandler Clienthandler)

09 throws oException {

10 string username = askSTRINPUT (ClientHandler, "User Name:");

11 IF (username! = Null && username.equalsignorecase ("quit")) {

12 Sendstring (ClientHandler, "Logged Out.");

13 // Close The Connection

14 ClientHandler.CloseConnection ();

15 return false;

16}

In one

18 string password = askSTRINPUT (ClientHandler, "Password:");

19

20 IF (username == null || Password == null)

21 Return False;

twenty two

23 IF (username.equals (password)) {

24 Sendstring (ClientHandler, "Auth OK");

25 // Store The UserName in ClientData

26 echoserverdata data = (echoserverdata) ClientHandler.getClientData ();

27 Data.SetUsername (username);

28 return true;

Else {

30 Sendstring (ClientHandler, "Auth Failed");

31 RETURN FALSE;

32}

33}

34}

4. Modify the ClientCommandrandler implementation class echocommandhandler. If the user sends "Hello", give him a greeting. If he sends multiple "Hello", tell him that I have sent N times "Hello". Below is the modified code:

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} IF (Command.Equalsignorecase ("Hello")) {

36 echoserverdata data = (echoserverdata) Handler.getClientData ();

37 data.sethellocount (data.gethellocount () 1);

38 if (Data.getHellocount () == 1) {

39 Handler.sendClientmsg ("Hello" Data.GetuserName ());

40} else {

41 Handler.sendClientmsg ("You Told Hello" Data.GetHellocount () 42 "Times"); 43}

Else {

45 Handler.sendClientmsg ("Echo:" Command;

46}

47}

48}

5. Compile the program, run, use the sockettest test. After logging in, send "Hello", the system will give a greeting, send "Hello" again, it will tell you how many "hello" sent.

4.2 Creating a ClientData Pool

Now we know that ClientData can work normally. However, for each client connected to QuickServer, create a new ClientData object, which may cause bottlenecks on performance, especially servers with higher performance requirements.

We can create a CLIENTDATA pool object, regardless of the client, use the same object. First implement the following interface:

Org.quickServer.util.pool.pooLableObject

Find a Quick Server API documentation You can find that PoolableObject has only two methods that must be implemented:

Org.apache.commons.pool.pooLableObjectFactory belongs to the usual factory method.

ISPOOLABLE () determines whether an object can become a pool object.

POOLABLEOBJECTFACTORY

Org.apache.commons.pool.pooLableObjectFactory interface contains the following methods:

o Void ActivateObject (Object Obj): Reinitialize an instance.

o Void DestroyObject (Object Obj): Destroy an instance that is no longer needed.

o Object makeObject (): Create an instance.

o Void passivateObject (Object Obj): Disable an instance of initialization.

o Boolean ValidateObject (Object Obj): Determine if an instance is safe.

We can extend an object-based implementation to create a "pool" object:

Org.apache.commons.pool.basepoolableObjectFactory

This class has only one abstract method MakeObject () and a validateObject () method, which only returns true.

Let's create an EchoServerPooLableData class.

01 // ---- EchoserverPooLableData.java ----

02 Package EchoServer;

03

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

05 import java.io. *;

06

07 Public Class EchoServerPooLableData

08 Extends EchoServerData

09 imports org.apache.commons.pool.pooLableObjectFactory {

10

11 public void activateObject (Object obj) {

12}

13 Public Void DestroyObject (Object Obj) {

14 if (obj == null) return;

15 PassivateObject (OBJ);

16 obj = null;

17}

18 Public Object MakeObject () {19 Return New EchoServerPooLableData ();

20}

21 Public Void PassivateObject (Object Obj) {

22 EchoserverPooLableData Pd = (EchoServerPooLableData) OBJ;

23 pd.sethellocount (0);

24 pd.setusername (null);

25}

26 Public Boolean ValidateObject (Object Obj) {

27 if (Obj == Null)

28 returnaf;

29 else

30 return true;

31}

32}

33 / / --- End of code ---

This class expands our EchoserverData, and then we implemented org.apache.commons.pool.basepooLableObjectFactory, this implementation is simple and unsuitable.

Now we need to tell QuickServer to use this class to replace the original ClientData class.

MyServer.setClientData ("EchoServer.echoServerPooLableData);

Compiling and modifying programs may be reported as follows:

Package org.apache.commons.pool does not exist.

This is because the compiler does not know this class. You can add D: /quickServer/dist/commons-pool.jar package in an environment variable, and run at runtime

Set classpath =% classpath%; D: /quickServer/dist/quickServer.jar; D: /quickServer/dist/commons-pool.jar ;. (The class is located).

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

New Post(0)