_04_GuessTheNumberTurnTheTables.Session.Save C# (CSharp) Method

Save() public method

public Save ( FileSystemDatabase fsd ) : void
fsd FileSystemDatabase
return void
        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);
        }
    }

Usage Example

Beispiel #1
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 ();
 }