<commit_before>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Jasper.Bus.Transports.Tcp;
using Jasper.Util;

namespace Jasper.Bus.Transports.Receiving
{

    // This is only really used in the automated testing now
    // to test out the wire protocol. Otherwise, this has been superceded
    // by SocketListeningAgent
    public class ListeningAgent : IDisposable
    {
        public int Port { get; }

        private readonly IReceiverCallback _callback;
        private readonly CancellationToken _cancellationToken;
        private readonly TcpListener _listener;
        private Task _receivingLoop;
        private readonly ActionBlock<Socket> _socketHandling;
        private readonly Uri _uri;

        public ListeningAgent(IReceiverCallback callback, int port, string protocol, CancellationToken cancellationToken)
        {
            Port = port;
            _callback = callback;
            _cancellationToken = cancellationToken;
            _listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, port));
            _listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            _socketHandling = new ActionBlock<Socket>(async s =>
            {
                using (var stream = new NetworkStream(s, true))
                {
                    await WireProtocol.Receive(stream, _callback, _uri);
                }
            });

            _uri = $"{protocol}://{Environment.MachineName}:{port}/".ToUri();

        }<commit_msg>no longer hard coding the listening IP address. Closes GH-234<commit_after>﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Jasper.Bus.Transports.Tcp;
using Jasper.Util;

namespace Jasper.Bus.Transports.Receiving
{

    // This is only really used in the automated testing now
    // to test out the wire protocol. Otherwise, this has been superceded
    // by SocketListeningAgent
    public class ListeningAgent : IDisposable
    {
        public int Port { get; }

        private readonly IReceiverCallback _callback;
        private readonly CancellationToken _cancellationToken;
        private readonly TcpListener _listener;
        private Task _receivingLoop;
        private readonly ActionBlock<Socket> _socketHandling;
        private readonly Uri _uri;

        public ListeningAgent(IReceiverCallback callback, int port, string protocol, CancellationToken cancellationToken)
        {
            Port = port;
            _callback = callback;
            _cancellationToken = cancellationToken;

            _listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
            _listener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            _socketHandling = new ActionBlock<Socket>(async s =>
            {
                using (var stream = new NetworkStream(s, true))
                {
                    await WireProtocol.Receive(stream, _callback, _uri);
                }
            });

            _uri = $"{protocol}://{Environment.MachineName}:{port}/".ToUri();

        }
{'fraction_non_alphanumeric': 0.07603406326034064, 'fraction_numerical': 0.0009124087591240876, 'mean_word_length': 2.610318331503842, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3866988', 'n_tokens_mistral': 841, 'n_tokens_neox': 829, 'n_words': 231}