PubSub is working now

This commit is contained in:
Taha authored and John Colvin committed 2023-09-30 12:53:51 -04:00
1 parent 3fa37397d9
commit 7bb7f1a5e2
27 files changed
+1108 -1124

No files matched your search

@@ -0,0 +1,132 @@
using System;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Spark.Core.Reactive;
using System.Security.Cryptography.X509Certificates;
using BCG;
namespace BCG
{
// This uses AsyncTCPConnection2's stateful async functions
public class SimpleTCPClientAsync : ITCPConnectionHandler, ITCPMessageHandler
{
AsyncTCPConnection2 _conn = null;
int _m_msgs_received = 0;
public static async Task start(string ip, int port)
{
SimpleTCPClientAsync c = new SimpleTCPClientAsync(ip, port);
c.connect();
}
public void connect()
{
Task.Run(async () => await TCPSocketUtils.connect_to_server_async("127.0.0.1", 12345, this, TimeSpan.FromSeconds(2)));
}
public void onConnection(ITCPConnection connection)
{
Console.WriteLine("SimpleTCPClientAsync.onConnection connected to server, starting task to send data in a loop");
connection.register(this);
// start sending data
Task.Run(async () =>
{
byte[] buff = new byte[1024];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = (byte)i;
}
_conn = connection as AsyncTCPConnection2;
while (!_conn.isDisposed)
{
await _conn.send_async(buff);
await Task.Delay(100);
}
});
}
public void onData(RemoteMessage msg)
{
_m_msgs_received++;
Console.WriteLine($"SimpleTCPClientAsync.onData n_msgs: {_m_msgs_received} msg: {msg}");
if (msg.messageType == RemoteMessageType.Disconnection)
{
connect();
}
}
public bool isDisposed { get { return false; } }
public void Dispose() { }
public SimpleTCPClientAsync(string ip, int port) { }
}
public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler
{
private int _n_msgs_received = 0;
AsyncTCPConnection2 _conn = null;
public static void start(string ip, int port)
{
SimpleTCPClient c = new SimpleTCPClient(ip, port);
c.connect();
}
public void connect()
{
Console.WriteLine("SimpleTCPClient.onConnection connect() called");
ClientToServerManagedTCPConnector.connect_to(new BlutoSubAddress("bluto://127.0.0.1:12345"), this, TimeSpan.FromSeconds(1));
}
public void onConnection(ITCPConnection connection)
{
_conn = connection as AsyncTCPConnection2;
Console.WriteLine("SimpleTCPClient.onConnection connected to server, starting task to send data in a loop");
_conn.register(this);
byte[] buff = new byte[1024];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = (byte)i;
}
while (!_conn.isDisposed)
{
_conn.send(buff);
System.Threading.Thread.Sleep(1000);
}
// Task.Run(async () =>
// {
// while (!_conn.isDisposed)
// {
// await _conn.send_async(buff);
// await Task.Delay(100);
// }
// });
}
public void onData(RemoteMessage msg)
{
_n_msgs_received++;
Console.WriteLine($"SimpleTCPClient.onData messages received: {_n_msgs_received} msg: {msg} messages received: {_n_msgs_received}");
if (msg.messageType == RemoteMessageType.Disconnection)
{
connect();
}
}
public bool isDisposed { get { return false; } }
public void Dispose() { }
public SimpleTCPClient(string ip, int port) { }
}
}