public async Task <StreamReader> Connect(SimulatorInfo simInfo)
    {
        if (onlineStream != null)
        {
            onlineStream.Close();
            onlineStream.Dispose();
            onlineStream = null;
        }

        var json = Newtonsoft.Json.JsonConvert.SerializeObject(simInfo, JsonSettings.camelCase);
        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, new Uri(InstanceURL, "/api/v1/clusters/connect"));

        message.Content = new StringContent(json, Encoding.UTF8, "application/json");
        message.Headers.Add("SimId", Config.SimID);
        message.Headers.Add("Accept", "application/json");
        message.Headers.Add("Connection", "Keep-Alive");
        message.Headers.Add("X-Accel-Buffering", "no");
        Console.WriteLine("[CONN] Connecting to WISE");

        var response = await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, requestTokenSource.Token).ConfigureAwait(false);

        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine("[CONN] Failed to connect to WISE");
            var content = await response.Content.ReadAsStringAsync();

            throw new NoSuccessException($"{content} ({(int)response.StatusCode})");
        }
        Console.WriteLine("[CONN] Connected to WISE.");
        onlineStream = new StreamReader(await response.Content.ReadAsStreamAsync());
        return(onlineStream);
    }
Example #2
0
 public void Setup(SimulatorInfo info)
 {
     if (_setup != null)
     {
         _setup(info);
     }
 }
Example #3
0
 public void Setup(SimulatorInfo info)
 {
     for (int index = 0; index < this._setters.Length; ++index)
     {
         this._setters[index].Setup(info);
     }
 }
Example #4
0
 public void TearDown(SimulatorInfo info)
 {
     if (_tearDown != null)
     {
         _tearDown(info);
     }
 }
 public void Setup(SimulatorInfo info)
 {
     for (int i = 0; i < _setters.Length; i++)
     {
         _setters[i].Setup(info);
     }
 }
Example #6
0
 public void TearDown(SimulatorInfo info)
 {
     for (int index = 0; index < this._setters.Length; ++index)
     {
         this._setters[index].TearDown(info);
     }
 }
 public void TearDown(SimulatorInfo info)
 {
     for (int i = 0; i < _setters.Length; i++)
     {
         _setters[i].TearDown(info);
     }
 }
Example #8
0
        public void AddLocation()
        {
            //InfoModel.Instance.server.ReadFromClient(InfoModel.Instance.server.client);
            SimulatorInfo si     = ClientSide.Instance.SampleFlightValues();
            var           emp    = InfoModel.Instance;
            string        result = " ";
            string        path   = Server.MapPath("~/App_Data/flight1.xml");

            if (new FileInfo(path).Length <= 1)
            {
                ToXml(InfoModel.Instance, path);
            }
            else
            {
                XDocument xDocument         = XDocument.Load(path);
                XElement  root              = xDocument.Element("Datas");
                IEnumerable <XElement> rows = root.Descendants("Data");
                XElement firstRow           = rows.First();

                firstRow.AddBeforeSelf(
                    new XElement("Data",
                                 new XElement("Lon", si.Lon),
                                 new XElement("Lat", si.Lat),
                                 new XElement("Rud", si.Rud),
                                 new XElement("Thr", si.Thr)));
                xDocument.Save(path);
            }
        }
Example #9
0
        public ActionResult display(string ip, int port, int?time)
        {
            System.Net.IPAddress Ip = null;
            if (System.Net.IPAddress.TryParse(ip, out Ip) == false)
            {
                Session["fileName"] = ip;
                Session["time"]     = port;
                string path = Server.MapPath("~/App_Data/" + ip + ".xml");
                string xml  = System.IO.File.ReadAllText(path);
                xmlist    = xml.Split(new string[] { "</Data>" }, StringSplitOptions.None).ToList();
                xmlist[0] = xmlist[0].Remove(0, 47);
                xmlist.RemoveAt(xmlist.Count - 1);
                for (int i = 0; i < xmlist.Count; i++)
                {
                    xmlist[i] = xmlist[i] + "</Data>";
                }
                counter = 0;
                return(View("Load"));
            }
            bool containsInt = ip.Any(char.IsDigit);

            //InfoModel.Instance.ip = ip;
            //InfoModel.Instance.port = port.ToString();
            if (time != null)
            {
                //InfoModel.Instance.time = (int)time;
                ClientSide.Instance.Time = (int)time;
                ViewBag.timeLoad         = 1;
            }
            else
            {
                InfoModel.Instance.time = 0;
                ViewBag.timeLoad        = 0;
            }
            //InfoModel.Instance.server.Connect(ip, port);
            ClientSide.Instance.Ip   = ip;
            ClientSide.Instance.Port = port;
            if (!ClientSide.Instance.IsConnectedToSimulator)
            {
                ClientSide.Instance.Connect();
            }
            else
            {
                ClientSide.Instance.Disconnect();
                ClientSide.Instance.Connect();
            }
            SimulatorInfo si = ClientSide.Instance.SampleFlightValues();

            if (containsInt) // Format -> ip/port/time -> Show the pass
            {
                ViewBag.Format = 1;
                ViewBag.lon    = InfoModel.Instance.lon;
                ViewBag.lat    = InfoModel.Instance.lat;

                Session["time"] = InfoModel.Instance.time;
            }

            return(View());
        }
