// Expecting: /Design/{Action}/{Id} public ControllerResponse Execute(ControllerRequest request) { if (request.Path.Length < 2) { return Index(); } int segmentId = 0; if (request.Path.Length >= 3) { segmentId = int.Parse(request.Path[2]); } switch (request.Path[1]) { case "Edit": return Edit(segmentId); case "Delete": return Delete(segmentId); case "EditPostback": return EditPostback(request); case "New": return New(); } return Index(); }
public async Task <ControllerResponse> SendRequestAsync(ControllerRequest request) { return(await Task.Run(async() => { await semaphore.WaitAsync(); try { request.Id = _nextRequestId++; byte[] data = GetBytes(request); var buffer = new ArraySegment <byte>(data); var resetEvent = new AutoResetEvent(false); if (request.HasResponse) { currentlyAwaitingResponseEvents.Add(request.Id, resetEvent); } await _clientWebSocket.SendAsync(buffer, WebSocketMessageType.Binary, true, CancellationToken.None); if (request.HasResponse) { resetEvent.WaitOne(); if (currentlyAwaitingResponses.TryGetValue(request.Id, out var latestResponse)) { return latestResponse; } return null; } return null; } finally { semaphore.Release(); } })); }
internal async Task Initialize() { var command = new ControllerRequest(Function.GetNodeProtocolInfo, new Payload(NodeID)); var protocolInfo = await Channel.Send <NodeProtocolInfo>(command, CancellationToken.None); NodeType = protocolInfo.NodeType; Security = protocolInfo.Security; IsListening = protocolInfo.IsListening; }
/// <summary> /// Creates controller proxy and establishes connection. /// </summary> /// <param name="endpointAddress"> /// Endpoint address (ip:port). <see cref="APIClient.ConnectToControllerProxy">Should be requested from control connection</see>. /// </param> /// <param name="keepAlive"> /// True if automatic pings should keep connection alive even if caller doesn't send data. /// </param> public ControllerProxy(string endpointAddress, bool keepAlive = false) : base(endpointAddress, keepAlive) { controller = new ControllerRequest() { Version = 2, Origin = (byte)ControllerOrigin.Zero, TaskType = (byte)ControllerTask.SendFullState }; }
public static void Main() { Bootstrapper.LoadModules("SalaryCalc.xml"); Console.Title = "Salary Calculation"; Console.OutputEncoding = Encoding.UTF8; ControllerRequest controllerRequest = new ControllerRequest <LoginController>(); while (controllerRequest != null) { controllerRequest = controllerRequest.RunController().RunView(); } Bootstrapper.Factory.GetInstance <IPersonService>().Save(); Bootstrapper.Factory.GetInstance <IWorkHoursDataService>().Save(); }
// /Data/{SegmentName(Id)}/Property/$value/$count?$filter=a lt b public ControllerResponse Execute(ControllerRequest request) { if (request.Path.Length <= 1) { return Collections(); } var items = request.Path[1].Split('(', ')'); string segmentName = items[0]; string id = items.Length > 1 ? items[1] : null; var db = Database.Open(); var segment = db.Segment.FindByName(segmentName); if (null == segment) { return null; } return Index(segment, id, request); }
/// <summary> /// Gets the neighbours of the node /// </summary> /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is None.</param> /// <returns>A task that represents the asynchronous operation</returns> public async Task <Node[]> GetNeighbours(CancellationToken cancellationToken = default) { var results = new List <Node>(); var command = new ControllerRequest(Function.GetRoutingTableLine, new Payload(NodeID)); // send request var response = await Channel.Send <Payload>(command, cancellationToken); var bits = new BitArray(response.ToArray()); for (byte i = 0; i < bits.Length; i++) { if (bits[i]) { results.Add(Controller.Nodes[(byte)(i + 1)]); } } return(results.ToArray()); }
private ControllerResponse EditPostback(ControllerRequest request) { dynamic database = Database.Open(); if (!this.CheckName(request.Query["Name"], int.Parse(request.Query["SegmentId"]), database)) { dynamic model = new ExpandoObject(); model.SegmentId = int.Parse(request.Query["SegmentId"]); model.Name = request.Query["Name"]; model.ConnectionString = request.Query["ConnectionString"]; model.Query = request.Query["Query"]; if (string.IsNullOrWhiteSpace(request.Query["Name"])) { model.Message = "Please enter a name for this data service"; } else { model.Message = "Please choose a different name, the name is already in use"; } return new ControllerResponse("Edit", model); } if (request.Query["SegmentId"] == "0") { database.Segment.Insert( Name: request.Query["Name"], ConnectionString: request.Query["ConnectionString"], Query: request.Query["Query"]); return Index(); } database.Segment.UpdateBySegmentId( SegmentId: int.Parse(request.Query["SegmentId"]), Name: request.Query["Name"], ConnectionString: request.Query["ConnectionString"], Query: request.Query["Query"]); return Index(); }
/// <summary> /// Disconnected from controller API and frees the API for other clients. /// </summary> public void Disconnect() { var disconnectRequest = new ControllerRequest() { Version = 2, TaskType = (byte)ControllerTask.Disconnect }; try { SendMessage(disconnectRequest); } catch (TimeoutException) { // Connection probably dropped another way, ignoring } catch (FiniteStateMachineException) { // Connection state invalid, close anyway } CloseSocket(); }
public ControllerResponse Index(dynamic segment, string id, ControllerRequest request) { dynamic model = new ExpandoObject(); model.Name = segment.Name; //model.BaseAddress = .Url.OriginalString.Replace(this.Request.Url.PathAndQuery, string.Empty); if (null == segment) { return null; } Tuple<int, string> pkInfo = GetPK(segment); model.PKIndex = pkInfo.Item1; model.BaseAddress = Program.BaseAddress; string fields = "*"; string orderby = null; int top = -1; bool json = false; List<Tuple<string, string, string>> where = new List<Tuple<string, string, string>>(); string view = null; bool singleResult = false; foreach (string key in request.Query.Keys) { string value = request.Query[key] ?? ""; switch (key.ToLower().Trim()) { case "$filter": string[] whereString = value.Split(','); foreach (string str in whereString) { string[] whereItems = str.Split(' '); if (whereItems.Length == 3) { where.Add(new Tuple<string, string, string>(whereItems[0].Trim(), whereItems[1].Trim(), whereItems[2].Trim())); } } break; case "$format": json = (0 == string.Compare(value, "json", true)); break; case "$inlinecount": { break; } case "$orderby": orderby = value; break; case "$select": fields = value; break; case "$skip": { break; } case "$skiptoken": { break; } case "$top": int.TryParse(value, out top); break; } } if (null != id) { view = "Entry"; where.Add(new Tuple<string, string, string>(pkInfo.Item2, "eq", id)); singleResult = true; string property = (from p in request.Path.Skip(2) where !p.StartsWith("$") select p).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(property)) { // the name of a property has been passed in view = request.Path.Contains("$value") ? "Value" : "Property"; fields = property; } } else { view = "Collection"; } if (request.Path.Contains("$count")) { fields = "count(*) as count"; singleResult = true; view = "Value"; } string query = DatabaseUtils.SelectWhere(segment.Query, top, fields, orderby, where.ToArray()); if (request.Query.Keys.Contains("$sql")) { model.Value = query; return new ControllerResponse("Value", model, "text/plain"); } model.Reader = DatabaseUtils.ExecuteReader(query, segment.ConnectionString); if (singleResult) { (model.Reader as SqlDataReader).Read(); } if ("Value" == view) { model.Value = (model.Reader as SqlDataReader).GetValue(0); return new ControllerResponse("Value", model, "text/plain"); } else { return new ControllerResponse(view, model, "application/rss+xml"); } }
private byte[] GetBytes(ControllerRequest data) { return(GetBytes(GetJson(data))); }
private string GetJson(ControllerRequest data) { return(JsonConvert.SerializeObject(data)); }
public MessageCarrier(T body, ControllerRequest whatToDo) { Body = body; WhatToDo = whatToDo; }