/scripster/src/razie/scripster/Scripster.scala

http://razpub.googlecode.com/ · Scala · 83 lines · 52 code · 15 blank · 16 comment · 4 complexity · 38c37c0e40f5f3eea2303dc902cbf01a MD5 · raw file

  1. package razie.scripster
  2. import com.razie.pub.lightsoa._
  3. import com.razie.pub.comms._
  4. import com.razie.pub.base._
  5. import com.razie.pub.base.data._
  6. import com.razie.pub.http._
  7. import com.razie.pub.http.sample._
  8. import com.razie.pub.http.LightContentServer
  9. import com.razie.pub.base.ExecutionContext
  10. import razie.base._
  11. import razie.scripting._
  12. /**
  13. * a door to the REPL
  14. *
  15. * Usage: it works with a LightServer (see project razweb). You can {@link #attachTo} to an existing server or {@link create} a new, dedicated one.
  16. *
  17. * If all you need in an app is the scripster, create one on a port of your choice, with no extra services.
  18. *
  19. * To customize interaction, you can mess with the Repl - that's the actual interaction with the REPL
  20. *
  21. * @author razvanc99
  22. */
  23. object Scripster {
  24. // hook to the web/telnet server
  25. var contents : CS = null
  26. /** use this to attach the REPL to an existing server */
  27. def attachTo (ls:LightServer) = {
  28. contents = new CS(ls.contents.asInstanceOf[LightContentServer]) // TODO cleanup cast
  29. ls.contents = contents
  30. }
  31. /** create a new server on the specified port and start it on the thread */
  32. def create (port:Int, t:Option[(Runnable) => Thread], services:Seq[HttpSoaBinding] = Nil) {
  33. val ME = new AgentHandle("localhost", "localhost", "127.0.0.1", port
  34. .toString(), "http://localhost:" + port.toString());
  35. // stuff to set before you start the server
  36. HtmlRenderUtils.setTheme(new HtmlRenderUtils.DarkTheme());
  37. NoStatics.put(classOf[Agents], new Agents(new AgentCloud(ME), ME));
  38. val server = new LightServer (port, 20, ExecutionContext.instance(), new LightContentServer())
  39. val get = new MyServer()
  40. server.registerHandler(get)
  41. server.registerHandler(new LightCmdPOST(get))
  42. get.registerSoa(new HttpSoaBinding(ScriptService))
  43. services.foreach (get.registerSoa(_))
  44. attachTo(server)
  45. t match {
  46. case Some(mk) => mk (server).start()
  47. case None => server.run()
  48. }
  49. }
  50. }
  51. /** actual interaction with the REPL */
  52. object Repl {
  53. ScriptFactory.init (new ScriptFactoryScala (null, true))
  54. def exec (lang:String, script:String, ctx:ScriptContext) : AnyRef = {
  55. razie.Log ("execute script=" + script)
  56. val s = ScriptFactory.make ("scala", script)
  57. s.eval(ctx)
  58. }
  59. def options (sessionId:String, line:String) = {
  60. val session = Sessions get sessionId
  61. val ret =
  62. if (line endsWith "a") razie.AI("b") :: razie.AI("c") :: Nil
  63. else if (line endsWith "b") razie.AI("c") :: Nil
  64. else Nil
  65. razie.Debug ("options for: \'"+line+"\' are: " +ret)
  66. ret
  67. }
  68. }