Advertisement

[java] Handling commands in a multiuser environment

Started by February 28, 2001 04:19 PM
7 comments, last by Spyder 23 years, 11 months ago
I''m working on a multiuser application. There will be a large number of players connected. Each player will have access to different commands. All in all I think the app will have 500 unique commands. I would like some advice.
I''m thinking of storing commands in a hashmap. Key=command and Value=object. The object performs the command, also based on arguments if there are any. Since every player will have it''s own setup of available commands I''m thinking they should have a similar list, but instead the value is a reference to the same object used in the fat hashmap. Any suggestions? Is this a good approach?
Think about using roles. A role will be a collection of permissions to perform a command. Each user can have one or more roles.

Try an array of objects instead of a hashmap. The key would be public static integers.

private static Commands[] = new Commands[500];
public static int COMMAND_1=1;
public static int COMMAND_2=2;
.
.
public static int COMMAND_500=500;

I would like to know more about your design.

[<><]
Advertisement
Thanks for the reply Tunabox.

How would this implementation work?
A player would have an int array populated with the static command ID''s? Why do you propose a static id&array instead of a hashmap? The problems that could arrise is that if new commands are added at runtine there would be a problem creating the new static ID?
I actually wrote this reply a bit ago, but the server was down, and I didn''t see tunabox''s reply - but it is along the same vain
---
My personal approach has bene the following when doing applications like this -

Instead of sending a string, set up a class that simply holds a series of public static final ints, with variable names that describe the actions you want.

(If you C++ code, this just becomes a variation on Enum''s)

(not knowing what level your at)

So you would have -

public abtract class GlobalCommands
{
public static final int LOGIN = 1;
public static final int LOGOUT = 2;
public static final int DO_THIS = 3;
....
}

This can be a long and tedious process, however, it does mean a few things -

1) You get less network traffic, as your only sending ints, and not strings
2) You can still set up a very readable case statement, and are not restricted as to what object you wish to call.
4)You don''t have to create a massive array of hashtable at startup time.
5) If you still want to use the Object to call the command, you can use an integer index on an array, which is better performance. (personally I prefer the case statement, but thats just me)
6) You can also pass this class across to your client, and then implement from there, knowing that the variables are the same.

Okay, thats my 2 bits worth

Mark
<< ...and here is one we developed earlier... >>
If commands are added during runtime then a HashMap should be considered.

Array -> fastest retrieval, set size
HashMap -> fast retrieval, dynamic size

How are you going to add commands during runtime?
I've also had a hard time replying and this BBS is _very_ slow. Again thanks for the replies. This can turn out to be an interesting discussion. (at least for me)

RE: Neurotic

quote:
1) You get less network traffic, as your only sending ints, and not strings

This is what I'm doing, the command is really an int. So the hash is id:object

quote:
2) You can still set up a very readable case statement, and are not restricted as to what object you wish to call.

I'm very sceptic when it comes to case and if statements. I really try to avoid them as much as possible. In my ears 500 case sounds very slow and bulky. Atm I'm using this I would like some feedback or optimization tips:
    final static void parseCmnd(Client client, String cmd, String params[]){<br>	CmdAbstractClass obj=(CmdAbstractClass) cmds.get(cmd);<br>			if (obj==null){<br>		// not implemented<br>	}else{obj.execute(client,params);}}<br>    

Every command extends the abstract class: CmdAbstractClass and defines the method execute. cmds is the hashmap.



RE: Tunabox

quote:
How are you going to add commands during runtime?

Admins will have a command to add commands. To be honest I havent thought to much about it. I just like a dynamic system.


Edited by - spyder on March 4, 2001 7:49:17 AM

Edited by - spyder on March 4, 2001 7:57:31 AM
Advertisement
I suspect the weak point in the code example is:

CmdAbstractClass obj=(CmdAbstractClass) cmds.get(cmd)

The conversion is prolly time consuming or am I just superstitious? Is there a faster/better way to do this?

Hello,

I am kinda facing the same problem. The way I am thinking on solving it is simpy using a Java mechanism.

Each command will have a class with the same name as the command, then I would simply just try and access the class with the same name as the command (which would implement/extend a Command base class) and allow it to do the work.
If I fail to access the class I would assume that we have a syntax error of some sort.

Advantages:
@) Easy to add new commands
@) Easy to understand
@) No big ugly case statement for parsing commands
@) Fast? This is the big question, will it be fast enough? Im considering using static methods to acctually avoid having instances of the classes hanging around clogging the system resources of the server

Any thoughts on this?

cheers,

Jens
// Jens
Jens

I guess you would need exception handling for that? From what I''ve read avoid exception handling as much as possible if you are worried about speed. I could be wrong. Your other 3 advantages seem solid to me.

This topic is closed to new replies.

Advertisement