Exemple #1
0
        public static SoftTech.Wui.HtmlResult<HElement> HView(object _state, JsonData[] jsons, HContext context)
        {
            var state = _state.As<MainState>() ?? new MainState();

              foreach (var json in jsons.Else_Empty())
              {

            switch (json.JPath("data", "command").ToString_Fair())
            {
              case "text":
            state = state.With(text: json.JPath("value").ToString_Fair());
            System.Threading.Thread.Sleep(new Random().Next(0, 500));
            break;
              default:
            break;
            }
              }

              var page = Page(state);
              return new SoftTech.Wui.HtmlResult<HElement>
              {
            Html = page,
            State = state,
              };
        }
Exemple #2
0
    public static SoftTech.Wui.HtmlResult<HElement> HView(object _state, JsonData json, HContext context)
    {
      var state = _state.As<SecondState>() ?? new SecondState();

      if (json != null)
      {
        switch (json.JPath("data", "command").ToString_Fair())
        {
          default:
            break;
        }
      }

      var page = Page();
      return new SoftTech.Wui.HtmlResult<HElement>
      {
        Html = page,
        State = state,
      };
    }
Exemple #3
0
        public static SoftTech.Wui.HtmlResult<HElement> HView(object _state, JsonData[] jsons, HContext context)
        {
            var state = _state.As<MainState>() ?? new MainState();

              foreach (var json in jsons.Else_Empty())
              {
            //var account = MemoryDatabase.World.Accounts.FirstOrDefault(_account => _account.Name == "darkgray");

            switch (json.JPath("data", "command").ToString_Fair())
            {
              default:
            //state = new MyLibState(new[] { new Message(json.ToString_Fair()) }.Concat(state.Messages).Take(10).ToArray());
            break;
            }
              }

              var page = Page(state, context);
              return new SoftTech.Wui.HtmlResult<HElement>
              {
            Html = page,
            State = state,
              };
        }
Exemple #4
0
        public string ProcessRequest(Dictionary<string, string> parameters, JsonData json)
        {
            var uri = parameters.Find("SCRIPT_NAME") + parameters.Find("PATH_INFO");
              var queryString = parameters.Find("QUERY_STRING");

              Console.WriteLine("{0} {1}, {2}", uri, queryString, Handlers.Select(pair => pair.Key).FirstOrDefault());

              var handlerPair = Handlers.FirstOrDefault(pair => uri._f(_ => _.StartsWith(pair.Key)));
              if (handlerPair.Value == null)
              {

            return "<html><body>invalid page</body></html>";
              }
              var handler = handlerPair.Value;
              var pageName = handlerPair.Key;

              if (uri.EndsWith(".js"))
              {
            var query = queryString.Else_Empty().Split('&').Select(pair => pair.Split('='))
              .ToDictionary(ss => ss.FirstOrDefault(), ss => ss.ElementAtOrDefault(1));
            var prevCycle = int.Parse(query.Find("cycle"));
            var prevUpdate = Updates.FirstOrDefault(_update => _update.Cycle == prevCycle);
            var prevPage = prevUpdate != null ? prevUpdate.Page : null;

            var update = PushUpdate(null);

            var result = handler(pageName, update.Cycle, json);
            //lock (locker)
            //{
            //  update.Page = result.Html;

            //}
            var page = result.Html;

            var commands = HtmlJavaScriptSynchronizer.JsSync(new HElementProvider(), prevPage == null ? null : prevPage.Element("body"), page.Element("body")).ToArray();
            var jupdate = new Dictionary<string, object>() { { "cycle", update.Cycle }, { "prev_cycle", prevCycle }, { "commands", commands } };

            var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            return jsSerializer.Serialize(jupdate);
              }
              else
              {
            var update = PushUpdate(null);

            var result = handler(pageName, update.Cycle, json);
            var page = result.Html;
            if (!uri.EndsWith(".raw.html"))
              page = h.Html(new HRaw(page.Element("head").ToString_Fair()), h.Body());

            //lock (locker)
            //{
            //  update.Page = page;
            //}
            return page.ToString();
              }
        }
Exemple #5
0
        private static void ProcessRequest(bool isFcgiDebug, FcgiSynchronizer sync, Socket s)
        {
            using (s)
              using (var stream = new NetworkStream(s))
              {
            Log("accepted..");

            for (var i = 0; i < 20; ++i)
            {
              if (stream.DataAvailable)
            break;
              System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(30));
              if (i == 19)
              {
            Log("cancel");
            return;
              }
            }

            var parameters = new Dictionary<string, string>();
            for (; ; )
            {
              var req = stream.ReadRequest();
              if (isFcgiDebug)
            Log(string.Format("{0}:{1}, {2}:{3}:{4}", req.Version, req.Type, req.RequestId, req.Content.Hex(), req.Padding.Hex()));

              if (req.Type == FcgiType.Params)
              {
            var _parameters = ParseParameters(req.Content);
            if (_parameters.Count > 0)
            {
              parameters = _parameters;
              if (isFcgiDebug)
              {
                Log("parameters:");
                foreach (var pair in parameters)
                  Log(string.Format("  {0}:{1}", pair.Key, pair.Value));
              }
            }
              }
              JsonData json = null;
              if (req.Type == FcgiType.Stdin && req.Content != null && req.Content.Length > 0)
              {
            if (isFcgiDebug)
              Log("Stdin text: " + System.Text.Encoding.UTF8.GetString(req.Content));
            var data = ParseQuery(System.Text.Encoding.UTF8.GetString(req.Content));
            var values = data.ContainsKey("values") ? data["values"] : null;
            if (isFcgiDebug)
              Log("  " + values);
            //var jsSerializer = new System.Net.Json.JsonTextParser();
            var jsSerializer = Newtonsoft.Json.JsonSerializer.Create();
            json = new JsonData(jsSerializer.Deserialize(new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(values))));

              }
              if (req.Type == FcgiType.Stdin && req.Padding.Length == 0)
              {
            var res_text = string.Format(@"Content-type: text/html

            {0}
            ", sync.ProcessRequest(parameters, json));
            var res_data = System.Text.Encoding.UTF8.GetBytes(res_text);
            var res_pad = new byte[8 - res_data.Length % 8];
            var res = new FcgiRequest { Version = 1, Type = FcgiType.Stdout, RequestId = req.RequestId, Content = res_data, Padding = res_pad };
            stream.Write(res);
            stream.Write(new FcgiRequest { Version = 1, Type = FcgiType.Stdout, RequestId = req.RequestId });
            stream.Write(new FcgiRequest { Version = 1, Type = FcgiType.EndRequest, RequestId = req.RequestId, Content = new byte[8] });
            break;
              }
            }
              }
        }
