|
|
| Author |
Message |
|
| Rechteck |
Posted: Sat Mar 03, 2007 8:59 am |
|
|
|
Joined: 03 Mar 2007
Posts: 2
|
First of all: Hello @ all!
Second: My Question:
What method shall I use to code a state machine?Code:
client (Socket, State) ->
case State of
a ->
DoSomething,
client(Socket, SomeNewStateLike_b_);
b ->
DoSomthingElse,
client(Socket, SomeNewStateLike_a_);
end,
orCode:
client (Socket, State) when State == a ->
DoSomething,
client(Socket, SomeNewStateLike_b_).
client (Socket, State) when State == b ->
DoSomthingElse,
client(Socket, SomeNewStateLike_a_).
The states are used for things like "login" "canplay" "play", i.e. the different states the server can be in. ("a" & "b" are obvioulsy just examples ;)
Or is there an even better way?
- Rechteck |
|
|
| Back to top |
|
| Mazen |
Posted: Wed Mar 07, 2007 10:05 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
Hi Rechteck,
First of all, you might want to consider looking at gen_fsm which is an implementation of a generic fsm (a behaviour). It should suite you perfectly. It can be found here: http://www.erlang.org/doc/doc-5.5.2/lib/stdlib-1.14.2/doc/html/gen_fsm.html
Now to answer your question. The second method is better. This is because of two reasons. 1) It is probably faster, and 2) The readability and structure of the code is far more comprehensive since it encapsulates the functionality of each state.
To consider...
Instead of writing
Code:
client (Socket, State) when State == a ->
DoSomething,
client(Socket, SomeNewStateLike_b_)
....
Then patternmatch the State directly in the head:
Code:
client (Socket, a) ->
DoSomething,
client(Socket, SomeNewStateLike_b_)
....
or if State is a record of many things:
Code:
client (Socket, #state{ state = a } = State) ->
DoSomething,
client(Socket, SomeNewStateLike_b_)
....
But I really must say using gen_fsm for this helps alot
Good luck
Rechteck wrote: First of all: Hello @ all!
Second: My Question:
What method shall I use to code a state machine? Code:
client (Socket, State) ->
case State of
a ->
DoSomething,
client(Socket, SomeNewStateLike_b_);
b ->
DoSomthingElse,
client(Socket, SomeNewStateLike_a_);
end,
or Code:
client (Socket, State) when State == a ->
DoSomething,
client(Socket, SomeNewStateLike_b_).
client (Socket, State) when State == b ->
DoSomthingElse,
client(Socket, SomeNewStateLike_a_).
The states are used for things like "login" "canplay" "play", i.e. the different states the server can be in. ("a" & "b" are obvioulsy just examples
Or is there an even better way?
- Rechteck |
|
|
| Back to top |
|
| Rechteck |
Posted: Sun Mar 11, 2007 4:39 pm |
|
|
|
Joined: 03 Mar 2007
Posts: 2
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|