예제 #1
0
        /// <summary>
        /// Monitors a connection to IB in the background. Waits until a signal is
        /// received to exit the program.
        /// </summary>
        /// <param name="opts">The parsed command line options.</param>
        private async Task Run(Options opts)
        {
            using var _ = new IbClient(
                      opts.Host, opts.Port, opts.ClientId, opts.PagerTreeIntegrationId);
            await _exitEvent.WaitAsync();

            await ConsoleX.WriteLineAsync("Goodbye");
        }
예제 #2
0
        /// <summary>
        /// Continuously checks the health of a connection to IB.
        /// </summary>
        /// <param name="host">The host to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="clientId">The client ID to connect as.</param>
        /// <param name="pagerTreeIntId">The PagerTree integration ID to notify when
        /// there is an error connecting to IB.</param>
        /// <param name="token">The token to check to end the task.</param>
        private static async Task Run(
            string host, int port, int clientId, string pagerTreeIntId,
            CancellationToken token)
        {
            Incident           incident = null;
            ITwsControllerBase tws      = null;
            var connected = false;

            while (!token.IsCancellationRequested)
            {
                try
                {
                    if (tws is null)
                    {
                        var ip = await GetIpAddress(host, token);

                        tws = new TwsObjectFactory(ip.ToString(), port, clientId)
                              .TwsController;
                    }

                    await CheckConnection(tws, token);

                    await tws.RequestPositions();

                    if (incident != null)
                    {
                        await incident.Resolve(token);

                        incident = null;
                        await ConsoleX.WriteLineAsync(
                            "Resolved PagerTree incident", token);
                    }

                    if (!connected)
                    {
                        await ConsoleX.WriteLineAsync("Active", token);

                        connected = true;
                    }
                }
                catch (Exception e)
                {
                    tws       = null;
                    connected = false;

                    if (incident is null && !string.IsNullOrEmpty(pagerTreeIntId))
                    {
                        incident = new Incident(
                            pagerTreeIntId, "IB is down", e.Message);
                        await incident.Notify(token);

                        await ConsoleX.WriteErrorLineAsync(
                            "Created PagerTree incident", token);
                    }

                    await ConsoleX.WriteErrorLineAsync(
                        e.Message, token);
                }

                await Task.Delay(Sleep, token);
            }
        }