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

@@ -3,6 +3,7 @@
<ItemGroup>
<ProjectReference Include="..\..\client\spark_api\spark_api.csproj" />
<ProjectReference Include="..\..\client\spark_core\spark_core.csproj" />
<ProjectReference Include="..\PubSub\PubSub.csproj" />
</ItemGroup>
<PropertyGroup>
@@ -1,16 +1,18 @@
using bts.utils;
using System;
using mbts.config;
using BCG;
bts.utils.BTSNativeMethods.provide_command_line(new string[] { }, 0);
mbts.config.Configuration.initialize();
bts.Log.Gen.info($"Starting DSPublisher");
Console.WriteLine("Starting DSPublisher...");
// SimpleTCPClient.start("127.0.0.1", 12345);
// Task.Delay(1000000000).Wait();
//TestPubliser.publish();
//TestPubliser.subscribe();
TestPublisher ts = new TestPublisher(false);
ts.keep_publishing(TimeSpan.FromSeconds(10));
using System;
using bts;
using mbts;
BTSNativeMethods.provide_command_line(args, args.Length);
// Must initialize config before using log system
Console.WriteLine("Initializing config...");
Configuration.initialize();
for (int i = 0; i < 10; i++)
{
bts.Log.Gen.info($"Starting DataStreamPublisher...log: {i}");
}
@@ -4,15 +4,13 @@ using System.Collections;
using System.Collections.Generic;
using BCG.Utilities;
namespace BCG
{
namespace BCG {
/// <summary>
/// Holds the state for a single tag. If the tagType has an
/// IPubSubServerTagHandler, it is updated and the state stored
/// there. Otherwise, this class saves the last published data.
/// </summary>
internal class SingleTagHandler
{
internal class SingleTagHandler {
private IPubSubServerTagHandler myHandler = null;
private string myTagType, myTag;
/// <summary>
@@ -20,9 +18,7 @@ namespace BCG
/// </summary>
private byte[] myData = null;
public SingleTagHandler(string tag, string tagType)
{
public SingleTagHandler(string tag, string tagType) {
myTagType = tagType;
myTag = tag;
myHandler = TagType2HandlerMap.getHandler(tagType, false);
@@ -30,23 +26,18 @@ namespace BCG
public string tagType { get { return myTagType; } }
public void publish(Stream data)
{
public void publish(Stream data) {
long pos = data.Position;
if (myHandler != null)
{
if (myHandler != null) {
myHandler.update(myTag, new Stream[] { data });
}
else
{
} else {
myData = new byte[data.Length];
data.Read(myData, 0, myData.Length);
}
data.Position = pos;
}
public byte[] getContent()
{
public byte[] getContent() {
return myHandler == null ? myData : myHandler.getContent(myTag);
}
}
@@ -54,46 +45,37 @@ namespace BCG
/// <summary>
/// Keeps the state of all the tags published.
/// </summary>
public class AllPublishedTagState
{
public class AllPublishedTagState {
private Dictionary<string, SingleTagHandler> myTagHandler = new Dictionary<string, SingleTagHandler>();
public AllPublishedTagState() { }
public void publish(string tag, string tagType, Stream data)
{
public void publish(string tag, string tagType, Stream data) {
if (!myTagHandler.ContainsKey(tag))
myTagHandler.Add(tag, new SingleTagHandler(tag, tagType));
myTagHandler[tag].publish(data);
}
public void publish(string tag, string tagType, byte[] data)
{
public void publish(string tag, string tagType, byte[] data) {
publish(tag, tagType, new MemoryStream(data));
}
public string[] publishedTags
{ get { return new ArrayList(myTagHandler.Keys).ToArray(typeof(string)) as string[]; } }
public string[] publishedTags { get { return new ArrayList(myTagHandler.Keys).ToArray(typeof(string)) as string[]; } }
public byte[] getContent(string tag, out string tagType)
{
public byte[] getContent(string tag, out string tagType) {
tagType = myTagHandler[tag].tagType;
return myTagHandler[tag].getContent();
}
public bool contains(string tag)
{
return myTagHandler.ContainsKey(tag);
public bool contains(string tag) {
return myTagHandler.ContainsKey(tag);
}
public void clear(string tag)
{
if (myTagHandler.ContainsKey(tag))
{
public void clear(string tag) {
if (myTagHandler.ContainsKey(tag)) {
myTagHandler.Remove(tag);
}
else{}
// BlueEventLog.logEvent("AllPublishTagState told to clear nonexisting tag " + tag, BlueEventLog2Level.Warning);
} else { }
// BlueEventLog.logEvent("AllPublishTagState told to clear nonexisting tag " + tag, BlueEventLog2Level.Warning);
}
}
@@ -1,117 +1,117 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using mbts;
//using BCG.Utilities;
// using System;
// using System.Collections.Generic;
// using System.Text;
// using System.Runtime.InteropServices;
// using mbts;
// //using BCG.Utilities;
namespace BCG
{
public class PubSubServerUnmanagedTCPConnection : IPubSubServerConnection
{
private BlutoSubAddress myServerAddress;
private SmartConnectionManagedWrapper myWrapper;
private ConnectionStatusChange myConnCallback;
private SyncQueue<Pair<int, byte[]>> myAllMsgsQueue = new SyncQueue<Pair<int, byte[]>>();
// namespace BCG
// {
// public class PubSubServerUnmanagedTCPConnection : IPubSubServerConnection
// {
// private BlutoSubAddress myServerAddress;
// private SmartConnectionManagedWrapper myWrapper;
// private ConnectionStatusChange myConnCallback;
// private SyncQueue<Pair<int, byte[]>> myAllMsgsQueue = new SyncQueue<Pair<int, byte[]>>();
public PubSubServerUnmanagedTCPConnection(string serverURL, SmartConnectionManagedWrapper wrapper)
{
myServerAddress = new BlutoSubAddress(serverURL);
myWrapper = wrapper;
myWrapper.initConnection(new BlutoSubAddress(serverURL));
myWrapper.resgisterDataCallback(myServerAddress, new SmartConnectionManagedWrapper.MessageDelegate(myHandleServerMessages));
}
// public PubSubServerUnmanagedTCPConnection(string serverURL, SmartConnectionManagedWrapper wrapper)
// {
// myServerAddress = new BlutoSubAddress(serverURL);
// myWrapper = wrapper;
// myWrapper.initConnection(new BlutoSubAddress(serverURL));
// myWrapper.resgisterDataCallback(myServerAddress, new SmartConnectionManagedWrapper.MessageDelegate(myHandleServerMessages));
// }
#region IPubSubServerConnection Members
// #region IPubSubServerConnection Members
public void sendWithConfirmation(byte[] data, ref string confId)
{
//BlueEventLog.logAssert(false, "This function should not be used on this class");
}
// public void sendWithConfirmation(byte[] data, ref string confId)
// {
// //BlueEventLog.logAssert(false, "This function should not be used on this class");
// }
public void send(byte[] data)
{
myWrapper.send(myServerAddress, data);
}
// public void send(byte[] data)
// {
// myWrapper.send(myServerAddress, data);
// }
public void send(System.IO.Stream str)
{
send (StreamUtilityMethods.getBytesFromStream(str));
}
// public void send(System.IO.Stream str)
// {
// send (StreamUtilityMethods.getBytesFromStream(str));
// }
public IList<byte[]> getUpdates(TimeSpan timeout, int maxElements)
{
List<Pair<int, byte[]>> msgs = myAllMsgsQueue.dequeueMultipleElements(Convert.ToInt32 (timeout.TotalMilliseconds), maxElements);
List<byte[]> rv = new List<byte[]>();
bool connSet = false;
foreach (Pair<int, byte[]> p in msgs)
{
int msgType = p.first;
byte[] msg = p.second;
if (msgType == 1)
{
rv.Add(msg);
}
else
{
connSet = true;
string connMsg = "connection to " + myServerAddress.ToString() + (msgType == 2 ? " broken" : " established");
Console.WriteLine(connMsg);
myConnCallback(myServerAddress.ToString(), msgType == 3);
}
}
return connSet ? new List<byte[]>() : rv;
}
public string serverURL
{
get { return myServerAddress.ToString(); }
}
// public IList<byte[]> getUpdates(TimeSpan timeout, int maxElements)
// {
// List<Pair<int, byte[]>> msgs = myAllMsgsQueue.dequeueMultipleElements(Convert.ToInt32 (timeout.TotalMilliseconds), maxElements);
// List<byte[]> rv = new List<byte[]>();
// bool connSet = false;
// foreach (Pair<int, byte[]> p in msgs)
// {
// int msgType = p.first;
// byte[] msg = p.second;
// if (msgType == 1)
// {
// rv.Add(msg);
// }
// else
// {
// connSet = true;
// string connMsg = "connection to " + myServerAddress.ToString() + (msgType == 2 ? " broken" : " established");
// Console.WriteLine(connMsg);
// myConnCallback(myServerAddress.ToString(), msgType == 3);
// }
// }
// return connSet ? new List<byte[]>() : rv;
// }
public string clientURL
{
get { return myWrapper.getClientAsSeenByServer (serverURL); }
}
// public string serverURL
// {
// get { return myServerAddress.ToString(); }
// }
public void registerCallback(ConnectionStatusChange del)
{
myConnCallback = del;
}
// public string clientURL
// {
// get { return myWrapper.getClientAsSeenByServer (serverURL); }
// }
public void registerDataCallback(DataDelegate del)
{
}
// public void registerCallback(ConnectionStatusChange del)
// {
// myConnCallback = del;
// }
public void registerSendConfirmationCallback(SendConfirmationDelegate del)
{
}
// public void registerDataCallback(DataDelegate del)
// {
// }
#endregion
// public void registerSendConfirmationCallback(SendConfirmationDelegate del)
// {
// }
#region IDisposable Members
// #endregion
public void Dispose()
{
}
// #region IDisposable Members
#endregion
// public void Dispose()
// {
#region IWorker Members
public void doWork()
{
}
#endregion
// }
/// <summary>
/// this is the only method that it executed on
/// a different thread
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private void myHandleServerMessages(int msgType, byte[] msg)
{
myAllMsgsQueue.enqueue(new Pair<int, byte[]>(msgType, msg));
}
}
}
// #endregion
// #region IWorker Members
// public void doWork()
// {
// }
// #endregion
// /// <summary>
// /// this is the only method that it executed on
// /// a different thread
// /// </summary>
// /// <param name="data"></param>
// /// <returns></returns>
// private void myHandleServerMessages(int msgType, byte[] msg)
// {
// myAllMsgsQueue.enqueue(new Pair<int, byte[]>(msgType, msg));
// }
// }
// }
@@ -4,8 +4,7 @@ using System.Text;
using System.IO;
using BCG.Utilities.General;
namespace BCG
{
namespace BCG {
/// <summary>
/// This is the interface that both the PublisherCore and the
/// SubscriberCore use to communicate with the PubsubServer.
@@ -20,22 +19,23 @@ namespace BCG
/// multiple threads need to communicate to the PubSub, they
/// should create their own Subscribers.
/// </summary>
public interface IPubSubServerConnection : IDisposable, IWorker
{
public interface IPubSubServerConnection : IDisposable, IWorker {
/// <summary>
/// The delegate is fired signalling if the data send was
/// successful or not
/// </summary>
void sendWithConfirmation(byte[] data, ref string confId);
//void sendWithConfirmation(byte[] data, ref string confId);
/// <summary>
/// send is a non waiting call with no guarantee
/// of data being delivered
/// </summary>
void send (byte[] data);
/// <summary>
/// similiar to above
/// </summary>
// void send(byte[] data);
// /// <summary>
// /// similiar to above
// /// </summary>
void send(Stream str);
void send(PubSubMessage msg);
void send_with_confirmation(PubSubMessage msg, ref string confId);
/// <summary>
/// Returns with 0 or more elements, iff the timeout
/// expires or maxElements have been reached (selective polling),
@@ -51,13 +51,13 @@ namespace BCG
/// <summary>
/// server URL to which the connection is made
/// </summary>
string serverURL { get;}
string serverURL { get; }
/// <summary>
/// client URL as seen by the server. if a connection has not
/// been established (as can happen in TCP) an empty string
/// is returned
/// </summary>
string clientURL { get;}
string clientURL { get; }
/// <summary>
/// registers the callback to receive
@@ -25,48 +25,64 @@ namespace BCG
ITCPMessageHandler,
ITCPConnectionHandler
{
private ITCPConnection myConnection, myTempNewConnection;
private BlutoSubAddress myServerAddress;
private ThreadSafeQueue<Pair<string, bool>> myConfs = new ThreadSafeQueue<Pair<string, bool>>();
private SyncQueue<byte[]> myDataQueue = new SyncQueue<byte[]>();
private DateTime myLastHB = DateTime.Now;
private ConnectionStatusChange myConnCallback;
private DataDelegate myDataCallback;
private SendConfirmationDelegate myOnSendConfirmCallback;
private TimeSpan myConnectionDropThreshold =
private ITCPConnection _connection, _new_connection;
private readonly BlutoSubAddress _server_address;
// if a message(with confirm) can not be sent , its id is queued here,
// on reconnection, _on_send_onfirm_callback is fired with a negative on these ids, so the user
//can re-send it. The re-send mechanism is not in implemented in this class, but by the user of this class
private ThreadSafeQueue<string> _negative_unsent_confs = new ThreadSafeQueue<string>();
// in onData, data is queued here
private SyncQueue<byte[]> _in_data_queue = new SyncQueue<byte[]>();
private DateTime _last_hb = DateTime.Now;
private ConnectionStatusChange _conn_callback;
private DataDelegate _data_callback;
private SendConfirmationDelegate _on_send_onfirm_callback;
private readonly TimeSpan _connection_drop_threshold =
new TimeSpan(0, 0, 0, 0, PubSub3Config.instance.PS3_HeartBeatInterval * PubSub3Config.instance.PS3_MissingHeartBeatsForGC);
public PubSubServerManagedTCPConnection(string serverURL)
: this(new BlutoSubAddress(serverURL)) { }
public PubSubServerManagedTCPConnection(BlutoSubAddress serverAddress)
public PubSubServerManagedTCPConnection(BlutoSubAddress server_address)
{
myServerAddress = serverAddress;
ClientToServerManagedTCPConnector.connectTo(serverAddress, this);
_server_address = server_address;
_connect_to_ps_server();
}
private void _connect_to_ps_server()
{
bts.Log.Gen.info($"PubSubServerManagedTCPConnection._connect_to_ps_server server: {_server_address}");
ClientToServerManagedTCPConnector.connect_to(_server_address, this, TimeSpan.FromSeconds(1));
}
#region IPubSubServerConnection Members
public void sendWithConfirmation(byte[] data, ref string confId)
public void sendWithConfirmation(byte[] data, ref string conf_id)
{
if (myConnection != null)
bts.Log.Gen.info($"PubSubServerManagedTCPConnection.sendWithConfirmation server: {_server_address} data: {data.Length} myConnection : {_connection != null}");
if (_connection != null)
{
myConnection.sendWithConfirmation(data, ref confId, new SendConfirmationDelegate(myOnSendConfirm));
_connection.sendWithConfirmation(data, ref conf_id, (msg_id, confirmed) =>
{
if (_on_send_onfirm_callback != null)
_on_send_onfirm_callback(msg_id, confirmed);
});
}
else
{
confId = Guid.NewGuid().ToString();
myConfs.enqueue(new Pair<string, bool>(confId, false));
bts.Log.Gen.warn($"PubSubServerManagedTCPConnection.sendWithConfirmation server: {_server_address} no connection, buffering send");
conf_id = Guid.NewGuid().ToString();
_negative_unsent_confs.enqueue(conf_id);
}
}
public void send(byte[] data)
{
Console.WriteLine($"PubSubServerManagedTCPConnection.send server: {myServerAddress} data: {data.Length} myConnection : {myConnection != null}");
if (myConnection != null)
bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_address} data: {data.Length} myConnection : {_connection != null}");
if (_connection != null)
{
Console.WriteLine("PubSubServerManagedTCPConnection.send server: " + myServerAddress + "data: " + data.Length);
myConnection.send(data);
bts.Log.Gen.info("PubSubServerManagedTCPConnection.send server: " + _server_address + "data: " + data.Length);
_connection.send(data);
}
}
@@ -76,14 +92,25 @@ namespace BCG
send(br.ReadBytes(Convert.ToInt32(str.Length - str.Position)));
}
public void send(PubSubMessage msg)
{
send(msg.to_stream());
}
public void send_with_confirmation(PubSubMessage msg, ref string conf_id)
{
var bytes = msg.to_bytes();
sendWithConfirmation(bytes, ref conf_id);
}
public IList<byte[]> getUpdates(TimeSpan timeout, int maxElements)
{
return myDataQueue.dequeueMultipleElements(Convert.ToInt32(timeout.TotalMilliseconds), maxElements);
return _in_data_queue.dequeueMultipleElements(Convert.ToInt32(timeout.TotalMilliseconds), maxElements);
}
public string serverURL
{
get { return myServerAddress.ToString(); }
get { return _server_address.ToString(); }
}
public string clientURL
@@ -93,7 +120,7 @@ namespace BCG
string rv = "";
lock (this)
{
if (myConnection == null)
if (_connection == null)
{
rv = "";
}
@@ -103,7 +130,7 @@ namespace BCG
{
string ip;
int port;
myConnection.getSelfAddress(out ip, out port);
_connection.getSelfAddress(out ip, out port);
rv = new BlutoSubAddress(ip, port).ToString();
}
else
@@ -118,40 +145,40 @@ namespace BCG
public void registerCallback(ConnectionStatusChange del)
{
myConnCallback = del;
_conn_callback = del;
}
public void registerDataCallback(DataDelegate del)
{
myDataCallback = del;
_data_callback = del;
}
public void registerSendConfirmationCallback(SendConfirmationDelegate del)
{
myOnSendConfirmCallback = del;
_on_send_onfirm_callback = del;
}
#endregion
#region IDisposable Members
private bool myIsDisposed = false;
private bool _is_disposed = false;
public void Dispose()
{
Console.WriteLine("PubSubServerManagedTCPConnection.Dispose");
bts.Log.Gen.info("PubSubServerManagedTCPConnection.Dispose");
//throw new Exception("The method or operation is not implemented.");
if (myConnection != null)
if (_connection != null)
{
(myConnection as AsyncTCPConnection).Dispose();
_connection.Dispose();
}
myIsDisposed = true;
_is_disposed = true;
}
public bool isDisposed
{
get
{
return myIsDisposed;
return _is_disposed;
}
}
@@ -168,17 +195,40 @@ namespace BCG
/// <param name="message"></param>
public void onData(RemoteMessage message)
{
Console.WriteLine($"PubSubServerManagedTCPConnection.onData msg:type: {message.messageType}");
if (message.messageType == RemoteMessageType.data)
bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onData message: {message}");
// TODO check for disconenction
switch (message.messageType)
{
if (myDataCallback != null)
myDataCallback(message.remoteConnectionInfo.ToString(), message.message);
case RemoteMessageType.Data:
if (_data_callback != null)
_data_callback(message.remoteConnectionInfo.ToString(), message.message);
else
_in_data_queue.enqueue(message.message);
break;
case RemoteMessageType.Heartbeat:
_last_hb = DateTime.Now;
break;
case RemoteMessageType.Connection:
_conn_callback(_server_address.ToString(), true);
break;
case RemoteMessageType.Disconnection:
if (_conn_callback != null)
_conn_callback(_server_address.ToString(), false);
_connection.Dispose();
_connect_to_ps_server();
break;
}
if (message.messageType == RemoteMessageType.Data)
{
if (_data_callback != null)
_data_callback(message.remoteConnectionInfo.ToString(), message.message);
else
myDataQueue.enqueue(message.message);
_in_data_queue.enqueue(message.message);
}
else
{
myLastHB = DateTime.Now;
_last_hb = DateTime.Now;
}
}
@@ -197,16 +247,56 @@ namespace BCG
int port;
connection.getPeerAddress(out ip, out port);
BlutoSubAddress add = new BlutoSubAddress(ip, port);
Console.WriteLine($"PubSubServerManagedTCPConnection.onConnection add: {add.ToString()}");
if (add.ToString() == myServerAddress.ToString())
bts.Log.Gen.info($"PubSubServerManagedTCPConnection.onConnection add: {add.ToString()} connection type: {connection.GetType()}");
if (add.ToString() == _server_address.ToString())
{
myLastHB = DateTime.Now;
myTempNewConnection = connection;
_last_hb = DateTime.Now;
_new_connection = connection;
}
}
#endregion
void _dispose_and_reconnect_if_missing_hb()
{
if (!_connection.isDisposed)
{
TimeSpan hb_interval = DateTime.Now - _last_hb;
bool hb_missed = hb_interval > _connection_drop_threshold;
if (hb_missed)
{
if (_conn_callback != null)
_conn_callback(_server_address.ToString(), false);
bts.Log.Gen.info("PubSubServerManagedTCPConnection._dispose_and_reconnect_if_missing_hb disposing connecion: {myServerAddress}");
_connection.Dispose();
_connect_to_ps_server();
}
}
else
{
// already disposed, nothing to do
}
}
void _check_for_new_connection()
{
if (_new_connection != null)
{
_connection = _new_connection;
_new_connection = null; // since we are using its presence as a marker of nw connections
bts.Log.Gen.info("PubSubServerManagedTCPConnection._check_for_new_connection assigning new connection");
_connection.register(this);
if (_conn_callback != null)
_conn_callback(_server_address.ToString(), true);
if (_on_send_onfirm_callback != null)
{
var msg_ids = _negative_unsent_confs.dequeueAll();
foreach (var id in msg_ids)
_on_send_onfirm_callback(id, false);
}
}
}
#region IWorker Members
/// <summary>
@@ -216,55 +306,56 @@ namespace BCG
/// </summary>
public void doWork()
{
//_dispose_and_reconnect_if_missing_hb();
_check_for_new_connection();
///check for heartbeats and let clients know in
///case of missing hb (connection breaking)
TimeSpan curr_hb_interval = DateTime.Now - myLastHB;
bool hb_missed = curr_hb_interval > myConnectionDropThreshold;
bool conn_disposed = myConnection != null && myConnection.isDisposed;
if (conn_disposed || hb_missed)
{
if (hb_missed)
{
// since conn breakage was diagnosed logically, lets physically close the connection
myConnection.Dispose();
string dispose_msg = $"PubSubServerManagedTCPConnection.doWork server: {myServerAddress} Disposing connection conn_disposed: {conn_disposed} hb_missed: {hb_missed}";
Console.WriteLine(dispose_msg);
}
myConnection = null; // this is used as a marker for a dead connection
if (myConnCallback != null)
myConnCallback(myServerAddress.ToString(), false);
string reconn_msg = $"PubSubServerManagedTCPConnection.doWork disconnecting client from server: {myServerAddress} conn_disposed: {conn_disposed} hb_missed: {hb_missed} " +
$"because of missing hbs last_hb: {myLastHB}, now: {DateTime.Now} curr_hb_interval: {curr_hb_interval.TotalSeconds} threshold: {myConnectionDropThreshold.TotalSeconds} conn_disposed: {conn_disposed}";
Console.WriteLine(reconn_msg);
ClientToServerManagedTCPConnector.connectTo(myServerAddress, this);
}
if (myTempNewConnection != null)
{
if (myConnection != null)
{
// TODOds
// log error and close it
// BlueEventLog.logAssert(myConnection == null, "Connection has to be nulled out and disposed "
// + " before a new connection is established");
}
Console.WriteLine("PubSubServerManagedTCPConnection hooking new connection");
myConnection = myTempNewConnection;
myTempNewConnection = null;
myConnection.register(this);
myConfs.dequeueAll();
if (myConnCallback != null)
myConnCallback(myServerAddress.ToString(), true);
}
foreach (Pair<string, bool> p in myConfs.dequeueAll())
if (myOnSendConfirmCallback != null)
myOnSendConfirmCallback(p.first, p.second);
// TimeSpan hb_interval = DateTime.Now - myLastHB;
// bool hb_missed = hb_interval > myConnectionDropThreshold;
// bool conn_disposed = myConnection != null || myConnection.isDisposed;
// if (conn_disposed || hb_missed)
// {
// if (hb_missed)
// {
// // since conn breakage was diagnosed logically, lets physically close the connection
// myConnection.Dispose();
// string dispose_msg = $"PubSubServerManagedTCPConnection.doWork server: {myServerAddress} Disposing connection conn_disposed: {conn_disposed} hb_missed: {hb_missed}";
// Console.WriteLine(dispose_msg);
// }
// myConnection = null; // this is used as a marker for a dead connection
// if (myConnCallback != null)
// myConnCallback(myServerAddress.ToString(), false);
// string reconn_msg = $"PubSubServerManagedTCPConnection.doWork disconnecting client from server: {myServerAddress} conn_disposed: {conn_disposed} hb_missed: {hb_missed} " +
// $"because of missing hbs last_hb: {myLastHB}, now: {DateTime.Now} curr_hb_interval: {hb_interval.TotalSeconds} threshold: {myConnectionDropThreshold.TotalSeconds} conn_disposed: {conn_disposed}";
// ClientToServerManagedTCPConnector.connectTo(myServerAddress, this);
// }
// if (myTempNewConnection != null)
// {
// if (myConnection != null)
// {
// // TODOds
// // log error and close it
// // BlueEventLog.logAssert(myConnection == null, "Connection has to be nulled out and disposed "
// // + " before a new connection is established");
// }
// Console.WriteLine("PubSubServerManagedTCPConnection hooking new connection");
// myConnection = myTempNewConnection;
// myTempNewConnection = null;
// myConnection.register(this);
// myConfs.dequeueAll();
// if (myConnCallback != null)
// myConnCallback(myServerAddress.ToString(), true);
// }
// foreach (Pair<string, bool> p in myConfs.dequeueAll())
// if (myOnSendConfirmCallback != null)
// myOnSendConfirmCallback(p.first, p.second);
}
#endregion
private void myOnSendConfirm(string messageId, bool confirmed)
{
myConfs.enqueue(new Pair<string, bool>(messageId, confirmed));
}
// private void myOnSendConfirm(string messageId, bool confirmed) {
// _negative_confs.enqueue(new Pair<string, bool>(messageId, confirmed));
// }
}
}
@@ -7,144 +7,116 @@ using System.Text;
using BCG;
//using BCG.Utilities;
using System.Collections.Generic;
namespace BCG
{
public class MetaData : ICloneable
{
private Hashtable myHash = new Hashtable();
namespace BCG {
// Thhis "wraps" around the following 4 PubSub meta objects, serializes it on the client and
// deserialize it on the pubsub
// PubMetaData
// SubMetaData
// TransactionMetaData
// UnSubMetaData
public class MetaData : ICloneable {
private IDictionary _name_value_map = new Dictionary<string, string>();
int mySize = -1;
public MetaData()
{}
public MetaData() { }
public MetaData(object o)
{copyFrom(o);}
public MetaData(object o) { copyFrom(o); }
public MetaData(Stream s)
{
public MetaData(Stream s) {
long initPos = s.Position;
load(s);
mySize = Convert.ToInt32(s.Position - initPos);
}
public string this[string name]
{
get
{return (string) myHash[name];}
set
{
public string this[string name] {
get { return (string)_name_value_map[name]; }
set {
if (value != null)
myHash[name] = value;
_name_value_map[name] = value;
}
}
public void add(string name, string [] values)
{
private void _add_string_array(string name, string[] values) {
StringBuilder sb = new StringBuilder();
foreach (string s in values)
{
foreach (string s in values) {
if (sb.Length != 0)
sb.Append(" ");
sb.Append(s);
}
myHash[name] = sb.ToString();
_name_value_map[name] = sb.ToString();
}
public int getAsInt(string name)
{return Int32.Parse(this[name]);}
private int getAsInt(string name) { return Int32.Parse(this[name]); }
public string[] getAsStringArray (string name)
{
private string[] _get_as_string_array(string name) {
if (this[name] != "" && this[name] != null)
return this[name].Split(' ');
else
return new string[0];
}
public int size()
{
if (mySize < 0)
{
public int size() {
if (mySize < 0) {
MemoryStream ms = new MemoryStream();
this.save(ms);
return mySize = (int)ms.Position;
}
else
} else
return mySize;
}
public bool isEqual (MetaData md)
{
if (md.myHash.Keys.Count != myHash.Keys.Count)
private bool isEqual(MetaData md) {
if (md._name_value_map.Keys.Count != _name_value_map.Keys.Count)
return false;
foreach (string k in myHash.Keys)
if (!myHash[k].Equals(md.myHash[k]))
foreach (string k in _name_value_map.Keys)
if (!_name_value_map[k].Equals(md._name_value_map[k]))
return false;
return true;
}
public void load(Stream s)
{
myHash = new Hashtable();
string [] state =
// loads the content of the stream into this instance's map
private void load(Stream s) {
_name_value_map = new Hashtable();
string[] state =
new BinaryReader(s).ReadString().Split('\n');
for (int i = 0; i < state.Length-1; i+= 2)
myHash[state[i]] = state[i+1];
for (int i = 0; i < state.Length - 1; i += 2)
_name_value_map[state[i]] = state[i + 1];
}
public void save(Stream s)
{
// writes the content of this instance to stream s
public void save(Stream s) {
new BinaryWriter(s).Write(this.ToString());
}
public override string ToString()
{
public override string ToString() {
StringBuilder sb = new StringBuilder();
foreach (string key in myHash.Keys)
sb.AppendFormat("{0}\n{1}\n",key,myHash[key]);
foreach (string key in _name_value_map.Keys)
sb.AppendFormat("{0}\n{1}\n", key, _name_value_map[key]);
return sb.ToString();
}
public void copyFrom(object o)
{
// iterates through the fields and enters the name, values into _hash
public void copyFrom(object o) {
this["op-code"] = o.GetType().Name;
foreach (FieldInfo fi in o.GetType().GetFields())
{
foreach (FieldInfo fi in o.GetType().GetFields()) {
if (fi.GetValue(o) == null)
continue;
else if (fi.GetValue(o) is String[])
add(fi.Name, (string[])fi.GetValue(o));
_add_string_array(fi.Name, (string[])fi.GetValue(o));
else
myHash[fi.Name] = fi.GetValue(o).ToString();
_name_value_map[fi.Name] = fi.GetValue(o).ToString();
}
}
public void copyTo(object o)
{
foreach (FieldInfo fi in o.GetType().GetFields())
{
// copies the name value map into object o
public void copyTo(object o) {
foreach (FieldInfo fi in o.GetType().GetFields()) {
string value = this[fi.Name];
//this should only be here temporarily, while the new decompression changes are waiting to go into prod.
if (value == null)
{
//BlueEventLog.logEvent("Error: Metadata missing field " + fi.Name, BlueEventLog2Level.MissingMetadata);
}
else
{
if (value == null) {
} else {
if (fi.FieldType.Equals(typeof(string)))
fi.SetValue(o, value);
else if (fi.FieldType.Equals(typeof(int)))
@@ -167,11 +139,10 @@ namespace BCG
}
}
public object Clone()
{
public object Clone() {
MemoryStream ms = new MemoryStream();
this.save(ms);
ms.Seek(0,SeekOrigin.Begin);
ms.Seek(0, SeekOrigin.Begin);
return new MetaData(ms);
}
}
@@ -1,25 +1,25 @@
using System;
// using System;
namespace BCG
{
[Serializable]
public class RenewMetaData
{
public string subscriber;
public int lease;
public bool c;
public SubAddress getSubAddress()
{return SubAddress.decode(subscriber);}
// namespace BCG
// {
// [Serializable]
// public class RenewMetaData
// {
// public string subscriber;
// public int lease;
// public bool c;
public RenewMetaData()
{}
// public SubAddress getSubAddress()
// {return SubAddress.decode(subscriber);}
public RenewMetaData(string s,
int lease)
{
this.subscriber = s;
this.lease = lease;
}
}
}
// public RenewMetaData()
// {}
// public RenewMetaData(string s,
// int lease)
// {
// this.subscriber = s;
// this.lease = lease;
// }
// }
// }
@@ -8,7 +8,8 @@ using System.IO;
using BCG.Utilities.General;
using System.Runtime.InteropServices;
namespace BCG {
namespace BCG
{
/// <summary>
/// This class, mantains a connection to a peer socket, using
/// the .Net async TCP library. It sends heartbeats at regular
@@ -28,14 +29,16 @@ namespace BCG {
/// methods indicate an error, missing heartbeats is guaranteed
/// to detect it.
/// </summary>
public class AsyncTCPConnection : ITCPConnection, IWorker, IDisposable {
public class AsyncTCPConnection : ITCPConnection, IWorker, IDisposable
{
/// <summary>
/// This is used as a state for the Async send. Reason being
/// that if onSend indicates that it could not send the full
/// data (ie EndSend does not return the full size), the offset
/// is used to resend the data
/// </summary>
public class AsyncMessage {
public class AsyncMessage
{
public byte[] buffer;
public int offset;
public string messageId;
@@ -44,7 +47,8 @@ namespace BCG {
public AsyncMessage(byte[] theBuffer, int theOffset)
: this(theBuffer, theOffset, String.Empty, null) { }
public AsyncMessage(byte[] theBuffer, int theOffset, string theMessageId, SendConfirmationDelegate theOnSendDel) {
public AsyncMessage(byte[] theBuffer, int theOffset, string theMessageId, SendConfirmationDelegate theOnSendDel)
{
buffer = theBuffer;
offset = theOffset;
messageId = theMessageId;
@@ -52,14 +56,17 @@ namespace BCG {
}
public int length {
get {
public int length
{
get
{
return buffer.Length - offset;
}
}
}
enum ReadState {
enum ReadState
{
readingSize,
readingData
};
@@ -67,13 +74,15 @@ namespace BCG {
#region work around for .Net socket's LocalEndPoint bug
[StructLayout(LayoutKind.Sequential)]
private struct in_addr {
private struct in_addr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public byte[] sin_addr;
}
[StructLayout(LayoutKind.Sequential)]
private struct sockaddr {
private struct sockaddr
{
public short sin_family;
public ushort sin_port;
public in_addr sin_addr;
@@ -106,7 +115,8 @@ namespace BCG {
private bool myHBReceived = false, myDataReceived = false, myHBSent = false;
private DateTime myLastHBReceivedTime = new DateTime(1900, 1, 1);
public AsyncTCPConnection(Socket sock, TimeSpan heartBeatTimeSpan) {
public AsyncTCPConnection(Socket sock, TimeSpan heartBeatTimeSpan)
{
myHBTimeSpan = heartBeatTimeSpan;
mySock = sock;
mySock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, true);
@@ -120,24 +130,28 @@ namespace BCG {
// mySelf.ToString(), myPeer.ToString()), PS3LogLevels.connection);
}
public override string ToString() {
public override string ToString()
{
return String.Format("peer = {0}, self = {1}", myPeer.ToString(), mySelf.ToString());
}
#region ITCPConnection Members
public void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del) {
public void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del)
{
confId = Guid.NewGuid().ToString();
byte[] msg = myBuildDataMessage(data);
myBeginOrResumeSending(new AsyncMessage(msg, 0, confId, del));
}
public void send(byte[] data) {
public void send(byte[] data)
{
byte[] msg = myBuildDataMessage(data);
myBeginOrResumeSending(new AsyncMessage(msg, 0));
}
public void register(ITCPMessageHandler messageHandler) {
public void register(ITCPMessageHandler messageHandler)
{
// BlueEventLog.logAssert(myMessageHandler == null, "Message Handler is already assigned");
// BlueEventLog.logEvent(String.Format("AsyncTCPConnection for peer = {0} being registered", myPeer.ToString()),
// PS3LogLevels.connection);
@@ -146,10 +160,12 @@ namespace BCG {
myReadSize();
}
public void getPeerAddress(out string ip, out int port) {
public void getPeerAddress(out string ip, out int port)
{
ip = "";
port = 0;
if (myAlive) {
if (myAlive)
{
IPEndPoint ipEp = mySock.RemoteEndPoint as IPEndPoint;
ip = ipEp.Address.ToString();
port = ipEp.Port;
@@ -162,10 +178,12 @@ namespace BCG {
//port = addr.sin_port;
}
public void getSelfAddress(out string ip, out int port) {
public void getSelfAddress(out string ip, out int port)
{
ip = "";
port = 0;
if (myAlive) {
if (myAlive)
{
IPEndPoint ipEp = mySock.LocalEndPoint as IPEndPoint;
ip = ipEp.Address.ToString();
port = ipEp.Port;
@@ -178,7 +196,8 @@ namespace BCG {
//port = addr.sin_port;
}
public bool isDisposed {
public bool isDisposed
{
get { return !myAlive; }
}
@@ -187,14 +206,16 @@ namespace BCG {
#region IWorker Members
bool _last_connected = false;
public void doWork() {
public void doWork()
{
if (mySock != null)
//if (mySock.Connected != _last_connected)
{
Console.WriteLine($"AsyncTCPConnection.doWork connected: {mySock.Connected}");
_last_connected = mySock.Connected;
}
if (!myHBSent) {
if (!myHBSent)
{
// BlueEventLog.logEvent(String.Format("Sending heartbeat to peer {0} for the first time", myPeer.ToString()),
// PS3LogLevels.connection);
myHBSent = true;
@@ -206,9 +227,12 @@ namespace BCG {
#region sending data
private void myBeginOrResumeSending(AsyncMessage message) {
if (myAlive) {
try {
private void myBeginOrResumeSending(AsyncMessage message)
{
if (myAlive)
{
try
{
//Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} connected: {mySock.Connected}");
SocketError beginSendErr;
mySock.BeginSend(message.buffer, message.offset, message.length, SocketFlags.None,
@@ -216,36 +240,50 @@ namespace BCG {
Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeSending len: {message.length} socker_error: {beginSendErr} connected: {mySock.Connected}");
if (beginSendErr != SocketError.Success)
myHandleError(beginSendErr);
} catch (Exception ex) {
}
catch (Exception ex)
{
myHandleError(ex);
}
}
}
private void myOnSend(IAsyncResult res) {
if (myAlive) {
try {
private void myOnSend(IAsyncResult res)
{
if (myAlive)
{
try
{
SocketError writeErr;
int bytesSent = mySock.EndSend(res, out writeErr);
Console.WriteLine($"AsyncTCPConnection.myOnSend bytes: {bytesSent} writeErr: {writeErr} connected: {mySock.Connected}");
AsyncMessage message = res.AsyncState as AsyncMessage;
message.offset += bytesSent;
if (writeErr == SocketError.Success) {
if (message.offset == message.buffer.Length) {
if (writeErr == SocketError.Success)
{
if (message.offset == message.buffer.Length)
{
// if confirmation needs to be sent
if (String.Empty != message.messageId) {
if (String.Empty != message.messageId)
{
message.onSendDel(message.messageId, true);
}
} else {
}
else
{
///resume writing rest of the message
// BlueEventLog.logEvent("could not send the whole message all at once , messageSize = " +
// message.buffer.Length + " offset = " + message.offset, PS3LogLevels.info);
myBeginOrResumeSending(message);
}
} else {
}
else
{
myHandleError(writeErr);
}
} catch (Exception ex) {
}
catch (Exception ex)
{
myHandleError(ex);
}
}
@@ -256,8 +294,10 @@ namespace BCG {
/// future references
/// </summary>
/// <returns></returns>
private byte[] myBuildHeartbeatMessage() {
if (myHBMessage == null) {
private byte[] myBuildHeartbeatMessage()
{
if (myHBMessage == null)
{
BinaryWriter bw = new BinaryWriter(new MemoryStream());
int sz = 4;
int msgType = 1;
@@ -276,7 +316,8 @@ namespace BCG {
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private byte[] myBuildDataMessage(byte[] data) {
private byte[] myBuildDataMessage(byte[] data)
{
BinaryWriter bw = new BinaryWriter(new MemoryStream());
bw.Write(data.Length + 8);
bw.Write(0);
@@ -289,25 +330,31 @@ namespace BCG {
#region receiving data
private void myReadSize() {
private void myReadSize()
{
myCurrentReadBuffer = new byte[4];
myCurrentReadBufferOffset = 0;
myCurrentReadState = ReadState.readingSize;
myBeginOrResumeReading();
}
private void myReadData(int dataSize) {
private void myReadData(int dataSize)
{
myCurrentReadBuffer = new byte[dataSize];
myCurrentReadBufferOffset = 0;
myCurrentReadState = ReadState.readingData;
myBeginOrResumeReading();
}
private void myBeginOrResumeReading() {
if (myAlive) {
try {
private void myBeginOrResumeReading()
{
if (myAlive)
{
try
{
int beginReceiveAttempts = 0;
while (true) {
while (true)
{
beginReceiveAttempts++;
SocketError readSockErr;
mySock.BeginReceive(myCurrentReadBuffer, myCurrentReadBufferOffset,
@@ -316,7 +363,8 @@ namespace BCG {
new AsyncCallback(myOnRead),
myCurrentReadBuffer);
Console.WriteLine($"AsyncTCPConnection.myBeginOrResumeReading socket_error: {readSockErr} connected: {mySock.Connected}");
if (readSockErr == SocketError.NoBufferSpaceAvailable) {
if (readSockErr == SocketError.NoBufferSpaceAvailable)
{
string msg = String.Format("Not enough buffer space to receive {0} bytes. Attempt: {1} of {2}.",
myCurrentReadBuffer.Length - myCurrentReadBufferOffset,
beginReceiveAttempts, PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1);
@@ -324,81 +372,108 @@ namespace BCG {
if (beginReceiveAttempts < (PubSub3Config.instance.PS3_retriesOnNoBufferSpace + 1))
System.Threading.Thread.Sleep(PubSub3Config.instance.PS3_millisecondsBetweenNoBufferSpaceRetries);
else {
else
{
//BlueEventLog.logEvent("Could not recover from NoBufferSpace condition. Breaking connection.", PS3LogLevels.disconnection);
myHandleError(readSockErr);
}
} else if (readSockErr == SocketError.Success) {
}
else if (readSockErr == SocketError.Success)
{
if (beginReceiveAttempts > 1)
// BlueEventLog.logEvent(String.Format("Recovered from NoBufferSpace after {0} tries",beginReceiveAttempts-1) ,
// PS3LogLevels.disconnection);
break;
} else
}
else
myHandleError(readSockErr);
}
} catch (Exception ex) {
}
catch (Exception ex)
{
myHandleError(ex);
}
}
}
private void myOnRead(IAsyncResult res) {
if (myAlive) {
try {
private void myOnRead(IAsyncResult res)
{
if (myAlive)
{
try
{
SocketError readErr;
Console.WriteLine($"AsyncTCPConnection.myOnRead pre connected: {mySock.Connected}");
int bytesRead = mySock.EndReceive(res, out readErr);
Console.WriteLine($"AsyncTCPConnection.myOnRead bytes: {bytesRead} readErr: {readErr} connected: {mySock.Connected}");
myCurrentReadBufferOffset += bytesRead;
if (readErr == SocketError.Success) {
if (myCurrentReadBufferOffset == myCurrentReadBuffer.Length) {
if (readErr == SocketError.Success)
{
if (myCurrentReadBufferOffset == myCurrentReadBuffer.Length)
{
///read the whole of the buffer
if (myCurrentReadState == ReadState.readingSize) {
if (myCurrentReadState == ReadState.readingSize)
{
///the size has already been read, now convert
///it into an int, and start reading the data
int dataSize = new BinaryReader(new MemoryStream(myCurrentReadBuffer)).ReadInt32();
if (dataSize > myMaxMessageSize) {
if (dataSize > myMaxMessageSize)
{
string errMessage = "Encountered malformed message, size indicated = " + dataSize;
//BlueEventLog.logEvent(errMessage, "Error");
myHandleError(new ApplicationException(errMessage));
} else
}
else
myReadData(dataSize);
} else {
}
else
{
///the whole data has been finally read, time to
///tell the subscriber about it
BinaryReader br = new BinaryReader(new MemoryStream(myCurrentReadBuffer));
int msgType = br.ReadInt32();
if (msgType == 0) {
if (msgType == 0)
{
///skips the message ID
br.ReadInt32();
byte[] data = br.ReadBytes(myCurrentReadBuffer.Length - 8);
///callbacks can be made through thread pool to avoid this
///thread from being blocked, by this callback
if (!myDataReceived) {
if (!myDataReceived)
{
myDataReceived = true;
Console.WriteLine($"AsyncTCPConnection Data received for the first time from peer {myPeer.ToString()}");
}
myMessageHandler.onData(new RemoteMessage(data, RemoteMessageType.data, myPeer));
myMessageHandler.onData(new RemoteMessage(data, RemoteMessageType.Data, myPeer));
} else if (msgType == 1) {
if (!myHBReceived) {
}
else if (msgType == 1)
{
if (!myHBReceived)
{
myHBReceived = true;
Console.WriteLine($"AsyncTCPConnection HB received for the first time from peer {myPeer.ToString()}");
}
myLastHBReceivedTime = DateTime.Now;
myMessageHandler.onData(new RemoteMessage(null, RemoteMessageType.heartbeat, myPeer));
myMessageHandler.onData(new RemoteMessage(null, RemoteMessageType.Heartbeat, myPeer));
}
myReadSize();
}
} else {
}
else
{
///still part of the buffer left to read
///continue reading
myBeginOrResumeReading();
}
} else {
}
else
{
myHandleError(readErr);
}
} catch (Exception ex) {
}
catch (Exception ex)
{
myHandleError(ex);
}
}
@@ -408,18 +483,24 @@ namespace BCG {
#region IDisposable Members
public void Dispose() {
public void Dispose()
{
Console.WriteLine("AsyncTCPConnection.Dispose()");
lock (this) {
if (myAlive) {
lock (this)
{
if (myAlive)
{
myMasterWorker.removeWorker(this);
myAlive = false;
// BlueEventLog.logEvent(String.Format("AsycnTCPConnection with peer {0} being disposed, last HB was received at {1}" ,
// myPeer.ToString(), myLastHBReceivedTime.ToLongTimeString()),
// PS3LogLevels.disconnection);
try {
try
{
mySock.Close();
} catch (Exception ex) {
}
catch (Exception ex)
{
String.Format("Exception of type {0} thrown while trying to close socket connected to {1} , with" +
" the following exception msg {2}", ex.GetType().ToString(), myPeer.ToString(), ex.Message);
//BlueEventLog.logEvent(ex);
@@ -432,21 +513,27 @@ namespace BCG {
#endregion
private void myHandleError(object err) {
lock (this) {
private void myHandleError(object err)
{
lock (this)
{
string errMsg = "";
if (err is Exception) {
if (err is Exception)
{
errMsg = String.Format("AsyncTCPConnection.Dispose Encountered an exception of type '{0}' on the socket connected to ip = {1} , port = {2} ; " +
"Exception message = {3} , if the socket has not been disposed, it will be disposed now", err.GetType(), myPeer.ip, myPeer.port, (err as Exception).Message);
if (err is OutOfMemoryException)
throw (err as OutOfMemoryException);
} else if (err is SocketError) {
}
else if (err is SocketError)
{
errMsg = String.Format("AsyncTCPConnection.Dispose Encountered a SocketError of type '{0}' on the socket connected to ip = {1} , port = {2} ; " +
"if the socket has not been disposed, it will be disposed now", err.ToString(), myPeer.ip, myPeer.port);
Console.WriteLine($"AsyncTCPConnection.myHandleError errMsg: {errMsg}");
}
//BlueEventLog.logEvent(errMsg, PS3LogLevels.disconnection);
if (myAlive) {
if (myAlive)
{
Dispose();
}
}
@@ -119,7 +119,7 @@ namespace BCG
try
{
byte[] msg = await TCPSocketUtils.read_bytes_async(_nw_stream);
//Console.WriteLine($"_keep_reading sz: {msg.Length}");
bts.Log.Gen.info($"AsyncTCPConnection2._keep_reading side: {_side} self: {get_self_address()} peer: {get_peer_address} msg len: {msg.Length}");
await _handle_msg(msg);
}
catch (Exception ex)
@@ -139,12 +139,12 @@ namespace BCG
if (msg_type == _msg_type_data)
{
byte[] data = br.ReadBytes(msg.Length - 1);
RemoteMessage m = new RemoteMessage(data, RemoteMessageType.data, _peer_address);
RemoteMessage m = new RemoteMessage(data, RemoteMessageType.Data, _peer_address);
_message_handler.onData(m);
}
else
{
RemoteMessage m = new RemoteMessage(null, RemoteMessageType.heartbeat, _peer_address);
RemoteMessage m = new RemoteMessage(null, RemoteMessageType.Heartbeat, _peer_address);
_message_handler.onData(m);
}
}
@@ -174,7 +174,7 @@ namespace BCG
bts.Log.Gen.error($"TCPConnection._handle_error got exception when closing Socket ex: {ex}");
}
_is_disposed = true;
RemoteMessage rm = new RemoteMessage(new byte[] { }, RemoteMessageType.disconnection, _peer_address);
RemoteMessage rm = new RemoteMessage(new byte[] { }, RemoteMessageType.Disconnection, _peer_address);
_message_handler.onData(rm);
}
@@ -194,14 +194,17 @@ namespace BCG
}
}
public void sendWithConfirmation(byte[] data, ref string confId, SendConfirmationDelegate del)
public void sendWithConfirmation(byte[] data, ref string conf_id, SendConfirmationDelegate del)
{
confId = String.Empty;
conf_id = (++_msg_id).ToString();
send(data);
del(conf_id, true);
}
public void send(byte[] data)
{
Task.Run(async () => await send_async(data));
bts.Log.Gen.info($"AsyncTCPConnection2.send self: {get_self_address()} peer: {get_peer_address} msg len: {data.Length} sent successfully");
//_data_send_queue.enqueue(new QueuedMessage(data));
}
@@ -242,6 +245,13 @@ namespace BCG
return new RemoteConnectionInfo(ip, port);
}
public RemoteConnectionInfo get_self_address()
{
string ip; int port;
getSelfAddress(out ip, out port);
return new RemoteConnectionInfo(ip, port);
}
public void getSelfAddress(out string ip, out int port)
{
@@ -269,7 +279,9 @@ namespace BCG
public void Dispose()
{
_nw_stream.Close();
_nw_stream.Dispose();
_is_disposed = true;
}
public bool isDisposed
@@ -2,12 +2,14 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace BCG {
namespace BCG
{
/// <summary>
/// Type of messages that can be sent over the transport.
/// </summary>
public enum RemoteMessageType {
heartbeat, data, connection, disconnection
public enum RemoteMessageType
{
Heartbeat, Data, Connection, Disconnection
}
/// <summary>
@@ -15,30 +17,36 @@ namespace BCG {
/// This can be a transport generated message (heartbeat) or user generated
/// message (data)
/// </summary>
public class RemoteMessage {
public class RemoteMessage
{
private byte[] myMsg;
private RemoteMessageType myMsgType;
private RemoteConnectionInfo myConnInfo;
public RemoteMessage(byte[] message, RemoteMessageType messageType, RemoteConnectionInfo connInfo) {
public RemoteMessage(byte[] message, RemoteMessageType messageType, RemoteConnectionInfo connInfo)
{
myMsg = message;
myMsgType = messageType;
myConnInfo = connInfo;
}
public byte[] message {
public byte[] message
{
get { return myMsg; }
}
public RemoteMessageType messageType {
public RemoteMessageType messageType
{
get { return myMsgType; }
}
public RemoteConnectionInfo remoteConnectionInfo {
public RemoteConnectionInfo remoteConnectionInfo
{
get { return myConnInfo; }
}
override public string ToString() {
override public string ToString()
{
Func<byte[], int> len = (byte[] m) => { return m == null ? 0 : m.Length; };
return $"type: {myMsgType} conn: {myConnInfo} msg: {len(myMsg)}";
}
@@ -44,9 +44,9 @@ namespace BCG {
// BlueEventLog.logEvent(msg, BlueEventLog2Level.Error);
return port;
} else {
SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
int assignedport = theAcceptor.activate(handler, port, clientHBTimeSpan);
myPortToSingleServerAcceptorMap[assignedport] = theAcceptor;
SingleServerToClientManagedTCPConnectionAcceptor ps_acceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
int assignedport = ps_acceptor.accept_on_port(handler, "127.0.0.1", port, clientHBTimeSpan);
myPortToSingleServerAcceptorMap[assignedport] = ps_acceptor;
return assignedport;
}
} //end lock
@@ -68,7 +68,7 @@ namespace BCG {
return myPort; // not sure why we need this class variable myPort, but I am just keeping it up with the existing code
}
public int accept_on_port(ITCPConnectionHandler handler, string ip, int port, TimeSpan clientHBTimeSpan) {
public int accept_on_port(ITCPConnectionHandler handler, string ip, int port, TimeSpan client_HB_timespan) {
Task.Run(async () => {
await TCPSocketUtils.accept_connections_async(ip, port, handler, TimeSpan.FromSeconds(1));
});
+23 -15
View File
@@ -4,31 +4,39 @@ using System.IO;
using BCG;
using BCG.Utilities;
namespace BCG
{
// Tuple of
public class PubSubMessage
{
public PubSubMessage (MetaData metaData, Stream body)
{this.metaData = metaData; this.body = body;}
namespace BCG {
// PubItems are published to the pubsub wrapped in this class. It contains PubMetaData, wrapped in the MetaData
// object and the actualy published payload in the body stream
public class PubSubMessage {
public PubSubMessage(MetaData metaData, Stream body) { this.meta_data = metaData; this.body = body; }
public PubSubMessage (MetaData metaData)
{this.metaData = metaData;}
public PubSubMessage(PubMetaData pub_meta_data, Stream body)
: this(new MetaData(pub_meta_data), body) { }
public MetaData metaData;
public PubSubMessage(MetaData metaData) { this.meta_data = metaData; }
public MetaData meta_data;
public Stream body = new MemoryStream(0);
public Stream toStream()
{
public Stream to_stream() {
MemoryStream ms = new MemoryStream();
metaData.save(ms);
if (body.Length == 0 || body.Length == body.Position)
meta_data.save(ms);
if (body.Length == 0 || body.Length == body.Position) {
bts.Log.Gen.error("Error: Trying to publishing message with no content.");
Console.WriteLine("Error: Trying to publishing message with no content.");
}
StreamUtilityMethods.copyTo(new Stream[] { body }, ms);
ms.Position = 0;
return ms;
}
public byte[] to_bytes() {
return StreamUtilityMethods.getBytesFromStream(to_stream());
}
public override string ToString() {
return meta_data.ToString();
}
}
}
+7 -8
View File
@@ -2,22 +2,21 @@ using System;
using System.Collections;
using System.IO;
namespace BCG
{
namespace BCG {
/// <summary>
/// this is the data returned from the
/// pubsub server
/// </summary>
public class PubSubUpdate
{
public PubSubUpdate(string tag, string tagType, Stream body)
{ this.tag = tag; this.body = body; this.tagType = tagType; }
public class PubSubUpdate {
public PubSubUpdate(string tag, string tagType, Stream body) { this.tag = tag; this.body = body; this.tagType = tagType; }
public string tag;
public Stream body;
public string tagType;
public override string ToString() {
return $"PubSubUpdate tag: {tag} tag_type: {tagType} len: {body.Position}";
}
};
}
@@ -9,17 +9,14 @@ using System.IO;
using System.Diagnostics;
using mbts;
namespace BCG
{
namespace BCG {
public class ManagedTCPMessageHub : IPubsubMessageHub,
ITCPConnectionHandler,
ITCPMessageHandler
{
ITCPMessageHandler {
private int myPort;
private bool myIsDisposed = false;
public int port
{
public int port {
get { return myPort; }
//property is read only
@@ -66,8 +63,7 @@ namespace BCG
private DateTime myLastHeartbeatDeadCollectionTime = DateTime.Now;
private DateTime myLastDisposedDeadCollectionTime = DateTime.Now;
public ManagedTCPMessageHub(int port, TimeSpan hbSendInterval, int missingHBsForGC, TimeSpan collectDisposedConnectionsPeriod)
{
public ManagedTCPMessageHub(int port, TimeSpan hbSendInterval, int missingHBsForGC, TimeSpan collectDisposedConnectionsPeriod) {
myPort = port;
myHeartbeatSendInterval = hbSendInterval;
mySubscriberDropThreshold = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(hbSendInterval.TotalMilliseconds * missingHBsForGC));
@@ -81,24 +77,21 @@ namespace BCG
#region IPubsubMessageHub Members
public void registerDataCallback(DataDelegate del)
{
public void registerDataCallback(DataDelegate del) {
// BlueEventLog.logAssert(!myActivated, "connection callbacks should be registered before the call to activate");
myDataCb = del;
}
public void registerConnectionCallback(ConnectionStatusChange del)
{
public void registerConnectionCallback(ConnectionStatusChange del) {
//BlueEventLog.logAssert(!myActivated, "data callbacks should be registered before the call to activate");
myConnCb = del;
}
public void activate()
{
public void activate() {
if (PubSub3Config.instance.PS3_BindToRandomPort || myPort == 0)
myPort = ServerToClientManagedTCPConnectionAcceptor.activateWithRandomPort(this, myHeartbeatSendInterval);
else
myPort = ServerToClientManagedTCPConnectionAcceptor.activate(this, myPort, myHeartbeatSendInterval);
myPort = ServerToClientManagedTCPConnectionAcceptor.accept_connections(this, myPort, myHeartbeatSendInterval);
string msg = String.Format("TCP Component is ready, listening on port {0} for incoming connections", myPort);
Console.WriteLine(msg);
@@ -108,19 +101,16 @@ namespace BCG
doItAllThread.Start();
}
public List<Pair<string, byte[]>> getUpdates(TimeSpan timeout, int maxElements)
{
public List<Pair<string, byte[]>> getUpdates(TimeSpan timeout, int maxElements) {
return myReceivedData.dequeueMultipleElements(timeout, maxElements);
}
public bool send(string receiver, byte[] data)
{
public bool send(string receiver, byte[] data) {
mySendData.enqueue(new Pair<string, byte[]>(receiver, data));
return true;
}
public bool send(string receiver, System.IO.Stream str)
{
public bool send(string receiver, System.IO.Stream str) {
BinaryReader br = new BinaryReader(str);
return send(receiver, br.ReadBytes(Convert.ToInt32(str.Length - str.Position)));
}
@@ -133,17 +123,13 @@ namespace BCG
/// Reports back connected and disconnected connections
/// via the ConnectionStatucChange callback
/// </summary>
public void doWork()
{
if (myConnCb != null)
{
foreach (string nc in myNewConnectionURLs.dequeueAll())
{
public void doWork() {
if (myConnCb != null) {
foreach (string nc in myNewConnectionURLs.dequeueAll()) {
//BlueEventLog.logEvent(String.Format("doWork : New client {0} connected, making connection callback", nc), PS3LogLevels.connection);
myConnCb(nc, true);
}
foreach (string dc in myDeadConnectionURLs.dequeueAll())
{
foreach (string dc in myDeadConnectionURLs.dequeueAll()) {
//BlueEventLog.logEvent(String.Format("doWork : Client {0} disconnected, making disconnection callback", dc), PS3LogLevels.disconnection);
myConnCb(dc, false);
}
@@ -160,8 +146,7 @@ namespace BCG
/// stored in the hashtable and used for later communication)
/// </summary>
/// <param name="connection"></param>
public void onConnection(ITCPConnection connection)
{
public void onConnection(ITCPConnection connection) {
myNewConnections.enqueue(connection);
}
@@ -169,48 +154,38 @@ namespace BCG
#region ITCPMessageHandler Members
public void onData(RemoteMessage message)
{
public void onData(RemoteMessage message) {
Console.WriteLine("ManagedTCPMessgageHub.onData recv msg :" + message.remoteConnectionInfo.ToString());
string sender = new BlutoSubAddress(message.remoteConnectionInfo.ip, message.remoteConnectionInfo.port).ToString();
if (message.messageType == RemoteMessageType.data)
{
if (message.messageType == RemoteMessageType.Data) {
if (myDataCb != null)
myDataCb(sender, message.message);
else
myReceivedData.enqueue(new Pair<string, byte[]>(sender, message.message));
}
else if (message.messageType == RemoteMessageType.heartbeat)
} else if (message.messageType == RemoteMessageType.Heartbeat)
myReceivedHeartbeats.enqueue(sender);
else
{
else {
//BlueEventLog.logEvent("Unknown message type from the client", BlueEventLevel.Error);
}
}
#endregion
class TimmingInfo
{
public TimmingInfo()
{
class TimmingInfo {
public TimmingInfo() {
myTimer = new Stopwatch();
reset();
}
public void start()
{
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0)
{
public void start() {
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0) {
myTimer.Reset();
myTimer.Start();
}
}
public void stop()
{
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0)
{
public void stop() {
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0) {
myTimer.Stop();
myMin = Math.Min(myMin, myTimer.ElapsedMilliseconds);
myMax = Math.Max(myMax, myTimer.ElapsedMilliseconds);
@@ -219,16 +194,14 @@ namespace BCG
}
}
public void reset()
{
public void reset() {
myMin = Double.MaxValue;
myMax = Double.MinValue;
myTotal = 0;
myNMeasurements = 0;
}
public override string ToString()
{
public override string ToString() {
if (myNMeasurements > 0)
return String.Format("min={0} max={1} avg={2} nMeasurements={3}", myMin, myMax, myTotal / myNMeasurements, myNMeasurements);
else
@@ -242,8 +215,7 @@ namespace BCG
private double myTotal;
}
private void myDoItAll()
{
private void myDoItAll() {
Dictionary<string, TimmingInfo> times = new Dictionary<string, TimmingInfo>();
times.Add("myHandleNewConnections", new TimmingInfo());
times.Add("myCollectDisposedConnections", new TimmingInfo());
@@ -252,12 +224,9 @@ namespace BCG
DateTime lastTimmingLog = DateTime.Now;
while (true)
{
try
{
while (true)
{
while (true) {
try {
while (true) {
///new connections
times["myHandleNewConnections"].start();
myHandleNewConnections();
@@ -279,16 +248,12 @@ namespace BCG
Pair<string, byte[]> p = mySendData.dequeue(myReadSendQueueTimeout);
times["send"].start();
if (p != null)
{
if (p != null) {
ITCPConnection conn;
myConnections.TryGetValue(p.first, out conn);
if (conn != null)
{
if (conn != null) {
conn.send(p.second);
}
else
{
} else {
string warningMsg = String.Format("sending data to {0} failed, this client might have " +
"disconnected, in which case it will be garbage collected within 60 secs", p.first);
//BlueEventLog.logEvent(warningMsg, PS3LogLevels.disconnection);
@@ -297,25 +262,21 @@ namespace BCG
times["send"].stop();
if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0 &&
(DateTime.Now - lastTimmingLog).TotalSeconds > PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs)
{
(DateTime.Now - lastTimmingLog).TotalSeconds > PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs) {
lastTimmingLog = DateTime.Now;
StringBuilder b = new StringBuilder();
b.AppendFormat("myDoItAll stats:\n");
b.AppendFormat("queue size: {0}\n", mySendData.getCount());
foreach (string timerName in times.Keys)
{
foreach (string timerName in times.Keys) {
b.AppendFormat("{0}: {1}\n", timerName, times[timerName]);
times[timerName].reset();
}
//BlueEventLog.logEvent(b.ToString(), PS3LogLevels.performance);
}
}
}
catch (Exception ex)
{
} catch (Exception ex) {
//BlueEventLog.logEvent(ex);
if (ex is OutOfMemoryException)
throw ex;
@@ -323,19 +284,15 @@ namespace BCG
}
}
private void myCollectDisposedConnections()
{
if (DateTime.Now - myLastDisposedDeadCollectionTime > myCollectDisposedConnectionsPeriod)
{
private void myCollectDisposedConnections() {
if (DateTime.Now - myLastDisposedDeadCollectionTime > myCollectDisposedConnectionsPeriod) {
myLastDisposedDeadCollectionTime = DateTime.Now;
List<string> disposedURLs = new List<string>();
foreach (KeyValuePair<string, ITCPConnection> p in myConnections)
{
foreach (KeyValuePair<string, ITCPConnection> p in myConnections) {
if (p.Value.isDisposed)
disposedURLs.Add(p.Key);
}
foreach (string url in disposedURLs)
{
foreach (string url in disposedURLs) {
string disposeMsg = String.Format("Collecting disposed connections : {0} is disposed by the connection itself, " +
"connection count before deleting this = {1}", url, myConnections.Count);
Console.WriteLine(disposeMsg);
@@ -346,10 +303,8 @@ namespace BCG
}
}
private void myCollectMissingHeartbeatConnections()
{
if (DateTime.Now - myLastHeartbeatDeadCollectionTime > mySubscriberDropThreshold)
{
private void myCollectMissingHeartbeatConnections() {
if (DateTime.Now - myLastHeartbeatDeadCollectionTime > mySubscriberDropThreshold) {
myLastHeartbeatDeadCollectionTime = DateTime.Now;
///these are all the hbs received bw the last
///and current collectDeadConnection calls
@@ -365,10 +320,9 @@ namespace BCG
foreach (string clientURL in myConnections.Keys)
if (!indexedHbs.ContainsKey(clientURL))
deadClients.Add(clientURL);
foreach (string deadClient in deadClients)
{
foreach (string deadClient in deadClients) {
string disposeMsg = String.Format("Collecting missing HB connections : {0} is dead due to missing hbs, Disposing it now, " +
"connection count before deleting this = {1}", deadClient, myConnections.Count);
"connection count before deleting this = {1}", deadClient, myConnections.Count);
//BlueEventLog.logEvent(disposeMsg, PS3LogLevels.disconnection);
Console.WriteLine(disposeMsg);
myConnections[deadClient].Dispose();
@@ -378,11 +332,9 @@ namespace BCG
}
}
private void myHandleNewConnections()
{
private void myHandleNewConnections() {
IList<ITCPConnection> newConns = myNewConnections.dequeueAll();
foreach (ITCPConnection conn in newConns)
{
foreach (ITCPConnection conn in newConns) {
string ip;
int port;
conn.getPeerAddress(out ip, out port);
@@ -399,13 +351,11 @@ namespace BCG
}
#region IDisposable Memebers
public void Dispose()
{
public void Dispose() {
myIsDisposed = true;
}
public bool isDisposed
{
public bool isDisposed {
get { return myIsDisposed; }
}
#endregion
@@ -4,23 +4,20 @@ using System.Text;
using BCG.Utilities.General;
using System.IO;
namespace BCG
{
namespace BCG {
/// <summary>
/// Periodicaly publishes a heartbeat, under the tag name System_Heartbeat.
/// A subscriber can subscribe to it, and find out of a PubSub is alive or
/// not. Infact more so, if the subsriptions are working or not.
/// </summary>
internal class SystemHeartbeatPublisher : IWorker
{
internal class SystemHeartbeatPublisher : IWorker {
public static string tagType = "System_Heartbeat";
public static string tagName = "System_Heartbeat";
private static SystemHeartbeatPublisher mySelf;
private SystemHeartbeatPublisher ()
{
private SystemHeartbeatPublisher() {
///this is using a multithreaded MasterWorker since the
///regular one is called within the MessageReactor, which
///waits 2500 msecs before calling doWork. This 2500 msecs
@@ -29,16 +26,14 @@ namespace BCG
MasterWorker.getInstance(true).addWorker(this, new TimeSpan(0, 0, 0, 0, 500));
}
public static void init()
{
public static void init() {
if (mySelf == null)
mySelf = new SystemHeartbeatPublisher();
}
#region IWorker Members
public void doWork()
{
public void doWork() {
PubMetaData pub = new PubMetaData(tagType, tagName, 1, "local", ClientInfo.thisClient.processString);
MetaData md = new MetaData(pub);
MemoryStream ms = new MemoryStream();
@@ -16,10 +16,8 @@ using System.Collections.Generic;
using mbts;
using mbts.spark;
namespace BCG
{
public class PubSubMessageReactor
{
namespace BCG {
public class PubSubMessageReactor {
private ServerMessageHub myMessageHub;
private delegate void MyMsgHandler(string sender, Stream msg, MetaData m);
private Dictionary<string, MyMsgHandler> myOperationToHandler = new Dictionary<string, MyMsgHandler>();
@@ -39,32 +37,24 @@ namespace BCG
/* This delegate allows pubsub components to be notified when the identity of the pubsub is known*/
public delegate void IdentityFoundDelegate(string identity, bool isRoot);
public event IdentityFoundDelegate onIdentityFound;
public void fireIdentityFound(string identity, bool isRoot)
{
public void fireIdentityFound(string identity, bool isRoot) {
if (onIdentityFound != null)
onIdentityFound(identity, isRoot);
}
private IPubSubGovernor myGovernor;
public Dictionary<string, bool> myTagCompressionState = new Dictionary<string, bool>();
public PubSubMessageReactor() : this(false, false, string.Empty, string.Empty) { }
public PubSubMessageReactor(string q) : this(false, false, string.Empty, q) { }
public PubSubMessageReactor(bool clearQueue, bool bootEvent, string oldIP, string psq)
{
public PubSubMessageReactor(bool clearQueue, bool bootEvent, string oldIP, string psq) {
myOperationToHandler.Add(typeof(PubMetaData).Name, new MyMsgHandler(myHandlePub));
myOperationToHandler.Add(typeof(SubMetaData).Name, new MyMsgHandler(myHandleSub));
myOperationToHandler.Add(typeof(UnSubMetaData).Name, new MyMsgHandler(myHandleUnsub));
myOperationToHandler.Add(typeof(TransactionMetaData).Name, new MyMsgHandler(myHandleTransaction));
// TODO, governor needs to be removed since its subststaion retired code
// if (PubSub3Config.instance.PS3_Components.governor != null)
// myGovernor = PubSubComponentFactory.buildGovernor(this);
//BlueEventLog.logEvent("System Initialized", PS3LogLevels.info);
bts.Log.Gen.info("PubSubMessageReactor.cons System Initialized");
myReceivingProcThread = new Thread(new ThreadStart(myReceivingThreadProc));
myMessageHub = ServerMessageHub.instance;
@@ -75,93 +65,52 @@ namespace BCG
#region message handlers
public PubSubState state
{
public PubSubState state {
get { return myState; }
//this variable is read only
}
private void myHandlePub(string sender, Stream msg, MetaData m)
{
private void myHandlePub(string sender, Stream msg_str, MetaData meta_data) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sender: {sender} metadata: {meta_data} str.pos: {msg_str.Position} str.len: {msg_str.Length}");
mySingleTagTimeSliceTimer.start();
///the position in the stream is set to where the data starts with,
///since the meta data was already read in the myProcessMsg
PubMetaData pub = new PubMetaData();
m.copyTo(pub);
meta_data.copyTo(pub);
pub.connection = sender;
Console.WriteLine("PubSubMessageReactor.myHandlePub sender: " + sender + " tag: " + pub.tag);
//check to see if this publish took a while to get here
pub.warnIfLatencyExceedsThreshold();
var subs_for_tag = myState.getSubscriberConnectionsForTag(pub.tag);
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub {subs_for_tag.Count} subs for tag: {pub.tag}");
//check the governor to see if this publish is allowed, if we have a governor
PubSubGovernorResponse response = PubSubGovernorResponse.Accepted;
if (myGovernor != null)
response = myGovernor.requestToPublish(pub, msg);
if (response != PubSubGovernorResponse.Accepted)
{
// BlueEventLog2.instance().logEvent(string.Concat("PubSub Governor ",
// myGovernor.GetType().Name,
// " returned response ",
// response.ToString(),
// " publish on tag",
// pub.tag,
// "was ignored."), BlueEventLog2Level.Warning);
return;
}
//report the fact that we have a publish on this tag, if it's a debug tag
if (PubSub3Config.instance.isTagDebugTag(pub.tag))
{
// BlueEventLog.logEvent(string.Concat(GetType().Name,
// " got publish on tag ",
// pub.tag,
// " from app ", pub.publishingApp,
// " over connection ",
// pub.connection, ".\n",
// pub.isCompressed ? "Data was compressed.\n" : "Data was not compresed.\n",
// "total size: ",
// pub.size),
// PS3LogLevels.debug);
}
///for all the subscribers, sends the messages over straightaway
foreach (string address in myState.getSubscriberConnectionsForTag(pub.tag))
{
foreach (string address in myState.getSubscriberConnectionsForTag(pub.tag)) {
///the position is adjusted to zero, so as to include
///the meta data as part of the message
msg.Position = 0;
myMessageHub.send(address, msg);
msg_str.Position = 0;
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sending tag: {pub.tag} to {address} len: {msg_str.Length - msg_str.Position}");
myMessageHub.send(address, msg_str);
}
//reset the position of the message so we can decompress it appropriately
msg.Position = 0;
msg.Position = m.size();
msg_str.Position = 0;
msg_str.Position = meta_data.size();
if (pub.isCompressed)
{
long mdLength = msg.Position;
long compressedSize = msg.Length - mdLength;
if (compressedSize == 0)
{
if (pub.isCompressed) {
long mdLength = msg_str.Position;
long compressedSize = msg_str.Length - mdLength;
if (compressedSize == 0) {
string errorMsg = "Error: tag " + pub.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
//BlueEventLog.logEvent(errorMsg, BlueEventLevel.Error);
return;
}
msg = StreamDecompressor.decompressStream(msg);
long decompressedSize = msg.Length - mdLength;
msg_str = StreamDecompressor.decompressStream(msg_str);
long decompressedSize = msg_str.Length - mdLength;
if (decompressedSize != pub.decompressedSize || compressedSize != pub.size)
{
if (decompressedSize != pub.decompressedSize || compressedSize != pub.size) {
string compressionWarning = string.Format("Warning: Stream for tag {0} decompressed from {1} to {2} bytes, with {3} byes of metadata. Metadata expected {4} to {5}. Tag state not updated.",
pub.tag,
compressedSize,
@@ -169,40 +118,31 @@ namespace BCG
mdLength,
pub.size,
pub.decompressedSize);
//BlueEventLog.logEvent(compressionWarning, BlueEventLevel.Error);
bts.Log.Gen.warn(compressionWarning);
return;
}
}
//give the user a notification of the compression status of the tag has changed.
if ((!(myTagCompressionState.ContainsKey(pub.tag)) ||
myTagCompressionState[pub.tag] != pub.isCompressed))
{
myTagCompressionState[pub.tag] != pub.isCompressed)) {
string compressionMessage;
myTagCompressionState[pub.tag] = pub.isCompressed;
if (pub.isCompressed)
{
if (pub.isCompressed) {
compressionMessage = "Tag " + pub.tag + " is compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
}
else
{
} else {
compressionMessage = "Tag " + pub.tag + " is not compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
}
}
///updating state
msg.Position = 0;
msg.Position += m.size();
myState.updatePubData(pub, msg);
msg_str.Position = 0;
msg_str.Position += meta_data.size();
myState.updatePubData(pub, msg_str);
mySingleTagTimeSliceTimer.stop();
@@ -213,8 +153,8 @@ namespace BCG
}
private void myHandleSub(string sender, Stream msg, MetaData m)
{
private void myHandleSub(string sender, Stream msg, MetaData m) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandleSub sender: {sender} metadata: {m} str.pos: {msg.Position} str.len: {msg.Length}");
SubMetaData sub = new SubMetaData();
m.copyTo(sub);
sub.connection = sender;
@@ -222,12 +162,10 @@ namespace BCG
///update the state
myState.sub(sub);
///to all these new subscriber s, send the existing data
foreach (string tag in sub.tags)
{
foreach (string tag in sub.tags) {
byte[] data = myState.getContent(tag);
//ie data has been published once
if (data != null)
{
if (data != null) {
string tagType = myState.getTagType(tag);
///passing "" as the 4th arg, as PubMeta is used both by the
///publisher to publish data to the server (where the publisher
@@ -237,17 +175,14 @@ namespace BCG
///needed as of now, so its left black to save a few bytes
PubMetaData pub;
MemoryStream realContent;
if (PubSub3Config.instance.isTagCompressed(tag))
{
if (PubSub3Config.instance.isTagCompressed(tag)) {
SharpZipCompressor Kompessor = new SharpZipCompressor();
realContent = new MemoryStream();
Kompessor.compress(new MemoryStream(data), realContent);
pub = new PubMetaData(tagType, tag, (int)realContent.Length, (int)data.Length, "", "");
}
else
{
} else {
realContent = new MemoryStream(data);
pub = new PubMetaData(tagType, tag, data.Length, "", "");
}
@@ -262,8 +197,7 @@ namespace BCG
}
}
private void myHandleUnsub(string sender, Stream msg, MetaData m)
{
private void myHandleUnsub(string sender, Stream msg, MetaData m) {
UnSubMetaData unsub = new UnSubMetaData();
m.copyTo(unsub);
if (unsub.connection.Trim().Length == 0)
@@ -271,8 +205,7 @@ namespace BCG
myState.unsub(unsub);
}
private void myHandleTransaction(string sender, Stream msg, MetaData md)
{
private void myHandleTransaction(string sender, Stream msg, MetaData md) {
mySingleTagTimeSliceTimer.start();
TransactionMetaData t = new TransactionMetaData();
md.copyTo(t);
@@ -280,8 +213,7 @@ namespace BCG
///form the structure tag->byte[]*
Dictionary<string, byte[]> tagToMessages = new Dictionary<string, byte[]>();
BinaryReader br = new BinaryReader(msg);
for (int i = 0; i < nMessages; i++)
{
for (int i = 0; i < nMessages; i++) {
MetaData pubMd = new MetaData(msg);
PubMetaData pub = new PubMetaData();
pubMd.copyTo(pub);
@@ -289,8 +221,7 @@ namespace BCG
byte[] tagData = new byte[pub.size];
msg.Read(tagData, 0, tagData.Length);
//report the fact that we have a publish on this tag, if it's a debug tag
if (PubSub3Config.instance.isTagDebugTag(pub.tag))
{
if (PubSub3Config.instance.isTagDebugTag(pub.tag)) {
// BlueEventLog.logEvent(string.Concat(GetType().Name,
// " got publish on tag ",
// pub.tag,
@@ -315,13 +246,11 @@ namespace BCG
}
///this gives subscriber->tag*
Dictionary<string, List<string>> subscriberToTags = myState.getSubscriberConnectionsForTags(tagToMessages.Keys);
foreach (KeyValuePair<string, List<string>> kvp in subscriberToTags)
{
foreach (KeyValuePair<string, List<string>> kvp in subscriberToTags) {
MemoryStream subStream = new MemoryStream();
TransactionMetaData subTransaction = new TransactionMetaData(kvp.Value.Count);
new MetaData(subTransaction).save(subStream);
foreach (string tag in kvp.Value)
{
foreach (string tag in kvp.Value) {
byte[] tagData = tagToMessages[tag];
PubMetaData pub = new PubMetaData(myState.getTagType(tag), tag, tagData.Length, "", "");
new MetaData(pub).save(subStream);
@@ -336,8 +265,7 @@ namespace BCG
double timeExpended = mySingleTagTimeSliceTimer.lastIntervalTime;
//Don't try to divide by zero
int numTags = tagToMessages.Keys.Count;
if (numTags != 0)
{
if (numTags != 0) {
double timeExpendedPerTag = timeExpended / tagToMessages.Keys.Count;
if (onSingleTagPub != null)
foreach (string tag in tagToMessages.Keys)
@@ -351,18 +279,16 @@ namespace BCG
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private void myProcessMsg(string sender, Stream msg, out MetaData md)
{
md = new MetaData(msg);
private void myProcessMsg(string sender, Stream msg, out MetaData md) {
md = new MetaData(msg); // msg's Posiiton moved to end of meta data
MyMsgHandler op = myOperationToHandler[md["op-code"]];
//BlueEventLog.logEvent(string.Format("process message {0}", m));
bts.Log.Gen.info($"PubSubMessageReactor.myProcessMsg sender: {sender} metadata: {md}");
op(sender, msg, md);
}
#endregion
private void myHandleConnections(string client, bool connected)
{
private void myHandleConnections(string client, bool connected) {
Console.WriteLine("PubSubMessageReactor.myHandleConnections client: " + client + " connected: " + connected);
///interested in disconnections only, as connections of no
///significance until they subscribe, in which case an entry
@@ -371,13 +297,11 @@ namespace BCG
myState.removeConnection(client);
}
public void stop()
{
public void stop() {
myStop = true;
}
public void start()
{
public void start() {
myReceivingProcThread.Start();
}
@@ -385,8 +309,7 @@ namespace BCG
/// loops around and reads messages from the messageReceiver. calls processMsg
/// passing in the IClientMessage. also logs stuff about the message.
/// </summary>
private void myReceivingThreadProc()
{
private void myReceivingThreadProc() {
TimeSpan receiveTimeout = new TimeSpan(0, 0, 0, 0, 2500);
myMessageHub.activate();
@@ -398,31 +321,25 @@ namespace BCG
string sender;
Stream msg;
MetaData meta;
while (!myStop)
{
while (!myStop) {
sender = null;
msg = null;
meta = null;
try
{
try {
List<Pair<string, byte[]>> receivedData = myMessageHub.getUpdates(receiveTimeout, 1);
if (receivedData.Count > 0)
{
if (receivedData.Count > 0) {
sender = receivedData[0].first;
msg = new MemoryStream(receivedData[0].second);
myTimer.start();
myProcessMsg(sender, msg, out meta);
myTimer.stop();
if (myTimer.lastIntervalTime > myProcessingWarningThreshold)
{
if (myTimer.lastIntervalTime > myProcessingWarningThreshold) {
string warningMsg = String.Format("processing metadata {0} took {1} msecs", meta, myTimer.lastIntervalTime);
//BlueEventLog.logEvent(warningMsg, PS3LogLevels.performance);
}
}
myMasterWorker.doWork();
}
catch (Exception e)
{
} catch (Exception e) {
//BlueEventLog.logEvent(e, BlueEventLevel.Error);
Console.WriteLine("{0} : exception handling message: {1}\n", DateTime.Now, e.Message);
string errMsg = String.Format("Following information about the meta data and sender is available : " +
@@ -7,21 +7,19 @@ using System.IO;
using mbts;
namespace BCG
{
namespace BCG {
/// <summary>
/// Mantains all the state that there is in the server.
/// This information is keyed off the tags (rather than
/// keyed off subscribers or publishers)
/// State information consists of 2 distints sets :
/// State information consists of 2 distinct sets :
/// myTagToData consists of all the data published to
/// this pubsub.
/// myTagToTagInfo consists of all the subscription and
/// publishing information
/// </summary>
///
public class PubSubState
{
public class PubSubState {
public delegate void PubSubStateSingleTagChangeDelegate(PubSubSingleTagStateEvent theEvent);
public event PubSubStateSingleTagChangeDelegate onSingleTagChange;
@@ -33,8 +31,7 @@ namespace BCG
private Dictionary<string, SingleTagData> myTagToData = new Dictionary<string, SingleTagData>();
private Dictionary<string, SingleTagPubSubMap> myTagToPubsubMap = new Dictionary<string, SingleTagPubSubMap>();
public PubSubState()
{
public PubSubState() {
SystemSubscriptionMapPublisher.init(this);
SystemStatsPublisher.init(this);
SystemHeartbeatPublisher.init();
@@ -49,13 +46,10 @@ namespace BCG
/// </summary>
/// <param name="sub"></param>
/// <param name="md"></param>
public void sub(SubMetaData sub)
{
public void sub(SubMetaData sub) {
///tag based key ds update
foreach (string tag in sub.tags)
{
if (!myTagToPubsubMap.ContainsKey(tag))
{
foreach (string tag in sub.tags) {
if (!myTagToPubsubMap.ContainsKey(tag)) {
myTagToPubsubMap.Add(tag, new SingleTagPubSubMap(tag));
}
myTagToPubsubMap[tag].onSub(sub);
@@ -67,20 +61,15 @@ namespace BCG
onTagMapChanged(sub.tags);
}
public void unsub(UnSubMetaData unsub)
{
foreach (string tag in unsub.tags)
{
if (myTagToPubsubMap.ContainsKey(tag))
{
public void unsub(UnSubMetaData unsub) {
foreach (string tag in unsub.tags) {
if (myTagToPubsubMap.ContainsKey(tag)) {
///maybe check here, that if there are no subscribers
///and publishers for the tag then remove it.
myTagToPubsubMap[tag].onUnsub(unsub);
if (onSingleTagChange != null)
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.unsubscription, unsub.connection, unsub.unsubscribingApp));
}
else
{
} else {
string warningMsg = String.Format("{0} was never published or subscribed on this pubsub, hence" +
" the unsub call by app {1} is voided", tag, unsub.unsubscribingApp);
//BlueEventLog.logEvent(warningMsg, BlueEventLevel.Warning);
@@ -90,11 +79,9 @@ namespace BCG
onTagMapChanged(unsub.tags);
}
public void removeConnection(string connection)
{
public void removeConnection(string connection) {
List<string> tagsEffected = new List<string>();
foreach (KeyValuePair<string, SingleTagPubSubMap> p in myTagToPubsubMap)
{
foreach (KeyValuePair<string, SingleTagPubSubMap> p in myTagToPubsubMap) {
///maybe check here, that if there are no subscribers
///and publishers for the tag then remove it.
bool wasPublisher = false;
@@ -103,8 +90,7 @@ namespace BCG
if (p.Value.onConnectionRemoveIfExists(connection, out wasPublisher, out wasSubscriber))
tagsEffected.Add(tag);
if (onSingleTagChange != null)
{
if (onSingleTagChange != null) {
//we don't know the app or key qualifier or anything for these connection. that is unfortunate.
if (wasPublisher)
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.publisherDie, connection, ""));
@@ -128,67 +114,60 @@ namespace BCG
/// </summary>
/// <param name="tag"></param>
/// <param name="msg"></param>
public void updatePubData(PubMetaData pub, Stream msg)
{
public void updatePubData(PubMetaData pub, Stream msg) {
int sz = (int)(msg.Length - msg.Position);
///this is the only place where SingleTagData should be created
if (!myTagToData.ContainsKey(pub.tag))
{
if (!myTagToData.ContainsKey(pub.tag)) {
///first time data is published for this tag
myTagToData.Add(pub.tag, new SingleTagData(pub.tag, pub.type));
}
myTagToData[pub.tag].pubUpdate(msg);
if (!myTagToPubsubMap.ContainsKey(pub.tag))
{
if (!myTagToPubsubMap.ContainsKey(pub.tag)) {
myTagToPubsubMap.Add(pub.tag, new SingleTagPubSubMap(pub.tag));
}
bool pubChanged = false;
myTagToPubsubMap[pub.tag].onPub(pub, out pubChanged);
if (pubChanged)
{
if (pubChanged) {
if (onTagMapChanged != null)
onTagMapChanged(new string[] { pub.tag });
if (onSingleTagChange != null)
onSingleTagChange(
new PubSubSingleTagStateEvent(pub.tag,
PubSubSingleTagStateEvent.EventType.publish,
new PubSubSingleTagStateEvent(pub.tag,
PubSubSingleTagStateEvent.EventType.publish,
pub.connection, pub.publishingApp));
}
if (onPublishStats != null)
onPublishStats(pub.tag, sz);
}
#endregion
public ICollection<string> getSubscriberConnectionsForTag(string tag)
{
public ICollection<string> getSubscriberConnectionsForTag(string tag) {
if (myTagToPubsubMap.ContainsKey(tag))
return myTagToPubsubMap[tag].subscriberConnections;
else
return new string[] { };
}
public ICollection<string> getSubscribingAppsForTag(string tag)
{
public ICollection<string> getSubscribingAppsForTag(string tag) {
if (myTagToPubsubMap.ContainsKey(tag))
return myTagToPubsubMap[tag].subscripingApps;
else
return new string[] { };
}
public byte[] getContent(string tag)
{
public byte[] getContent(string tag) {
if (!myTagToData.ContainsKey(tag))
return null;
else
return myTagToData[tag].getCurrentContent();
}
public string getTagType(string tag)
{
public string getTagType(string tag) {
if (!myTagToData.ContainsKey(tag))
return null;
else
@@ -207,29 +186,23 @@ namespace BCG
/// <param name="deadPublishers"></param>
public void getAllConnections(string tag, out Dictionary<string, string> subscribers,
out Dictionary<string, string> activePublishers,
out ICollection<Pair<string, string>> deadPublishers)
{
if (myTagToPubsubMap.ContainsKey(tag))
{
out ICollection<Pair<string, string>> deadPublishers) {
if (myTagToPubsubMap.ContainsKey(tag)) {
SingleTagPubSubMap map = myTagToPubsubMap[tag];
subscribers = map.subscribers;
activePublishers = map.activePublishers;
deadPublishers = map.deadPublishers;
}
else
{
} else {
subscribers = activePublishers = new Dictionary<string, string>();
deadPublishers = new List<Pair<string, string>>();
}
}
public ICollection<string> allTags
{
public ICollection<string> allTags {
get { return myTagToPubsubMap.Keys; }
}
public ICollection<string> tagsWithData
{
public ICollection<string> tagsWithData {
get { return myTagToData.Keys; }
}
@@ -239,15 +212,11 @@ namespace BCG
/// </summary>
/// <param name="tags"></param>
/// <returns></returns>
public Dictionary<string, List<string>> getSubscriberConnectionsForTags(IEnumerable<string> tags)
{
public Dictionary<string, List<string>> getSubscriberConnectionsForTags(IEnumerable<string> tags) {
Dictionary<string, List<string>> retVal = new Dictionary<string, List<string>>();
foreach (string tag in tags)
{
if (myTagToPubsubMap.ContainsKey(tag))
{
foreach (string sub in myTagToPubsubMap[tag].subscriberConnections)
{
foreach (string tag in tags) {
if (myTagToPubsubMap.ContainsKey(tag)) {
foreach (string sub in myTagToPubsubMap[tag].subscriberConnections) {
if (!retVal.ContainsKey(sub))
retVal.Add(sub, new List<string>());
retVal[sub].Add(tag);
@@ -7,21 +7,18 @@ using System.Collections.Generic;
using BCG.Utilities;
using mbts;
namespace BCG
{
namespace BCG {
/// <summary>
/// SingleTagData consists of (as the name implies)
/// the data published for a tag
/// </summary>
internal class SingleTagData
{
internal class SingleTagData {
private string myTagType = null;
private string myTag;
private byte[] myLastPublishedMsg;
private IPubSubServerTagHandler myHandler;
public SingleTagData(string tag, string tagType)
{
public SingleTagData(string tag, string tagType) {
myTag = tag;
myTagType = tagType;
if (myTagType == SystemSubscriptionMapPublisher.tagType)
@@ -38,16 +35,12 @@ namespace BCG
public string tagType { get { return myTagType; } }
internal void pubUpdate(Stream stream)
{
if (myHandler != null)
{
try
{
internal void pubUpdate(Stream stream) {
bts.Log.Gen.info($"SingleTagData.pubUpdate tag: {myTag} handler: {myHandler} stream len: {stream.Length - stream.Position}");
if (myHandler != null) {
try {
myHandler.update(myTag, new Stream[] { stream });
}
catch (Exception e)
{
} catch (Exception e) {
// BlueEventLog.logEvent(e);
string errMsg = "tag handler threw an exception " + e.Message + " for tag type = " + myTagType +
" tag = " + myTag + " length = " + stream.Length + "\n" +
@@ -58,30 +51,22 @@ namespace BCG
else
throw new ApplicationException(errMsg, e);
}
}
else
{
} else {
myLastPublishedMsg = new byte[stream.Length - stream.Position];
stream.Read(myLastPublishedMsg, 0, myLastPublishedMsg.Length);
}
}
public byte[] getCurrentContent()
{
if (myHandler != null)
{
public byte[] getCurrentContent() {
if (myHandler != null) {
return myHandler.getContent(myTag);
}
else
{
} else {
return myLastPublishedMsg;
}
}
private void myHandleHandlerCallbacks(ICollection<Triplet<string, string, byte[]>> cbItems)
{
foreach (Triplet<string, string, byte[]> p in cbItems)
{
private void myHandleHandlerCallbacks(ICollection<Triplet<string, string, byte[]>> cbItems) {
foreach (Triplet<string, string, byte[]> p in cbItems) {
string tagName = p.first, tagType = p.second;
byte[] data = p.third;
PubMetaData pub = new PubMetaData(tagType, tagName, data.Length, "local", ClientInfo.thisClient.processString);
@@ -7,8 +7,7 @@ using BCG.Utilities.General;
using mbts;
using mbts.spark;
namespace BCG
{
namespace BCG {
/// <summary>
/// Deals with the subscriptions (and getting data) from one
/// pubsub server. Once again, theres no sharing of conenctions
@@ -17,15 +16,13 @@ namespace BCG
/// its concerned with (passed as a conctructor arg).
/// </summary>
internal interface ISingleServerSubscriberCore : IDisposable
{
internal interface ISingleServerSubscriberCore : IDisposable {
IList<PubSubUpdate> getUpdates(int timeout);
event PubSubConnectionChange onPubSubConnectionChange;
void subscribe(params string[] tags);
void unsubscribe(params string[] tags);
}
internal class SingleServerSubscriberCore : IDisposable, ISingleServerSubscriberCore
{
internal class SingleServerSubscriberCore : IDisposable, ISingleServerSubscriberCore {
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
private PubSubServerConnectionFactory myConnectionFactory;
private IPubSubServerConnection myConnection;
@@ -33,15 +30,13 @@ namespace BCG
public event PubSubConnectionChange onPubSubConnectionChange;
public SingleServerSubscriberCore(string serverURL, PubSubServerConnectionFactory connectionFactory)
{
public SingleServerSubscriberCore(string serverURL, PubSubServerConnectionFactory connectionFactory) {
myConnectionFactory = connectionFactory;
myConnection = myConnectionFactory.getConnection(serverURL);
myConnection.registerCallback(new ConnectionStatusChange(myHandleServerReboot));
}
public IList<PubSubUpdate> getUpdates()
{
public IList<PubSubUpdate> getUpdates() {
return getUpdates(1);
}
@@ -51,54 +46,41 @@ namespace BCG
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
public IList<PubSubUpdate> getUpdates(int timeout)
{
public IList<PubSubUpdate> getUpdates(int timeout) {
myConnection.doWork();
List<PubSubUpdate> retVal = new List<PubSubUpdate>();
IList<byte[]> dataItems = myConnection.getUpdates(new TimeSpan(0, 0, 0, 0, timeout), Int32.MaxValue);
foreach (byte[] data in dataItems)
{
foreach (byte[] data in dataItems) {
processMessage(data, retVal);
}
return retVal;
}
public List<string> getSubscribedTags()
{
public List<string> getSubscribedTags() {
return myTags;
}
public void subscribe(params string[] tags)
{
foreach (string tag in tags)
{
if (myTags.Contains(tag))
{
public void subscribe(params string[] tags) {
foreach (string tag in tags) {
if (myTags.Contains(tag)) {
string errMsg = String.Format("tag {0} was already subscribed from server {1}",
tag, myConnection.serverURL);
// BlueEventLog.logEvent(errMsg, BlueEventLevel.Warning);
}
else
{
} else {
myTags.Add(tag);
}
}
mySubscribe(tags);
}
public void unsubscribe(params string[] tags)
{
public void unsubscribe(params string[] tags) {
List<string> unsubTags = new List<string>();
foreach (string tag in tags)
{
if (myTags.Contains(tag))
{
foreach (string tag in tags) {
if (myTags.Contains(tag)) {
myTags.Remove(tag);
unsubTags.Add(tag);
}
else
{
} else {
string errMsg = String.Format("tag {0} is being unsubscribed from server {1}, eventhough " +
"it was never subscribed", tag, myConnection.serverURL);
// BlueEventLog.logEvent(errMsg, BlueEventLevel.Warning);
@@ -108,13 +90,13 @@ namespace BCG
Stream str = new MemoryStream();
new MetaData(unsub).save(str);
str.Position = 0;
// this is where data is actually sent out
myConnection.send(str);
}
#region IDisposable Members
public void Dispose()
{
public void Dispose() {
///remove the subscriber
//myConnection.send(new PubSubMessage(new MetaData(new RemoveSubscriberMetaData(myConnection.clientURL))).toStream());
myConnection.Dispose();
@@ -126,8 +108,7 @@ namespace BCG
#endregion
private void mySubscribe(params string[] tags)
{
private void mySubscribe(params string[] tags) {
SubMetaData sub = new SubMetaData(myConnection.clientURL, tags);
Stream str = new MemoryStream();
new MetaData(sub).save(str);
@@ -135,20 +116,16 @@ namespace BCG
myConnection.send(str);
}
private void myHandleServerReboot(string serverURL, bool connection)
{
if (connection)
{
private void myHandleServerReboot(string serverURL, bool connection) {
if (connection) {
//ie connection established again, so has to
//resubscribe to all the tags
// BlueEventLog.logEvent("Connection to " + serverURL + " established....resubscribing to all the tags",
// PS3LogLevels.disconnection);
mySubscribe(myTags.ToArray());
}
else
{
} else {
// BlueEventLog.logEvent("Connection to " + serverURL + " broke....attempting to reconnect...",
// PS3LogLevels.disconnection);
}
@@ -158,18 +135,17 @@ namespace BCG
onPubSubConnectionChange(connection, serverURL);
}
internal static void processMessage(byte[] data, List<PubSubUpdate> updates)
{
if (data != null && data.Length > 0)
{
internal static void processMessage(byte[] data, List<PubSubUpdate> updates) {
bts.Log.Gen.info($"SingleServerSubscriberCore.processMessage data_len: {data.Length}");
if (data != null && data.Length > 0) {
MemoryStream str = new MemoryStream(data);
MetaData md = new MetaData(str);
bts.Log.Gen.info($"SingleServerSubscriberCore.processMessage meta_data: {md}");
MemoryStream bodyStream = str;
long bodyPosition = str.Position;
object op = Activator.CreateInstance (Type.GetType("BCG." + md["op-code"]));
object op = Activator.CreateInstance(Type.GetType("BCG." + md["op-code"]));
md.copyTo(op);
if (op is PubMetaData)
{
if (op is PubMetaData) {
PubMetaData p = op as PubMetaData;
//BCG.BlueTrace.WriteBlueCommReceiveW(p.tag, p.publishTimeTicks);
@@ -177,41 +153,29 @@ namespace BCG
p.warnIfLatencyExceedsThreshold();
//find out if we need to decompress this data
Stream realBody = bodyStream;
if (p.isCompressed)
{
if (p.isCompressed) {
long compressedSize = bodyStream.Length - bodyStream.Position;
if (compressedSize == 0)
{
if (compressedSize == 0) {
string errorMsg = "Error: tag " + p.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
Console.WriteLine(errorMsg);
//BlueEventLog.logEvent(errorMsg, BlueEventLevel.Error);
return;
}
realBody = StreamDecompressor.decompressStream(bodyStream);
realBody.Position = bodyPosition;
}
updates.Add(new PubSubUpdate(p.tag, p.type, realBody));
}
else if (op is TransactionMetaData)
{
} else if (op is TransactionMetaData) {
TransactionMetaData tr = op as TransactionMetaData;
for (int i = 0; i < tr.nMessages; i++)
{
for (int i = 0; i < tr.nMessages; i++) {
PubMetaData pub = new PubMetaData();
new MetaData(bodyStream).copyTo(pub);
byte[] trConstituent = new byte[pub.size];
bodyStream.Read(trConstituent, 0, trConstituent.Length);
updates.Add(new PubSubUpdate(pub.tag, pub.type, new MemoryStream(trConstituent)));
}
}
else{}
//BlueEventLog.logAssert(false, "Unknown message received: " + md.ToString() + " , only Pub and TransactionMetaData are legal");
} else { }
//BlueEventLog.logAssert(false, "Unknown message received: " + md.ToString() + " , only Pub and TransactionMetaData are legal");
}
}
}
+134 -207
View File
@@ -15,91 +15,79 @@ using mbts;
* This file was originally called 'PublisherCore.cs'. It was renamed so that the publisher core could be replaced.
*
* */
namespace BCG
{
public class CrushedTagManager
{
private static CrushedTagManager mySelf;
private Dictionary<string, bool> myTags = new Dictionary<string, bool>();
public static CrushedTagManager instance
{
get
{
if (mySelf == null)
mySelf = new CrushedTagManager();
return mySelf;
namespace BCG {
// Provides a function to check if a tag is crushabel or not
public class CrushedTagManager {
private static CrushedTagManager _self;
private Dictionary<string, bool> _tag_map = new Dictionary<string, bool>();
public static CrushedTagManager instance {
get {
if (_self == null)
_self = new CrushedTagManager();
return _self;
}
}
public bool isTagCrushable(string tag)
{
public bool is_tag_crushable(string tag) {
tag = tag.ToLower();
lock (typeof(CrushedTagManager))
{
if (!myTags.ContainsKey(tag))
{
bool rv = false;
lock (typeof(CrushedTagManager)) {
if (!_tag_map.TryGetValue(tag, out rv)) {
foreach (string r in PubSub3Config.instance.PS3_CrushableTags)
if (new System.Text.RegularExpressions.Regex(r).IsMatch(tag))
{
myTags.Add(tag, true);
if (new System.Text.RegularExpressions.Regex(r).IsMatch(tag)) {
_tag_map.Add(tag, true);
break;
}
if (!myTags.ContainsKey(tag))
myTags.Add(tag, false);
_tag_map.Add(tag, false);
}
}
return myTags[tag];
return rv;
}
}
/// <summary>
/// All the data for a single tag, thats published within
/// a transaction
/// </summary>
public class TagWaitingInTransaction
{
private bool myIsCrushable;
private List<byte[]> myData = new List<byte[]>();
private int myTotalSize = 0;
private string myTag, myTagType;
public TagWaitingInTransaction(string theTagType, string theTag)
{
myTag = theTag;
myTagType = theTagType;
myIsCrushable = CrushedTagManager.instance.isTagCrushable(myTag);
public class TagWaitingInTransaction {
private bool _is_crushable;
private List<byte[]> _buffered_data = new List<byte[]>();
// this is the size of all the buffered data
private int _total_size = 0;
private string _tag, _tag_type;
public TagWaitingInTransaction(string theTagType, string theTag) {
_tag = theTag;
_tag_type = theTagType;
_is_crushable = CrushedTagManager.instance.is_tag_crushable(_tag);
}
public void addPub(Stream pubStream)
{
if (myIsCrushable)
{
byte[] newData = new byte[(int)(pubStream.Length - pubStream.Position)];
pubStream.Read(newData, 0, newData.Length);
myData.Clear();
myData.Add(newData);
myTotalSize = newData.Length;
}
else
{
byte[] newData = new byte[(int)(pubStream.Length - pubStream.Position)];
pubStream.Read(newData, 0, newData.Length);
myData.Add(newData);
myTotalSize += newData.Length;
public void add_pub(Stream pub_stream) {
if (_is_crushable) {
// extract bytes, clear _buffered_data and queue to _buffered_data
byte[] new_data = new byte[(int)(pub_stream.Length - pub_stream.Position)];
pub_stream.Read(new_data, 0, new_data.Length);
_buffered_data.Clear();
_buffered_data.Add(new_data);
_total_size = new_data.Length;
} else {
// extract btes and queue to _buffered_data
byte[] new_data = new byte[(int)(pub_stream.Length - pub_stream.Position)];
pub_stream.Read(new_data, 0, new_data.Length);
_buffered_data.Add(new_data);
_total_size += new_data.Length;
}
}
public void writeTo(Stream s, string connection, string publishingApp)
{
new MetaData(new PubMetaData(myTagType, myTag, myTotalSize, connection, publishingApp)).save(s);
foreach (byte[] data in myData)
public void write_to(Stream s, string connection, string publishing_app) {
new MetaData(new PubMetaData(_tag_type, _tag, _total_size, connection, publishing_app)).save(s);
foreach (byte[] data in _buffered_data)
s.Write(data, 0, data.Length);
}
public string tag { get { return myTag; } }
public string tagType { get { return myTagType; } }
public List<byte[]> data { get { return myData; } }
public string tag { get { return _tag; } }
public string tagType { get { return _tag_type; } }
public List<byte[]> data { get { return _buffered_data; } }
}
public enum PublisherCoreMessageType
{
public enum PublisherCoreMessageType {
transactionBegin,
transactionCommit,
transactionAbort,
@@ -107,26 +95,22 @@ namespace BCG
clear
}
public class PublisherCoreMessage
{
public class PublisherCoreMessage {
public PublisherCoreMessageType msgType;
public string tagType, tag;
public Stream data;
public PublisherCoreMessage(string tagToClear)
{
public PublisherCoreMessage(string tagToClear) {
tag = tagToClear;
msgType = PublisherCoreMessageType.clear;
}
public PublisherCoreMessage(PublisherCoreMessageType theMsgType)
{
public PublisherCoreMessage(PublisherCoreMessageType theMsgType) {
msgType = theMsgType;
}
public PublisherCoreMessage(string theTagType, string theTag, Stream theData)
{
public PublisherCoreMessage(string theTagType, string theTag, Stream theData) {
msgType = PublisherCoreMessageType.publish;
tagType = theTagType;
tag = theTag;
@@ -136,8 +120,7 @@ namespace BCG
// this class uses an internal queue + thread to serialize and process calls
// made through its public interface (IPublisherCore)
public class StaticPublisherCore : IPublisherCore
{
public class StaticPublisherCore : IPublisherCore {
// used to allow for different transports
// to be used when communicating to a pubsubserver
protected PubSubServerConnectionFactory myConnectionFactory = new PubSubServerConnectionFactory(false);
@@ -172,57 +155,46 @@ namespace BCG
public StaticPublisherCore()
// TODOds
: this(new PubSubRouter())
{ }
: this(new PubSubRouter()) { }
public StaticPublisherCore(string serverURL)
: this(new PubSubRouter(new PubSubRoute(serverURL, ".*")))
{ }
: this(new PubSubRouter(new PubSubRoute(serverURL, ".*"))) { }
public StaticPublisherCore(IPubSubRouter r)
{
public StaticPublisherCore(IPubSubRouter r) {
myRouter = r;
Thread pumpThread = new Thread(new ThreadStart(myMessagePump));
pumpThread.IsBackground = true;
pumpThread.Start();
}
public void Dispose()
{ }
public void Dispose() { }
#endregion
public int getNumberOfWaitingMessages() { return myMainQueue.getCount(); }
#region Main Thread - all library functions are called on the main thread and queued to be picked by the pump
public void publish(string tag, Stream content)
{ publish("", tag, content); }
public void publish(string tag, Stream content) { publish("", tag, content); }
public void publish(string tagType, string tag, Stream content)
{
public void publish(string tagType, string tag, Stream content) {
myMainQueue.enqueue(new PublisherCoreMessage(tagType, tag, content));
}
public void beginTransaction()
{
public void beginTransaction() {
myMainQueue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transactionBegin));
}
public void commitTransaction()
{
public void commitTransaction() {
myMainQueue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transactionCommit));
}
public void abortTransaction()
{
public void abortTransaction() {
myMainQueue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transactionAbort));
}
public bool inTransaction
{ get { return myInTransaction; } }
public bool inTransaction { get { return myInTransaction; } }
public void clearState(string tag)
{
public void clearState(string tag) {
myMainQueue.enqueue(new PublisherCoreMessage(tag));
}
@@ -230,49 +202,32 @@ namespace BCG
#region Message Pump Thread - this is where messages are dequeued and the action performed
private void myMessagePump()
{
while (true)
{
try
{
while (true)
{
private void myMessagePump() {
while (true) {
try {
while (true) {
object data = myMainQueue.dequeue(1000);
if (data != null)
{
if (data != null) {
int qc = myMainQueue.getCount();
PublisherCoreMessage msg = data as PublisherCoreMessage;
if (msg.msgType == PublisherCoreMessageType.transactionBegin)
{
if (msg.msgType == PublisherCoreMessageType.transactionBegin) {
myBeginTransaction();
}
else if (msg.msgType == PublisherCoreMessageType.transactionCommit)
{
} else if (msg.msgType == PublisherCoreMessageType.transactionCommit) {
myCommitTransaction();
}
else if (msg.msgType == PublisherCoreMessageType.transactionAbort)
{
} else if (msg.msgType == PublisherCoreMessageType.transactionAbort) {
myAbortTransaction();
}
else if (msg.msgType == PublisherCoreMessageType.publish)
{
} else if (msg.msgType == PublisherCoreMessageType.publish) {
myPublish(msg.tagType, msg.tag, msg.data);
}
else if (msg.msgType == PublisherCoreMessageType.clear)
{
} else if (msg.msgType == PublisherCoreMessageType.clear) {
myClearState(msg.tag);
}
}
foreach (IPubSubServerConnection conn in mySenders)
{
foreach (IPubSubServerConnection conn in mySenders) {
conn.doWork();
}
}
}
catch (Exception ex)
{
} catch (Exception ex) {
// BlueEventLog.logEvent(ex);
if (ex is OutOfMemoryException)
throw ex;
@@ -280,20 +235,17 @@ namespace BCG
}
}
private void myBeginTransaction()
{
private void myBeginTransaction() {
// BlueEventLog.logAssert(!inTransaction);
//BlueEventLog.logEvent("StaticPublisherCore Starting Transaction.", BlueEventLevel.Information);
myInTransaction = true;
}
private void myCommitTransaction()
{
private void myCommitTransaction() {
myEndTransaction(true);
}
private void myAbortTransaction()
{
private void myAbortTransaction() {
myEndTransaction(false);
}
@@ -304,21 +256,17 @@ namespace BCG
//keep it consistent with the way it was done earlier), this should
//simply publish it non transactionally (easy way out), or
//transactionally with respect to each pubsub
protected virtual void myEndTransaction(bool success)
{
lock (this)
{
protected virtual void myEndTransaction(bool success) {
lock (this) {
// BlueEventLog.logAssert(inTransaction);
//BlueEventLog.logEvent("StaticPublisherCore Finishing Transaction.", BlueEventLevel.Information);
myInTransaction = false;
if (success)
{
if (success) {
// pubsub server -> tags with updates
Dictionary<string, List<string>> routesToTags = new Dictionary<string, List<string>>();
// group updates per pubsub server
foreach (string tag in myWaitingInTransactionMessages.Keys)
{
foreach (string tag in myWaitingInTransactionMessages.Keys) {
string thisURL = myRouter.getPubURLForTag(tag);
if (!routesToTags.ContainsKey(thisURL))
routesToTags[thisURL] = new List<string>();
@@ -327,17 +275,14 @@ namespace BCG
// more than one server means we won't be able to honor the transactionality request
// log a warning if that is the case.
if (routesToTags.Keys.Count > 1)
{
if (routesToTags.Keys.Count > 1) {
//this is going to be a long error message, but it's an importat one to be able to debug.
StringBuilder errorMessageBuilder = new StringBuilder();
errorMessageBuilder.Append("Tags within this transaction are routed to different pubSub servers.\n");
errorMessageBuilder.Append("This will break the transactionality of clients receiving the data. Details:\n");
foreach (string route in routesToTags.Keys)
{
foreach (string tag in routesToTags[route])
{
foreach (string route in routesToTags.Keys) {
foreach (string tag in routesToTags[route]) {
errorMessageBuilder.Append("\tTag ");
errorMessageBuilder.Append(tag);
errorMessageBuilder.Append(" is routed to ");
@@ -351,16 +296,14 @@ namespace BCG
}
// iterate through each server with pending messages
foreach (KeyValuePair<string, List<string>> kvp in routesToTags)
{
foreach (KeyValuePair<string, List<string>> kvp in routesToTags) {
IPubSubServerConnection transactionSender = null;
string publishingApp = ClientInfo.thisClient.processString;
string connection = kvp.Key;
Stream transactionData = new MemoryStream();
// iterate through each of the pending tag updates for one server
foreach (string tag in kvp.Value)
{
foreach (string tag in kvp.Value) {
///it would be the same transaction sender that would
///return each time
transactionSender = myGetOrBuildSender(tag);
@@ -372,7 +315,7 @@ namespace BCG
// update internal state
myState.publish(waiting.tag, waiting.tagType, data);
// add a pubsub header and copy each message to the transactionData stream
waiting.writeTo(transactionData, connection, publishingApp);
waiting.write_to(transactionData, connection, publishingApp);
}
// now transactionData holds all the updates for all the tags
// that are to be sent to the PubSub represented by transactionSender
@@ -384,35 +327,32 @@ namespace BCG
transactionData.Position = 0;
// send the entire thing to the specific pubsub server.
transactionSender.send(new PubSubMessage(new MetaData(tMd), transactionData).toStream());
// this is where the data is actually sent out !
var ps_msg = new PubSubMessage(new MetaData(tMd), transactionData);
Console.WriteLine($"serverConn.send metadata: {ps_msg.meta_data}");
bts.Log.Gen.info($"serverConn.send metadata: {ps_msg.meta_data}");
transactionSender.send(ps_msg);
}
}
myWaitingInTransactionMessages.Clear();
}
}
protected virtual void myPublish(string tagType, string tag, Stream content)
{
if (myInTransaction)
{
protected virtual void myPublish(string tagType, string tag, Stream content) {
if (myInTransaction) {
///if its in transaction message is cached, and is sent
///only when theres a commit or abort
if (!myWaitingInTransactionMessages.ContainsKey(tag))
myWaitingInTransactionMessages.Add(tag, new TagWaitingInTransaction(tagType, tag));
myWaitingInTransactionMessages[tag].addPub(content);
}
else
{
myWaitingInTransactionMessages[tag].add_pub(content);
} else {
//if (CrushedTagManager.instance.isTagCrushable(tag))
{
myState.publish(tag, tagType, content);
if (!myCrushableTagsWaitingOnAck.ContainsKey(tag))
{
if (!myCrushableTagsWaitingOnAck.ContainsKey(tag)) {
// BlueEventLog.logEvent("Sending crushable tag " + tag, BlueEventLevel.Diagnosis);
mySendData(tag, tagType, content);
}
else
{
} else {
// BlueEventLog.logEvent("Could not send tag " + tag + " since its waiting on an ack", BlueEventLevel.Diagnosis);
myCrushableTagsWaitingOnAck[tag].second++;
}
@@ -428,64 +368,59 @@ namespace BCG
}
// add header, possibly compress the data, and sendto appropriate pubsub
private void mySendData(string tag, string tagType, Stream content)
{
private void mySendData(string tag, string tagType, Stream content) {
long initPos = content.Position;
// locate right pubsub for this tag
IPubSubServerConnection serverConn = myGetOrBuildSender(tag);
Stream contentToPublish = content;
PubMetaData pubMd;
PubMetaData pub_md;
if (PubSub3Config.instance.isTagCompressed(tag))
{
if (PubSub3Config.instance.isTagCompressed(tag)) {
contentToPublish = new MemoryStream();
SharpZipCompressor Kompressor = new SharpZipCompressor();
Kompressor.compress(content, contentToPublish);
contentToPublish.Position = 0;
pubMd = new PubMetaData(tagType, tag, (int)contentToPublish.Length, (int)content.Length, serverConn.serverURL, ClientInfo.thisClient.processString);
if (PubSub3Config.instance.shouldReportTagsCompressionRatio(tag))
{
pub_md = new PubMetaData(tagType, tag, (int)contentToPublish.Length, (int)content.Length, serverConn.serverURL, ClientInfo.thisClient.processString);
if (PubSub3Config.instance.shouldReportTagsCompressionRatio(tag)) {
string msg = String.Format("Compressed tag {0} from {1} to {2} bytes, for a ratio of {3}.", tag, content.Length, contentToPublish.Length, (float)content.Length / (float)contentToPublish.Length);
// BlueEventLog.logEvent(msg, PS3LogLevels.compressionRatio);
}
}
else
pubMd = new PubMetaData(tagType, tag, (int)content.Length, serverConn.serverURL, ClientInfo.thisClient.processString);
} else
pub_md = new PubMetaData(tagType, tag, (int)content.Length, serverConn.serverURL, ClientInfo.thisClient.processString);
content.Position = initPos;
if (CrushedTagManager.instance.isTagCrushable(tag))
{
if (CrushedTagManager.instance.is_tag_crushable(tag)) {
string refId = String.Empty;
Console.WriteLine("StaticPublisherCore.mySend serverConn.sendWithConfirmation tag: " + tag + " server: " + serverConn.serverURL);
serverConn.sendWithConfirmation((new PubSubMessage(new MetaData(pubMd), contentToPublish).toStream() as MemoryStream).ToArray(), ref refId);
// this is where the data is actually sent out (there are multiple sends)
PubSubMessage ps_msg = new PubSubMessage(new MetaData(pub_md), contentToPublish);
//Console.WriteLine($"serverConn.send_with_confirmation metadata: {ps_msg.meta_data}");
bts.Log.Gen.info($"serverConn.send_with_confirmation metadata: {ps_msg.meta_data}");
serverConn.send_with_confirmation(ps_msg, ref refId);
myCrushableTagsWaitingOnAck.Add(tag, new Pair<string, int>(refId, 0));
}
else
{
Console.WriteLine("StaticPublisherCore.mySend serverConn.send tag: " + tag + " server: " + serverConn.serverURL);
serverConn.send(new PubSubMessage(new MetaData(pubMd), contentToPublish).toStream());
} else {
//Console.WriteLine("StaticPublisherCore.mySend serverConn.send tag: " + tag + " server: " + serverConn.serverURL);
// this is where the data is actually sent out
var ps_msg = new PubSubMessage(pub_md, contentToPublish);
bts.Log.Gen.info($"serverConn.send metadata: {ps_msg}");
Console.WriteLine($"serverConn.send metadata: {ps_msg}");
serverConn.send(ps_msg);
}
}
private void myClearState(string tag)
{
if (myTagToSender.ContainsKey(tag))
{
private void myClearState(string tag) {
if (myTagToSender.ContainsKey(tag)) {
myTagToSender.Remove(tag);
}
myState.clear(tag);
}
protected virtual IPubSubServerConnection myGetOrBuildSender(string tag)
{
if (!myTagToSender.ContainsKey(tag))
{
protected virtual IPubSubServerConnection myGetOrBuildSender(string tag) {
if (!myTagToSender.ContainsKey(tag)) {
string route = myRouter.getPubURLForTag(tag);
IPubSubServerConnection sender = myConnectionFactory.getConnection(route);
myTagToSender.Add(tag, sender);
if (!mySenders.Contains(sender))
{
if (!mySenders.Contains(sender)) {
mySenders.Add(sender);
sender.registerCallback(new ConnectionStatusChange(myHandleServerReboot));
sender.registerSendConfirmationCallback(new SendConfirmationDelegate(myOnSendConfirm));
@@ -497,15 +432,11 @@ namespace BCG
///in case of the server coming up, it publishes all the stored state
///for that server, back to it
protected void myHandleServerReboot(string serverURL, bool connection)
{
if (connection)
{
protected void myHandleServerReboot(string serverURL, bool connection) {
if (connection) {
string thisPublisherInfo = ClientInfo.thisClient.processString;
foreach (KeyValuePair<string, IPubSubServerConnection> p in myTagToSender)
{
if (p.Value.serverURL == serverURL)
{
foreach (KeyValuePair<string, IPubSubServerConnection> p in myTagToSender) {
if (p.Value.serverURL == serverURL) {
string tagType;
string tag = p.Key;
myCrushableTagsWaitingOnAck.Clear();
@@ -547,16 +478,12 @@ namespace BCG
/// of waiting for ack
/// </summary>
private Dictionary<string, Pair<string, int>> myCrushableTagsWaitingOnAck = new Dictionary<string, Pair<string, int>>();
private void myOnSendConfirm(string messageId, bool confirmed)
{
foreach (string tag in new List<string>(myCrushableTagsWaitingOnAck.Keys))
{
private void myOnSendConfirm(string messageId, bool confirmed) {
foreach (string tag in new List<string>(myCrushableTagsWaitingOnAck.Keys)) {
Pair<string, int> tagData = myCrushableTagsWaitingOnAck[tag];
if (messageId == tagData.first)
{
if (messageId == tagData.first) {
myCrushableTagsWaitingOnAck.Remove(tag);
if (tagData.second > 0)
{
if (tagData.second > 0) {
// BlueEventLog.logEvent("Missed sending " + tagData.second + " messages, sending latest message now", BlueEventLevel.Diagnosis);
/// this data was waiting on an
/// ack, need to resend it
@@ -10,42 +10,52 @@ using Spark.Core.Reactive;
using System.Security.Cryptography.X509Certificates;
using BCG;
namespace BCG {
namespace BCG
{
// This uses AsyncTCPConnection2's stateful async functions
public class SimpleTCPClientAsync : ITCPConnectionHandler, ITCPMessageHandler {
public class SimpleTCPClientAsync : ITCPConnectionHandler, ITCPMessageHandler
{
AsyncTCPConnection2 _conn = null;
int _m_msgs_received = 0;
public static async Task start(string ip, int port) {
public static async Task start(string ip, int port)
{
SimpleTCPClientAsync c = new SimpleTCPClientAsync(ip, port);
c.connect();
}
public void 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) {
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 () => {
Task.Run(async () =>
{
byte[] buff = new byte[1024];
for (int i = 0; i < buff.Length; i++) {
for (int i = 0; i < buff.Length; i++)
{
buff[i] = (byte)i;
}
_conn = connection as AsyncTCPConnection2;
while (!_conn.isDisposed) {
while (!_conn.isDisposed)
{
await _conn.send_async(buff);
await Task.Delay(100);
}
});
}
public void onData(RemoteMessage msg) {
public void onData(RemoteMessage msg)
{
_m_msgs_received++;
Console.WriteLine($"SimpleTCPClientAsync.onData n_msgs: {_m_msgs_received} msg: {msg}");
if (msg.messageType == RemoteMessageType.disconnection) {
if (msg.messageType == RemoteMessageType.Disconnection)
{
connect();
}
}
@@ -57,31 +67,37 @@ namespace BCG {
public SimpleTCPClientAsync(string ip, int port) { }
}
public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler {
public class SimpleTCPClient : ITCPConnectionHandler, ITCPMessageHandler
{
private int _n_msgs_received = 0;
AsyncTCPConnection2 _conn = null;
public static void start(string ip, int port) {
public static void start(string ip, int port)
{
SimpleTCPClient c = new SimpleTCPClient(ip, port);
c.connect();
}
public void 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) {
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++) {
for (int i = 0; i < buff.Length; i++)
{
buff[i] = (byte)i;
}
while (!_conn.isDisposed) {
while (!_conn.isDisposed)
{
_conn.send(buff);
System.Threading.Thread.Sleep(1000);
}
@@ -96,10 +112,12 @@ namespace BCG {
// });
}
public void onData(RemoteMessage msg) {
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) {
if (msg.messageType == RemoteMessageType.Disconnection)
{
connect();
}
}
@@ -16,14 +16,17 @@ using System.Reflection;
using System.Data;
using System.Collections.Generic;
namespace BCG {
namespace BCG
{
public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler {
public class SimpleTCPServerAsync : ITCPConnectionHandler, ITCPMessageHandler
{
int _n_msgs_received = 0;
SingleServerToClientManagedTCPConnectionAcceptor _acceptor = new SingleServerToClientManagedTCPConnectionAcceptor();
IDictionary<RemoteConnectionInfo, ITCPConnection> _conn_map = new Dictionary<RemoteConnectionInfo, ITCPConnection>();
public static async Task start() {
public static async Task start()
{
Console.WriteLine("SimpleTCPServer.start accepting socket connections");
SimpleTCPServerAsync s = new SimpleTCPServerAsync();
@@ -35,11 +38,13 @@ namespace BCG {
// {
// }
public void accept() {
public void accept()
{
_acceptor.accept_on_port(this, "127.0.0.1", 12345, TimeSpan.Zero);
}
public void onConnection(ITCPConnection connection) {
public void onConnection(ITCPConnection connection)
{
AsyncTCPConnection2 new_conn = connection as AsyncTCPConnection2;
RemoteConnectionInfo rci = new_conn.get_peer_address();
_conn_map.Add(rci, new_conn);
@@ -48,18 +53,21 @@ namespace BCG {
}
public void onData(RemoteMessage msg) {
public void onData(RemoteMessage msg)
{
Console.WriteLine($"SimpleTCPServerAsync.onData msg: {msg}");
RemoteConnectionInfo rci = msg.remoteConnectionInfo;
ITCPConnection this_conn;
bool found = _conn_map.TryGetValue(rci, out this_conn);
if (!found) {
if (!found)
{
Console.WriteLine("Could not find connection !!!!!!");
return;
}
_n_msgs_received++;
Console.WriteLine($"SimpleTCPServerAsync.onData msgs_received: {_n_msgs_received} msg: {msg} ci: {rci} echoing it back");
if (msg.messageType == RemoteMessageType.data) {
if (msg.messageType == RemoteMessageType.Data)
{
this_conn.send(msg.message);
// Task.Run(async () => {
@@ -0,0 +1,50 @@
using BCG;
using bts;
using System;
using System.IO;
using System.Runtime.Serialization;
namespace BCG {
public class TestPubItem : IPubItem {
int i = 100;
public static string test_tag = "TestTag";
public string getTag() {
return test_tag;
}
public void to_stream(Stream str) {
BinaryWriter bw = new BinaryWriter(str);
//bw.Write(d);
bw.Write(i);
}
}
public class TestPublisher {
TestPubItem _tbi = new TestPubItem();
IPublisherCore pcore = new StaticPublisherCore();
bool _hit_key_to_send = false;
public TestPublisher(bool hit_key_to_send) {
_hit_key_to_send = hit_key_to_send;
bts.Log.Gen.info($"Started TestPubliser _hit_key_to_send: {_hit_key_to_send}");
}
public void keep_publishing(TimeSpan wait_per, int n_pubs = System.Int32.MaxValue) {
TestPubItem pi = new TestPubItem();
for (int i = 0; i < n_pubs; i++) {
if (_hit_key_to_send) {
Console.WriteLine("Hit key to send data");
Console.ReadLine();
}
MemoryStream ms = new MemoryStream();
pi.to_stream(ms);
ms.Position = 0;
pcore.publish(TestPubItem.test_tag, ms);
if (!_hit_key_to_send)
System.Threading.Thread.Sleep(wait_per);
}
}
}
}
@@ -0,0 +1,38 @@
using BCG;
using bts;
using System;
using System.IO;
using System.Runtime.Serialization;
namespace BCG
{
public class TestSubscriber
{
SubscriberCore _sub_core = new SubscriberCore();
public TestSubscriber()
{
bts.Log.Gen.info("Started TestSubscriber");
}
public void keep_polling(TimeSpan wait_per, int n_polls = System.Int32.MaxValue)
{
_sub_core.subscribe(TestPubItem.test_tag);
for (int i = 0; i < n_polls; i++)
{
var list = _sub_core.getUpdates();
if (list.Count > 0)
{
Console.WriteLine($"Received {list.Count} items");
}
foreach (var p in list)
{
Console.WriteLine($"Received item: {p}");
}
System.Threading.Thread.Sleep(wait_per);
}
}
}
}
+4 -2
View File
@@ -15,8 +15,10 @@ namespace BCG {
/// </summary>
[STAThread]
public static async Task Main(string[] args) {
SimpleTCPServerAsync.start();
Task.Delay(1000000000).Wait();
bts.utils.BTSNativeMethods.provide_command_line(new string[] { }, 0);
mbts.config.Configuration.initialize();
// SimpleTCPServerAsync.start();
// Task.Delay(1000000000).Wait();
if (args.Length > 0 && args[0] == "daemon")
args = args.Skip(1).ToArray();