Example #10
0
 public void TearDown(SimulatorInfo info)
 {
     if (this._tearDown == null)
     {
         return;
     }
     this._tearDown(info);
 }
Example #11
0
 public void Setup(SimulatorInfo info)
 {
     if (this._setup == null)
     {
         return;
     }
     this._setup(info);
 }
        public int GetTimesToRunMultiplier(SimulatorInfo info)
        {
            int netID = info.npcVictim.netID;

            if (netID != 216 && netID != 491)
            {
                return(0);
            }
            return(_timesToRun);
        }
        public string GetValuesFromSimulatorAndDisplayRoute()
        {
            SimulatorInfo info     = ClientSide.Instance.SampleFlightValues();
            double        lon      = info.Lon;
            double        lat      = info.Lat;
            double        throttle = info.Throttle;
            double        rudder   = info.Rudder;

            return(ToXml(lat, lon, throttle, rudder, 0));
        }
        public int GetTimesToRunMultiplier(SimulatorInfo info)
        {
            switch (info.npcVictim.netID)
            {
            case 216:
            case 491:
                return(this._timesToRun);

            default:
                return(0);
            }
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            string        ip         = Configuration.GetValue <string>("SimulatorInfo:IP");
            string        httpPort   = Configuration.GetValue <string>("SimulatorInfo:HttpPort");
            string        telnetPort = Configuration.GetValue <string>("SimulatorInfo:TelnetPort");
            SimulatorInfo info       = new SimulatorInfo(ip, httpPort, telnetPort);

            services.AddControllers();
            services.AddSingleton(info);
            services.AddSingleton(typeof(IFlightGearClient), typeof(FlightGearClient));
            services.AddSingleton(typeof(IFlightSimulatorManager), typeof(FlightSimulatorManager));
        }
        // Establish the connection to the flight simulator.
        private void ConnectToSimulator(String ip, int port)
        {
            ClientSide.Instance.Ip   = ip;
            ClientSide.Instance.Port = port;
            ClientSide.Instance.Connect();
            SimulatorInfo info = ClientSide.Instance.SampleFlightValues();

            // Send the initial values that were sampled by the flight simulator to the view.
            ViewBag.Lon      = info.Lon;
            ViewBag.Lat      = info.Lat;
            ViewBag.Throttle = info.Throttle;
            ViewBag.Rudder   = info.Rudder;
        }
        public string GetValuesFromSimulatorAndSave()
        {
            SimulatorInfo info              = ClientSide.Instance.SampleFlightValues();
            double        lon               = info.Lon;
            double        lat               = info.Lat;
            double        throttle          = info.Throttle;
            double        rudder            = info.Rudder;
            string        dataToWriteInFile = lon.ToString() + "," + lat.ToString() + ","
                                              + throttle.ToString() + "," + rudder.ToString() + "\r\n";

            WriteToFile(dataToWriteInFile);
            return(ToXml(lat, lon, throttle, rudder, 0));
        }
Example #18
0
        public string GetLocation()
        {
            //InfoModel.Instance.server.ReadFromClient(InfoModel.Instance.server.client);
            SimulatorInfo si = ClientSide.Instance.SampleFlightValues();

            //var emp = InfoModel.Instance;
            //var emp = ClientSide.Instance;
            //System.Diagnostics.Debug.WriteLine("Lon: " + InfoModel.Instance.lon + ", Lat: " + InfoModel.Instance.lat + "\n");
            System.Diagnostics.Debug.WriteLine("Lon: " + si.Lon + ", Lat: " + si.Lat + "\n");
            /*emp.Salary = rnd.Next(1000);*/

            return(ToXml(si.Lon, si.Lat, si.Rud, si.Thr, 0));
        }
