From 287ee291cce65878a011c6683fa40d0d5706fb59 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 25 Jan 2024 13:37:41 -0500 Subject: [PATCH] Adding a stream that send a TCP packet to the echo server and monitors the n_sends and n_recvs --- .../Misc/TCPConnectionTestStream.cs | 79 +++++++++++++++++++ src/DataStreamSuite/PubSubServer/Program.cs | 4 +- 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/DataStreamSuite/DataStream/Misc/TCPConnectionTestStream.cs diff --git a/src/DataStreamSuite/DataStream/Misc/TCPConnectionTestStream.cs b/src/DataStreamSuite/DataStream/Misc/TCPConnectionTestStream.cs new file mode 100644 index 000000000..b275558da --- /dev/null +++ b/src/DataStreamSuite/DataStream/Misc/TCPConnectionTestStream.cs @@ -0,0 +1,79 @@ +using BCG.Utilities.General; + +using System; + +namespace BCG { + public class TCPConnectionTestStream : ViewStream, IWorker, ITCPConnectionHandler, ITCPMessageHandler { + class State { + public int n_sends = 0; + public int n_recvs = 0; + } + private readonly string _ip; + private readonly int _port; + private readonly ViewStreamDatabase _vsd; + State _state = new State(); + private readonly int _msg_size; + + private IBlueTableSchema _schema; + private TimeSpan _send_period; + private ITCPConnection _connection = null; + private byte[] _buff; + + public TCPConnectionTestStream(string ip, int port, int msg_size, int send_period_seconds) : + this(ip, port, msg_size, send_period_seconds, ViewStreamDatabase.instance) { } + public TCPConnectionTestStream(string ip, int port, int msg_size, int send_period_seconds, ViewStreamDatabase vsd) { + _ip = ip; + _port = port; + _msg_size = msg_size; + _vsd = vsd; + _schema = BlueDataUtility.form_schema("TCPClient", + Tuple.Create("id", TypeCode.Int32), + Tuple.Create("n_sends", TypeCode.UInt32), + Tuple.Create("n_recvs", TypeCode.UInt32)); + _send_period = TimeSpan.FromSeconds(send_period_seconds); + _buff = new byte[msg_size]; + for (int i = 0; i < _buff.Length; i++) { + _buff[i] = (byte)i; + } + } + + protected override void myOnFirstSubscriber() { + MasterWorker.getInstance(false).addWorker(this, _send_period); + var server_address = new BlutoSubAddress(_ip, _port); + bts.AsyncTCPConnector_APM.connect_to(server_address, this); + } + + protected override void myOnLastSubscriber() { + MasterWorker.getInstance(false).removeWorker(this); + } + + public void doWork() { + if (_connection != null) { + _connection.send(_buff); + _state.n_sends++; + } + IBlueRow r = new BlueRow(_schema, 0, _state.n_sends, _state.n_recvs); + BlueTable rows = new BlueTable(r.schema); + rows.addRow(r); + var dp = BlueDataUtility.build_dp_inc(rows, myLastValue as DataPacket, true); + myFireEvent(dp); + } + + public void onConnection(ITCPConnection connection) { + _connection = connection; + _connection.register(this); + } + public bool isDisposed { get; } + + public void onData(RemoteMessage msg) { + bts.Log.Gen.info($"TCPConnectionTestStream received msg: {msg}"); + _state.n_recvs++; + } + + public void Dispose() { + + } + + + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 39fac8009..1c3df22fb 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -21,7 +21,9 @@ namespace BCG { if (run_echo_tcp_server_only) { Console.WriteLine("Running as TCP Echo server, Hit enter to continue"); Console.ReadLine(); - SimpleTCPServer sts = new SimpleTCPServer(12345); + var port = 12345; + Console.WriteLine($"ready to listen to port: {port}"); + SimpleTCPServer sts = new SimpleTCPServer(port); sts.init(); Task.Delay(1000000000).Wait(); }