public static Session Load (FileSystemDatabase fsd, int key) { var toLoad = fsd.Read (key); if (toLoad == null) { return null; } var result = new Session (); result.Id = key; if (toLoad.ContainsKey (ShovelVmStateName)) { result.ShovelVmState = toLoad [ShovelVmStateName]; } if (toLoad.ContainsKey (ShovelVmBytecodeName)) { result.ShovelVmBytecode = toLoad [ShovelVmBytecodeName]; } if (toLoad.ContainsKey (ShovelVmSourcesName)) { result.ShovelVmSources = Encoding.UTF8.GetString (toLoad [ShovelVmSourcesName]); } if (toLoad.ContainsKey (PageContentName)) { result.PageContent.Append (Encoding.UTF8.GetString (toLoad [PageContentName])); } if (toLoad.ContainsKey (ReadStateName)) { result.ReadState = (ReadStates)BitConverter.ToInt32 (toLoad [ReadStateName], 0); } return result; }
public static void Main(string[] args) { if (!HttpListener.IsSupported) { Console.WriteLine ("HttpListener not available."); Environment.Exit (-1); } var bytecode = ProgramBytecode (); if (bytecode == null) { Environment.Exit (-1); } HttpListener hl = new HttpListener (); hl.Prefixes.Add ("http://localhost:8080/"); hl.Start (); var requestNo = 1; var fsd = new FileSystemDatabase ("db"); while (hl.IsListening) { var ctx = hl.GetContext (); Console.WriteLine ("Serving a request ({0} {1}).", requestNo, ctx.Request.Url.AbsolutePath); if (ctx.Request.Url.AbsolutePath == "/") { ServeGuessNumberRequest (ctx, fsd); } else { ctx.Response.OutputStream.Close (); } Console.WriteLine ("Served a request ({0}).", requestNo); requestNo++; } }
public static Session Load(FileSystemDatabase fsd, int key) { var toLoad = fsd.Read(key); if (toLoad == null) { return(null); } var result = new Session(); result.Id = key; if (toLoad.ContainsKey(ShovelVmStateName)) { result.ShovelVmState = toLoad [ShovelVmStateName]; } if (toLoad.ContainsKey(ShovelVmBytecodeName)) { result.ShovelVmBytecode = toLoad [ShovelVmBytecodeName]; } if (toLoad.ContainsKey(ShovelVmSourcesName)) { result.ShovelVmSources = Encoding.UTF8.GetString(toLoad [ShovelVmSourcesName]); } if (toLoad.ContainsKey(PageContentName)) { result.PageContent.Append(Encoding.UTF8.GetString(toLoad [PageContentName])); } if (toLoad.ContainsKey(ReadStateName)) { result.ReadState = (ReadStates)BitConverter.ToInt32(toLoad [ReadStateName], 0); } return(result); }
static Session FreshSession(FileSystemDatabase fsd) { var session = new Session (); session.Id = fsd.GetFreshId (); session.ShovelVmSources = Program (); session.ShovelVmBytecode = Shovel.Api.SerializeBytecode (ProgramBytecode ()); return session; }
static Session FreshSession(FileSystemDatabase fsd) { var session = new Session(); session.Id = fsd.GetFreshId(); session.ShovelVmSources = Program(); session.ShovelVmBytecode = Shovel.Api.SerializeBytecode(ProgramBytecode()); return(session); }
public void Save(FileSystemDatabase fsd) { var toSave = new Dictionary <string, byte[]> (); toSave [ShovelVmStateName] = this.ShovelVmState; toSave [ShovelVmBytecodeName] = this.ShovelVmBytecode; toSave [PageContentName] = Encoding.UTF8.GetBytes(this.PageContent.ToString()); toSave [ReadStateName] = BitConverter.GetBytes((int)this.ReadState); toSave [ShovelVmSourcesName] = Encoding.UTF8.GetBytes(this.ShovelVmSources); fsd.Write(this.Id, toSave); }
private static void ServeGuessNumberRequest(HttpListenerContext ctx, FileSystemDatabase fsd) { ctx.Response.ContentType = "text/html"; var userInput = ctx.Request.QueryString ["input"]; int sessionId = 0; var sessionIdStr = ctx.Request.QueryString ["sessionid"]; int.TryParse(sessionIdStr, out sessionId); Session session = null; if (sessionId != 0) { session = Session.Load(fsd, sessionId); } if (session == null) { session = FreshSession(fsd); } var vm = Shovel.Api.RunVm( Shovel.Api.DeserializeBytecode(session.ShovelVmBytecode), ProgramSources(Program()), Udps(session, userInput), session.ShovelVmState); if (Shovel.Api.VmExecutionComplete(vm)) { ctx.Response.Redirect("/"); } else { session.ShovelVmState = Shovel.Api.SerializeVmState(vm); // FIXME: Uncomment the next statement to fix the 'back button bug'. //session.Id = fsd.GetFreshId (); session.Save(fsd); using (var sw = new StreamWriter(ctx.Response.OutputStream)) { sw.Write("<!DOCTYPE html>\n"); sw.Write(session.PageContent.ToString()); sw.Write("<form action='/' method='get'>"); sw.Write("<input type='text' name='input' id='shovel-input'/>"); sw.Write("<input type='submit' value='Submit'/>"); sw.Write(String.Format( "<input type='hidden' name='sessionid' value='{0}' id='shovel-input'/>", session.Id)); sw.Write("</form>"); sw.Write("<script>\n"); sw.Write("document.getElementById('shovel-input').focus()\n"); sw.Write("</script>\n"); } } ctx.Response.OutputStream.Close(); }
public static void Main(string[] args) { if (!HttpListener.IsSupported) { Console.WriteLine("HttpListener not available."); Environment.Exit(-1); } var bytecode = ProgramBytecode(); if (bytecode == null) { Environment.Exit(-1); } HttpListener hl = new HttpListener(); hl.Prefixes.Add("http://localhost:8080/"); hl.Start(); var requestNo = 1; var fsd = new FileSystemDatabase("db"); while (hl.IsListening) { var ctx = hl.GetContext(); Console.WriteLine("Serving a request ({0} {1}).", requestNo, ctx.Request.Url.AbsolutePath); if (ctx.Request.Url.AbsolutePath == "/") { ServeGuessNumberRequest(ctx, fsd); } else { ctx.Response.OutputStream.Close(); } Console.WriteLine("Served a request ({0}).", requestNo); requestNo++; } }
private static void ServeGuessNumberRequest(HttpListenerContext ctx, FileSystemDatabase fsd) { ctx.Response.ContentType = "text/html"; var userInput = ctx.Request.QueryString ["input"]; int sessionId = 0; var sessionIdStr = ctx.Request.QueryString ["sessionid"]; int.TryParse (sessionIdStr, out sessionId); Session session = null; if (sessionId != 0) { session = Session.Load (fsd, sessionId); } if (session == null) { session = FreshSession (fsd); } var vm = Shovel.Api.RunVm ( Shovel.Api.DeserializeBytecode (session.ShovelVmBytecode), ProgramSources (Program ()), Udps (session, userInput), session.ShovelVmState); if (Shovel.Api.VmExecutionComplete (vm)) { ctx.Response.Redirect ("/"); } else { session.ShovelVmState = Shovel.Api.SerializeVmState (vm); // FIXME: Uncomment the next statement to fix the 'back button bug'. //session.Id = fsd.GetFreshId (); session.Save (fsd); using (var sw = new StreamWriter(ctx.Response.OutputStream)) { sw.Write ("<!DOCTYPE html>\n"); sw.Write (session.PageContent.ToString ()); sw.Write ("<form action='/' method='get'>"); sw.Write ("<input type='text' name='input' id='shovel-input'/>"); sw.Write ("<input type='submit' value='Submit'/>"); sw.Write (String.Format ( "<input type='hidden' name='sessionid' value='{0}' id='shovel-input'/>", session.Id)); sw.Write ("</form>"); sw.Write ("<script>\n"); sw.Write ("document.getElementById('shovel-input').focus()\n"); sw.Write ("</script>\n"); } } ctx.Response.OutputStream.Close (); }
public void Save (FileSystemDatabase fsd) { var toSave = new Dictionary<string, byte[]> (); toSave [ShovelVmStateName] = this.ShovelVmState; toSave [ShovelVmBytecodeName] = this.ShovelVmBytecode; toSave [PageContentName] = Encoding.UTF8.GetBytes (this.PageContent.ToString ()); toSave [ReadStateName] = BitConverter.GetBytes ((int)this.ReadState); toSave [ShovelVmSourcesName] = Encoding.UTF8.GetBytes (this.ShovelVmSources); fsd.Write (this.Id, toSave); }