question about interruptable state runner

Hello. I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type ErrorT MyError (StateT ScriptState IO) so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState. But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation. Can smb give an advice what is the best way to do it? -- Best regards Alexander V Vershilov

On Thu, Jun 9, 2011 at 3:14 PM, Alexander V Vershilov
Hello.
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
What do you mean by 'server event'? How do you plan on representing these events?
Can smb give an advice what is the best way to do it?
-- Best regards Alexander V Vershilov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello. Thu, Jun 09, 2011 at 01:40:51PM -0500, Antoine Latter wrote
On Thu, Jun 9, 2011 at 3:14 PM, Alexander V Vershilov
wrote: Hello.
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
What do you mean by 'server event'? How do you plan on representing these events?
I'm using term server event for next thing when server get some message from client it generate 'server event' i.e. some action or save this event in event queue. And then forces this queue to act. So simplified workflow looks like: client server interpretator 1 *sends some messages -> save parsed messages in queue 2 *send 'END' message -> forces functions F(message) from queue 3 *some F is to call interpretator -> run script 4 <- *call server <- send messages to client callback goto step 2 <- *finish script * - stand for event initiator. Here everything can be done in a straitforward way. What I want to do and do know a good way for: client server -> run script ... <- call 'wait' command <-> *goto 1 ... send 'END' -> continue script -> Sorry if I do not answer on your question directly. -- Best regards, Alexander V Vershilov
Can smb give an advice what is the best way to do it?
-- Best regards Alexander V Vershilov

On Thu, Jun 9, 2011 at 4:04 PM, Alexander V Vershilov
Hello.
Thu, Jun 09, 2011 at 01:40:51PM -0500, Antoine Latter wrote
On Thu, Jun 9, 2011 at 3:14 PM, Alexander V Vershilov
wrote: Hello.
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
What do you mean by 'server event'? How do you plan on representing these events?
I'm using term server event for next thing when server get some message from client it generate 'server event' i.e. some action or save this event in event queue. And then forces this queue to act.
So simplified workflow looks like: client server interpretator 1 *sends some messages -> save parsed messages in queue 2 *send 'END' message -> forces functions F(message) from queue 3 *some F is to call interpretator -> run script 4 <- *call server <- send messages to client callback goto step 2 <- *finish script
* - stand for event initiator. Here everything can be done in a straitforward way.
What I want to do and do know a good way for: client server -> run script ... <- call 'wait' command <-> *goto 1 ... send 'END' -> continue script ->
Sorry if I do not answer on your question directly.
-- Best regards, Alexander V Vershilov
The next question - are you waiting for the client to open a new connection to your server and send the result, or are you waiting for the client to send a response on an already open socket? If the later, the GHC IO manager will halt your thread of execution and wake it back up once the socket has data on it. If the former, you'll need to explore some of the solutions Heinrich has put forth.
Can smb give an advice what is the best way to do it?
-- Best regards Alexander V Vershilov
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Alexander V Vershilov wrote:
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
Can smb give an advice what is the best way to do it?
Basically, you want to stop the interpreter and resume it at some later point. You need to implement a custom monad for that, which can easily be done with the help of my operational package. http://projects.haskell.org/operational/ In particular, the examples WebSessionState.lhs TicTacToe.hs PoorMansConcurrency.hs show how to suspend and resume the control flow. Feel free to request additional examples. Best regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com

Just for the culture (because Heinrich's solution is undoubtly simpler) you
can also achive this using continuations.
This is one of the purposes of the Cont monad, used jointly with a State
monad, you can store the continuations and resume them later.
2011/6/10 Heinrich Apfelmus
Alexander V Vershilov wrote:
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation. Can smb give an advice what is the best way to do it?
Basically, you want to stop the interpreter and resume it at some later point. You need to implement a custom monad for that, which can easily be done with the help of my operational package.
http://projects.haskell.org/operational/
In particular, the examples
WebSessionState.lhs TicTacToe.hs PoorMansConcurrency.hs
show how to suspend and resume the control flow. Feel free to request additional examples.
Best regards, Heinrich Apfelmus
-- http://apfelmus.nfshost.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Fri, Jun 10, 2011 at 03:27:44PM +0200, Heinrich Apfelmus wrote
Alexander V Vershilov wrote:
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
Can smb give an advice what is the best way to do it?
Basically, you want to stop the interpreter and resume it at some later point. You need to implement a custom monad for that, which can easily be done with the help of my operational package.
http://projects.haskell.org/operational/
In particular, the examples
WebSessionState.lhs TicTacToe.hs PoorMansConcurrency.hs
show how to suspend and resume the control flow. Feel free to request additional examples.
Best regards, Heinrich Apfelmus
Operational package is the what I need, thanks. Documentation is and examples are very good so I think I'll have no problems to implement such a behavior. -- Best regards Alexander V Vershilov

On 11-06-09 04:14 PM, Alexander V Vershilov wrote:
Hello.
I'm writing a small tcp server with that can handle connections and answer by rules writen in a small script that can be interpreted by server. For this purpose I've written an interpreter that has type
ErrorT MyError (StateT ScriptState IO)
so I can call "native" IO function in that script, and define new one. I can run this script with runState (runErrorT (...)) oldState.
But there is one problem: in script i should be able to call functions that will stop script interpretation and wait for some server event. To continue interpretation.
Can smb give an advice what is the best way to do it?
It appears you've already settled on operational, and that's a good choice. I just wanted to point out the monad-coroutine as a possible alternative. The two packages have lots of similarities, the main difference appears to be in the way the monad suspension is seen: Prompt vs. Functor.
participants (5)
-
Alexander V Vershilov
-
Antoine Latter
-
Heinrich Apfelmus
-
Mario Blažević
-
Yves Parès