3

I have a state machine that I would like to model using a scalaz-stream Process1.

The state machine models the message flow between a client and a server.

A basic set of data types might be:

sealed trait ServerState
case object Disconnected extends ServerState
case object Authenticating extends ServerState
case object Idle extends ServerState
case object Busy extends ServerState

sealed trait Message
case object Query extends Message
case object StartupMessage extends Message
case object AuthenticationOk extends Message
case object QueryResponse extends Message

In my head this would be modeled by a Process1[I, O], where type I = Messageand type O = scalaz.State[Message, ServerState].

It's like being a fog - I can see the outline of a solution but the firm definition of it escapes me.

Currently I have something that looks like this

type Transition = scalaz.State[Message, ServerState]
val connecting = Transition { StartupMessage => (StartupMessage, Authenticating) }
def fsm(state: Transition): Process1[Message, Transition] = {
  Await [Message, Transition] { msg =>
    case (connecting, AuthenticationOk) => Emit1(connecting) 
  }
}

I know this is wrong but I can't figure out where the State Transitions need to live.

Should the Process accept a Message and return a physical ServerState while the Process1 driver handles the internal state?

I'm struggling to see how to "carry along" a message that cannot be executed at this time.

Example:

1. Current ServerState = Disconnected
2. StateMachine gets Query Message
3. StateMachine must send StartupMessage, ServerState now equals = Authenticating
4. StateMachine receives AuthenticationOk, ServerState now equals Idle
5. StateMachine must now sends original query message, ServerState now equals Busy
6. StateMachine gets QueryResponse, ServerState now equals Idle

1 Answer 1

3

I think you should be able to encode your state machine through a recursive Process1[Message, Message] like this

def fsm(state: ServerState): Process1[Message, Message] = {
  receive1 { msg: Message =>
    (msg, state) match {
      case (Query, Disconnected) => 
        emit(StartupMessage) fby fsm(Authenticating)
       case (AuthenticationOk, Authenticating) => 
         fsm(Idle)
      ...
    }
  }
}

You might probably want to distinguish the Messages that your machine accepts as events (to trigger transitions) and the ones that it emits (as "actions") by having a Process1[InMessage, OutMessage]

2
  • I don't see any reason init would be needed inside of go, so the whole go layer isn't really necessary, right? Mar 6, 2015 at 3:18
  • Oh, right I was taking that from some similar code I have where the init state is encapsulated. I'll edit the answer.
    – Eric
    Mar 6, 2015 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.