Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("HttpListener not available.");
                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 == "/")
                {
                    ServeRequest(ctx, fsd);
                }
                else
                {
                    ctx.Response.OutputStream.Close();
                }

                Console.WriteLine("Served a request ({0}).", requestNo);
                requestNo++;
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            if (!HttpListener.IsSupported) {
                Console.WriteLine ("HttpListener not available.");
                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 == "/") {
                    ServeRequest (ctx, fsd);
                } else {
                    ctx.Response.OutputStream.Close ();
                }

                Console.WriteLine ("Served a request ({0}).", requestNo);
                requestNo++;
            }
        }
Ejemplo n.º 4
0
 static void ServeRequest(HttpListenerContext ctx, FileSystemDatabase fsd)
 {
     ctx.Response.ContentType = "text/html";
     if (ctx.Request.HttpMethod.ToUpper() == "POST")
     {
         CompileProgram(ctx, fsd);
     }
     else
     {
         var userInput    = ctx.Request.QueryString ["input"];
         var sessionIdStr = ctx.Request.QueryString ["sessionid"];
         if (sessionIdStr == null)
         {
             ServeTheTextArea(ctx);
         }
         else
         {
             int sessionId = 0;
             int.TryParse(sessionIdStr, out sessionId);
             Session session = null;
             if (sessionId != 0)
             {
                 session = Session.Load(fsd, sessionId);
             }
             if (session == null)
             {
                 ServeTheTextArea(ctx);
             }
             else
             {
                 ServeSession(ctx, fsd, session, userInput);
             }
         }
     }
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
        static Session FreshSession(FileSystemDatabase fsd, Shovel.Instruction[] bytecode, string program)
        {
            var session = new Session();

            session.Id = fsd.GetFreshId();
            session.ShovelVmSources  = program;
            session.ShovelVmBytecode = Shovel.Api.SerializeBytecode(bytecode);
            return(session);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        static void ServeSession(HttpListenerContext ctx, FileSystemDatabase fsd, Session session, string userInput)
        {
            var vm = Shovel.Api.RunVm(
                Shovel.Api.DeserializeBytecode(session.ShovelVmBytecode),
                ProgramSources(session.ShovelVmSources),
                Udps(session, userInput),
                session.ShovelVmState,
                totalTicksQuota: null,
                ticksUntilNextNapQuota: null,
                usedCellsQuota: null);

            if (Shovel.Api.VmProgrammingError(vm) != null)
            {
                ServeError(ctx, session, Shovel.Api.VmProgrammingError(vm).Message);
            }
            else if (Shovel.Api.VmUserDefinedPrimitiveError(vm) != null)
            {
                ServeError(ctx, session, Shovel.Api.VmUserDefinedPrimitiveError(vm).Message);
            }
            else if (Shovel.Api.VmExecutionComplete(vm))
            {
                using (var sw = new StreamWriter(ctx.Response.OutputStream)) {
                    sw.Write("<!DOCTYPE html>\n");
                    sw.Write(session.PageContent.ToString());
                    sw.Write("<p>Program execution completed.</p>");
                    sw.Write("<a href='/' id='restart-link'>Enter another program.</p>");
                    sw.Write("<script>\n");
                    sw.Write("document.getElementById('restart-link').focus()\n");
                    sw.Write("</script>\n");
                }
            }
            else
            {
                session.ShovelVmState = Shovel.Api.SerializeVmState(vm);
                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();
        }
Ejemplo n.º 9
0
 static void CompileProgram(HttpListenerContext ctx, FileSystemDatabase fsd)
 {
     string postData;
     using (StreamReader getPostParam = new StreamReader(ctx.Request.InputStream, true)) {
         postData = getPostParam.ReadToEnd ();
         var parsedPostData = ParsePostData (postData);
         if (parsedPostData.ContainsKey ("program")) {
             var source = parsedPostData ["program"];
             try {
                 var bytecode = Shovel.Api.GetBytecode (ProgramSources (source));
                 var session = FreshSession (fsd, bytecode, source);
                 ServeSession (ctx, fsd, session, null);
             } catch (Shovel.Exceptions.ShovelException shex) {
                 ServeTheTextArea (ctx, source, shex.Message);
             }
         } else {
             ServeTheTextArea (ctx);
         }
     }
 }
Ejemplo n.º 10
0
        static void CompileProgram(HttpListenerContext ctx, FileSystemDatabase fsd)
        {
            string postData;

            using (StreamReader getPostParam = new StreamReader(ctx.Request.InputStream, true)) {
                postData = getPostParam.ReadToEnd();
                var parsedPostData = ParsePostData(postData);
                if (parsedPostData.ContainsKey("program"))
                {
                    var source = parsedPostData ["program"];
                    try {
                        var bytecode = Shovel.Api.GetBytecode(ProgramSources(source));
                        var session  = FreshSession(fsd, bytecode, source);
                        ServeSession(ctx, fsd, session, null);
                    } catch (Shovel.Exceptions.ShovelException shex) {
                        ServeTheTextArea(ctx, source, shex.Message);
                    }
                }
                else
                {
                    ServeTheTextArea(ctx);
                }
            }
        }
Ejemplo n.º 11
0
 static Session FreshSession(FileSystemDatabase fsd, Shovel.Instruction[] bytecode, string program)
 {
     var session = new Session ();
     session.Id = fsd.GetFreshId ();
     session.ShovelVmSources = program;
     session.ShovelVmBytecode = Shovel.Api.SerializeBytecode (bytecode);
     return session;
 }
Ejemplo n.º 12
0
 static void ServeSession(HttpListenerContext ctx, FileSystemDatabase fsd, Session session, string userInput)
 {
     var vm = Shovel.Api.RunVm (
         Shovel.Api.DeserializeBytecode (session.ShovelVmBytecode),
         ProgramSources (session.ShovelVmSources),
         Udps (session, userInput),
         session.ShovelVmState,
         totalTicksQuota: null,
         ticksUntilNextNapQuota: null,
         usedCellsQuota: null);
     if (Shovel.Api.VmProgrammingError (vm) != null) {
         ServeError (ctx, session, Shovel.Api.VmProgrammingError (vm).Message);
     } else if (Shovel.Api.VmUserDefinedPrimitiveError (vm) != null) {
         ServeError (ctx, session, Shovel.Api.VmUserDefinedPrimitiveError (vm).Message);
     } else if (Shovel.Api.VmExecutionComplete (vm)) {
         using (var sw = new StreamWriter(ctx.Response.OutputStream)) {
             sw.Write ("<!DOCTYPE html>\n");
             sw.Write (session.PageContent.ToString ());
             sw.Write ("<p>Program execution completed.</p>");
             sw.Write ("<a href='/' id='restart-link'>Enter another program.</p>");
             sw.Write ("<script>\n");
             sw.Write ("document.getElementById('restart-link').focus()\n");
             sw.Write ("</script>\n");
         }
     } else {
         session.ShovelVmState = Shovel.Api.SerializeVmState (vm);
         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 ();
 }
Ejemplo n.º 13
0
 static void ServeRequest(HttpListenerContext ctx, FileSystemDatabase fsd)
 {
     ctx.Response.ContentType = "text/html";
     if (ctx.Request.HttpMethod.ToUpper () == "POST") {
         CompileProgram (ctx, fsd);
     } else {
         var userInput = ctx.Request.QueryString ["input"];
         var sessionIdStr = ctx.Request.QueryString ["sessionid"];
         if (sessionIdStr == null) {
             ServeTheTextArea (ctx);
         } else {
             int sessionId = 0;
             int.TryParse (sessionIdStr, out sessionId);
             Session session = null;
             if (sessionId != 0) {
                 session = Session.Load (fsd, sessionId);
             }
             if (session == null) {
                 ServeTheTextArea (ctx);
             } else {
                 ServeSession (ctx, fsd, session, userInput);
             }
         }
     }
 }
Ejemplo n.º 14
0
 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);
 }