Exemple #6
0
    public static SoftTech.Wui.HtmlResult<HElement> HView(object _state, JsonData json, HContext context)
    {
      var state = _state.As<MainState>() ?? new MainState();

      if (json != null)
      {
        switch (json.JPath("data", "command").ToString_Fair())
        {
          case "new-order":
            {
              var name = json.JPath("data", "name").ToString_Fair() ?? "C";
              var isDelivery = json.JPath("data", "is-delivery").ToString_Fair() == "True";
              var toTime = ToTime(json.JPath("data", "to-time").ToString_Fair());
              var status = toTime == null ? ProductStatus.New: ProductStatus.InQueue;
              var products = name == "B" ? new[] { new Product("K", status) } : new[] { new Product("M", status), new Product("P", status) };
              state = state.With(orders: state.Orders.Add(new Order(name: name, isDelivery: isDelivery, status: toTime == null ? OrderStatus.New: OrderStatus.InQueue, products: ImmutableArray.Create(products), toTime: toTime)));  
            }
            break;
          case "product-prepare":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var productId = ConvertHlp.ToGuid(json.JPath("data", "product"));
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              var product = order._f(_ => _.Products).Else_Empty().FirstOrDefault(_product => _product.Id == productId);
              if (product != null && product.Status != ProductStatus.New)
                product = null;
              if (product != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(products: order.Products.Replace(product, product.With(status: ProductStatus.Prepare)))));
              }
            }
            break;
          case "product-ready":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var productId = ConvertHlp.ToGuid(json.JPath("data", "product"));
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              var product = order._f(_ => _.Products).Else_Empty().FirstOrDefault(_product => _product.Id == productId);
              if (product != null && product.Status != ProductStatus.Prepare)
                product = null;
              if (product != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(products: order.Products.Replace(product, product.With(status: ProductStatus.Ready)))));
              }
            }
            break;
          case "order-build":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              if (order != null && order.Status != OrderStatus.Prepare && !order.IsReady)
                order = null;
              if (order != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(status: OrderStatus.Ready)));
              }
            }
            break;
          case "courier":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var courier = json.JPath("data", "courier").ToString_Fair();
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              if (order != null && order.Status != OrderStatus.Ready && !order.IsDelivery)
                order = null;
              if (order != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(status: OrderStatus.ToDelivery, courier: courier)));
              }
            }
            break;
          case "order-deliveried":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              if (order != null && order.Status != OrderStatus.ToDelivery)
                order = null;
              if (order != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(status: OrderStatus.Deliveried)));
              }
            }
            break;
          case "order-to-table":
            {
              var orderId = ConvertHlp.ToGuid(json.JPath("data", "order"));
              var order = state.Orders.FirstOrDefault(_order => _order.Id == orderId);
              if (order != null && order.Status != OrderStatus.Ready && order.IsDelivery)
                order = null;
              if (order != null)
              {
                state = state.With(orders: state.Orders.Replace(order, order.With(status: OrderStatus.ToTable)));
              }
            }
            break;
          default:
            break;
        }
      }
      state = state.With(orders: DeQueue(state.Orders, DateTime.Now));

      var page = Page(state);
      return new SoftTech.Wui.HtmlResult<HElement>
      {
        Html = page,
        State = state,
      };
    }
Exemple #7
0
        //
        //  см. также Global.Application_AuthenticateRequest
        //
        public static SoftTech.Wui.HtmlResult<HElement> HView(object _state, JsonData[] jsons, HContext context)
        {
            var logins = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase){{"Demo", "demodemo"}, {"Test", "test"}};

              foreach (var json in jsons.Else_Empty())
              {

            switch (json.JPath("data", "command").ToString_Fair())
            {
              case "login":
            {
              var login = json.JPath("data", "login").ToString_Fair();
              var password = json.JPath("data", "password").ToString_Fair();
              if (logins.Find(login) == password)
                context.HttpContext.SetUserAndCookie(login);
              else
              {
                //display error
              }
            }
            break;
              case "logout":
            context.HttpContext.SetUserAndCookie(null);
            break;
              default:
            break;
            }

            if (context.HttpContext.UserName() != null)
            {
              switch (json.JPath("data", "command").ToString_Fair())
              {
            case "login":
              {
                var login = json.JPath("data", "login").ToString_Fair();
                var password = json.JPath("data", "password").ToString_Fair();
                if (logins.Find(login) == password)
                  context.HttpContext.SetUserAndCookie(login);
                else
                {
                  //display error
                }
              }
              break;
            case "logout":
              context.HttpContext.SetUserAndCookie(null);
              break;
            default:
              break;
              }

            }
              }
              var account = context.HttpContext.UserName();

              var page = Page(logins, account);
              return new SoftTech.Wui.HtmlResult<HElement>
              {
            Html = page,
            State = null,
              };
        }