public void Start(
            Action<TTelemetryBatch, ServerCallContext> onBatchReceived,
            Func<CurrentLibraryConfig, ServerCallContext, UpdatedLibraryConfig> onConfigReceived)
        {
            if (this.IsRunning)
            {
                throw new InvalidOperationException(
                    FormattableString.Invariant($"Can't Start the input, it's already running"));
            }

            this.stats = new InputStats();
            this.cts = new CancellationTokenSource();
            this.onBatchReceived = onBatchReceived;
            this.onConfigReceived = onConfigReceived;
            try
            {
                this.server = new Server
                {
                    Services = {this.aiServer != null ? AITelemetryService.BindService(this.aiServer) : TraceService.BindService(this.openCensusServer)},
                    Ports = {new ServerPort(this.host, this.port, ServerCredentials.Insecure)}
                };

                this.server.Start();

                this.IsRunning = true;
            }
            catch (System.Exception e)
            {
                throw new InvalidOperationException(
                    FormattableString.Invariant($"Could not initialize the gRPC server. {e.ToString()}"));
            }
        }
Example #2
0
        /// <summary>
        /// Starts listening for data.
        /// </summary>
        /// <param name="onBatchReceived">A callback to be invoked every time there is a new incoming telemetry batch. No guarantees are provided as to which thread the callback is called on.</param>
        public void Start(Action <TelemetryBatch> onBatchReceived)
        {
            if (this.IsRunning)
            {
                throw new InvalidOperationException(FormattableString.Invariant($"NamedPipeInput is already running, can't start it."));
            }

            this.onBatchReceived = onBatchReceived;

            this.cts = new CancellationTokenSource();

            this.stats = new InputStats();

            try
            {
                // start the first PipeServer, it will wait for the first connection
                var firstPipeServer = new PipeServer();

                lock (this.pipeServers)
                {
                    this.pipeServers.Add(firstPipeServer);
                }

                Task.Run(() =>
                {
                    // call pipe server's Start() and mark ourselves as running before it completes (after the first await), so no await in the call below
#pragma warning disable 4014
                    firstPipeServer.Start(this.OnClientConnected, this.OnClientDisconnected, this.OnBatchReceived);
#pragma warning restore 4014

                    this.IsRunning = true;
                });
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Failed to start the first pipe server. This entire pipe input will not be able to receive data.", e);
            }
        }
        public void Start()
        {
            if (this.IsRunning)
            {
                throw new InvalidOperationException(
                          FormattableString.Invariant($"Can't Start the input, it's already running"));
            }

            this.stats = new InputStats();
            this.cts   = new CancellationTokenSource();

            try
            {
                this.server = new Server
                {
                    Services =
                    {
                        Tracespan.HandleTraceSpanService.BindService(new IstioMixerTraceSpanService((request, context) =>
                        {
                            this.OnDataReceived(request, context);
                            return(Task.FromResult(new ReportResult()));
                        }))
                    },
                    Ports = { new ServerPort(this.host, this.port, ServerCredentials.Insecure) }
                };

                this.server.Start();

                this.IsRunning = true;
            }
            catch (System.Exception e)
            {
                throw new InvalidOperationException(
                          FormattableString.Invariant($"Could not initialize the gRPC server. {e.ToString()}"));
            }
        }