public void run() { // configure http server but do not start running yet Servers.MinimalHTTPServer.ServerConfiguration config = new Servers.MinimalHTTPServer.ServerConfiguration(); Servers.MinimalHTTPServer.Server server = new Servers.MinimalHTTPServer.Server(config); server.ServerNotify += server_ServerNotify; server.addHTTPResponsibility(new ElseResponsibility()); server.addHTTPResponsibility(new RunningResponsibility()); // configure web socket server but do not start running Servers.MinimalWebSocketServer.ServerConfiguration webSocketconfig = new Servers.MinimalWebSocketServer.ServerConfiguration(); webSocketconfig.bufferSize = 100; Servers.MinimalWebSocketServer.Server webSocketServer = new Servers.MinimalWebSocketServer.Server(webSocketconfig); webSocketServer.ServerNotify += server_ServerNotify; DataSources dataSources = new DataSources(); VesselChangeDetector vesselChangeDetector = new VesselChangeDetector(false); IKSPAPI kspAPI = new DummyKSPAPI(JSONFormatterProvider.Instance, vesselChangeDetector, config); //webSocketServer.addWebSocketService("/server", new KSPWebSocketService(kspAPI,)); //webSocketServer.subscribeToHTTPForStealing(server); // start the HTTP server //server.startServing(); //Console.Read(); //server.stopServing(); }
public bool process(Servers.AsynchronousServer.ClientConnection cc, HTTPRequest request) { DataSources dataSources = new DataSources(); if (request.path.StartsWith(PAGE_PREFIX)) { if (request.requestType == HTTPRequest.GET) { dataRates.addUpLinkPoint(System.DateTime.Now, request.path.Length * UpLinkDownLinkRate.BITS_PER_BYTE); } else if (request.requestType == HTTPRequest.POST) { dataRates.addUpLinkPoint(System.DateTime.Now, request.content.Length * UpLinkDownLinkRate.BITS_PER_BYTE); } try { dataSources.vessel = kspAPI.getVessel(); } catch (Exception e) { PluginLogger.debug(e.Message + " " + e.StackTrace); } if (request.requestType == HTTPRequest.GET) { dataRates.addDownLinkPoint( System.DateTime.Now, ((Servers.MinimalHTTPServer.ClientConnection)cc).Send(new OKResponsePage( argumentsParse(request.path.Remove(0, request.path.IndexOf(ARGUMENTS_START) + 1), dataSources) )) * UpLinkDownLinkRate.BITS_PER_BYTE); } else if (request.requestType == HTTPRequest.POST) { dataRates.addDownLinkPoint( System.DateTime.Now, ((Servers.MinimalHTTPServer.ClientConnection)cc).Send(new OKResponsePage( argumentsParse(request.content, dataSources) )) * UpLinkDownLinkRate.BITS_PER_BYTE); } return true; } return false; }
public DataSources Clone() { DataSources d = new DataSources(); d.vessel = this.vessel; d.args = new List<string>(this.args); return d; }
private String argumentsParse(String args, DataSources dataSources) { APIEntry currentEntry = null; List<string> APIResults = new List<string>(); String[] argsSplit = args.Split(ARGUMENTS_DELIMETER); foreach (String arg in argsSplit) { string refArg = arg; PluginLogger.fine(refArg); kspAPI.parseParams(ref refArg, ref dataSources); currentEntry = argumentParse(refArg, dataSources); APIResults.Add(currentEntry.formatter.format(currentEntry.function(dataSources))); //Only parse the paused argument if the active vessel is null if (dataSources.vessel == null) { break; } } return currentEntry.formatter.pack(APIResults); }
private APIEntry argumentParse(String args, DataSources dataSources) { String[] argsSplit = args.Split(ARGUMENTS_ASSIGN); APIEntry result = null; kspAPI.process(argsSplit[1], out result); result.formatter.setVarName(argsSplit[0]); return result; }
private void parseParams(ref String arg, ref DataSources dataSources) { dataSources.args.Clear(); try { if (arg.Contains("[")) { String[] argsSplit = arg.Split('['); argsSplit[1] = argsSplit[1].Substring(0, argsSplit[1].Length - 1); arg = argsSplit[0]; String[] paramSplit = argsSplit[1].Split(','); for (int i = 0; i < paramSplit.Length; i++) { dataSources.args.Add(paramSplit[i]); } } } catch (Exception e) { PluginLogger.debug(e.Message + " " + e.StackTrace); } }
private void streamData(object sender, ElapsedEventArgs e) { streamTimer.Interval = streamRate; DataSources dataSources = new DataSources(); if (toRun.Count + subscriptions.Count > 0) { try { List<string> entries = new List<string>(); APIEntry entry = null; lock (subscriptionLock) { dataSources.vessel = kspAPI.getVessel(); //Only parse the paused argument if the active vessel is null if (dataSources.vessel != null) { toRun.UnionWith(subscriptions); foreach (string s in toRun) { DataSources dataSourcesClone = dataSources.Clone(); string trimedQuotes = s.Trim(); string refArg = trimedQuotes; kspAPI.parseParams(ref refArg, ref dataSourcesClone); kspAPI.process(refArg, out entry); if (entry != null) { entry.formatter.setVarName(trimedQuotes); entries.Add(entry.formatter.format(entry.function(dataSourcesClone))); } } toRun.Clear(); if (entry != null) { WebSocketFrame frame = new WebSocketFrame(ASCIIEncoding.UTF8.GetBytes(entry.formatter.pack(entries))); byte[] bFrame = frame.AsBytes(); dataRates.addDownLinkPoint(System.DateTime.Now, bFrame.Length * UpLinkDownLinkRate.BITS_PER_BYTE); clientConnection.Send(bFrame); } } else { sendNullMessage(); } } } catch(NullReferenceException) { PluginLogger.debug("Swallowing null reference exception, potentially due to async game state change."); sendNullMessage(); } catch (Exception ex) { PluginLogger.debug("Closing socket due to potential client disconnect:" + ex.GetType().ToString()); close(); } } else { sendNullMessage(); } }
private void streamData(object sender, ElapsedEventArgs e) { streamTimer.Interval = streamRate; DataSources dataSources = new DataSources(); if (toRun.Count + subscriptions.Count > 0) { try { List <string> entries = new List <string>(); APIEntry entry = null; lock (subscriptionLock) { dataSources.vessel = kspAPI.getVessel(); //Only parse the paused argument if the active vessel is null if (dataSources.vessel != null) { toRun.UnionWith(subscriptions); foreach (string s in toRun) { DataSources dataSourcesClone = dataSources.Clone(); string trimedQuotes = s.Trim().Substring(1, s.Length - 2); string refArg = trimedQuotes; kspAPI.parseParams(ref refArg, ref dataSourcesClone); kspAPI.process(refArg, out entry); if (entry != null) { entry.formatter.setVarName(trimedQuotes); entries.Add(entry.formatter.format(entry.function(dataSourcesClone))); } } toRun.Clear(); if (entry != null) { WebSocketFrame frame = new WebSocketFrame(ASCIIEncoding.UTF8.GetBytes(entry.formatter.pack(entries))); byte[] bFrame = frame.AsBytes(); dataRates.addDownLinkPoint(System.DateTime.Now, bFrame.Length * UpLinkDownLinkRate.BITS_PER_BYTE); clientConnection.Send(bFrame); } } else { sendNullMessage(); } } } catch (NullReferenceException) { PluginLogger.debug("Swallowing null reference exception, potentially due to async game state change."); sendNullMessage(); } catch (Exception ex) { PluginLogger.debug("Closing socket due to potential client disconnect:" + ex.GetType().ToString()); close(); } } else { sendNullMessage(); } }
private String argumentsParse(String args, DataSources dataSources) { APIEntry currentEntry = null; var APIResults = new Dictionary<string,object>(); String[] argsSplit = args.Split(ARGUMENTS_DELIMETER); foreach (String arg in argsSplit) { string refArg = arg; PluginLogger.fine(refArg); kspAPI.parseParams(ref refArg, ref dataSources); currentEntry = argumentParse(refArg, dataSources); APIResults[dataSources.getVarName()] = currentEntry.formatter.prepareForSerialization(currentEntry.function(dataSources)); //Only parse the paused argument if the active vessel is null if (dataSources.vessel == null) { break; } } return SimpleJson.SimpleJson.SerializeObject(APIResults); }