Example #19
0
        public int GetTimesToRunMultiplier(SimulatorInfo info)
        {
            switch (info.npcVictim.netID)
            {
            case -33:
            case -32:
            case -10:
            case -9:
            case -8:
            case -7:
            case -6:
            case -5:
            case -4:
            case -3:
            case 1:
            case 16:
            case 138:
            case 141:
            case 147:
            case 184:
            case 187:
            case 204:
            case 302:
            case 333:
            case 334:
            case 335:
            case 336:
            case 433:
            case 535:
            case 537:
                return(this._timesToRun);

            default:
                return(0);
            }
        }
Example #20
0
 public int GetTimesToRunMultiplier(SimulatorInfo info)
 {
     return(1);
 }
Example #21
0
    async Task Connect()
    {
        try
        {
            simInfo          = CloudAPI.GetInfo();
            Status           = ConnectionStatus.Connecting;
            DisconnectReason = null;
            RunOnUnityThread(() =>
            {
                ConnectionUI.instance.UpdateStatus();
            });

            foreach (var timeOut in timeOutSequence)
            {
                try
                {
                    var reader = await API.Connect(simInfo);
                    await ReadResponse(reader);

                    break;
                }
                catch (HttpRequestException ex)
                {
                    // temporary network issue, we'll retry
                    Debug.Log(ex.Message + ", reconnecting after " + timeOut + " seconds");
                    await Task.Delay(1000 *timeOut);
                }
                if (Status == ConnectionStatus.Offline)
                {
                    Debug.Log("User cancelled connection.");
                    break;
                }
            }

            if (Config.RetryForever)
            {
                while (true)
                {
                    try
                    {
                        var reader = await API.Connect(simInfo);
                        await ReadResponse(reader);

                        break;
                    }
                    catch (CloudAPI.NoSuccessException ex)
                    {
                        Debug.Log(ex.Message + ", reconnecting after " + timeOutSequence[timeOutSequence.Length - 1] + " seconds");
                        DisconnectReason = ex.Message;
                        await Task.Delay(1000 *timeOutSequence[timeOutSequence.Length - 1]);
                    }
                }
            }
        }
        catch (CloudAPI.NoSuccessException ex)
        {
            // WISE told us it does not like us, so stop reconnecting
            DisconnectReason = ex.Message;
            Debug.Log($"WISE backend reported error: {ex.Message}, will not reconnect");
        }
        catch (TaskCanceledException)
        {
            Debug.Log("Linking task canceled.");
            DisconnectReason = "Linking task canceled.";
        }
        catch (System.Net.Sockets.SocketException se)
        {
            Debug.Log($"Could not reach WISE SSE at {Config.CloudUrl}: {se.Message}");
            DisconnectReason = $"Could not reach WISE SSE at {Config.CloudUrl}: {se.Message}";
        }
        catch (Exception ex)
        {
            Debug.LogException(ex);
        }

        Debug.Log("Giving up reconnecting.");
        Disconnect();
    }
 public void Setup(SimulatorInfo info)
 {
 }
        async void LinkTask(SimulatorInfo simInfo)
        {
            try
            {
                var stream = await API.Connect(simInfo);

                string line;
                using var reader = new StreamReader(stream);
                while (true)
                {
                    var lineTask = reader.ReadLineAsync();
                    if (await Task.WhenAny(lineTask, Task.Delay(30000)) != lineTask)
                    {
                        Debug.Log("Took to long to link to cluster, aborting");
                        return;
                    }
                    line = lineTask.Result;

                    if (line == null)
                    {
                        break;
                    }

                    if (line.StartsWith("data:") && !string.IsNullOrEmpty(line.Substring(6)))
                    {
                        JObject deserialized = JObject.Parse(line.Substring(5));
                        if (deserialized != null && deserialized.HasValues)
                        {
                            var status = deserialized.GetValue("status");
                            if (status != null)
                            {
                                switch (status.ToString())
                                {
                                case "Unrecognized":
                                    Application.OpenURL(Config.CloudUrl + "/clusters/link?token=" + simInfo.linkToken);
                                    break;

                                case "OK":
                                    Debug.Log("appear to be linked!");
                                    Refresh();
                                    linked = true;
                                    return;

                                default:
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("error linking editor instance to wise");
                Debug.LogException(e);
            }
            finally
            {
                Debug.Log("closing api");
                API.Disconnect();
            }
        }
 public void TearDown(SimulatorInfo info)
 {
 }