PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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