166 lines
7.0 KiB
C#
166 lines
7.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using BCG.Utilities;
|
|
using BCG.Utilities.General;
|
|
using mbts;
|
|
using mbts.spark;
|
|
using System.Linq;
|
|
|
|
namespace BCG {
|
|
/// <summary>
|
|
/// Deals with the subscriptions (and getting data) from one sole
|
|
/// pubsub server. Theres no sharing of conenctions
|
|
/// from this point. Every new instance of SingleServerSubscriberCore
|
|
/// will open its new dedicated connection to the serverURL that
|
|
/// its concerned with (passed as a conctructor arg).
|
|
/// </summary>
|
|
|
|
internal interface ISingleServerSubscriberCore : IDisposable {
|
|
IList<PubSubUpdate> get_updates(int timeout);
|
|
event PubSubConnectionChange onPubSubConnectionChange;
|
|
void subscribe(params string[] tags);
|
|
void unsubscribe(params string[] tags);
|
|
}
|
|
|
|
internal class SingleServerSubscriberCore : IDisposable, ISingleServerSubscriberCore {
|
|
private readonly IPubSubServerConnection _connection;
|
|
private HashSet<string> _tags = new HashSet<string>();
|
|
|
|
public event PubSubConnectionChange onPubSubConnectionChange;
|
|
|
|
public SingleServerSubscriberCore(string server_url, PubSubServerConnectionFactory connection_factory) {
|
|
_connection = connection_factory.getConnection(server_url);
|
|
_connection.register_connection_callback(new PubSubServerConnectionStatusChange(_handle_server_reboot));
|
|
}
|
|
|
|
// gets as many data items that it can get in
|
|
// timeout milliseconds
|
|
public IList<PubSubUpdate> get_updates(int timeout) {
|
|
_connection.doWork();
|
|
List<PubSubUpdate> rv = new List<PubSubUpdate>();
|
|
IList<byte[]> data_items = _connection.get_updates(new TimeSpan(0, 0, 0, 0, timeout), Int32.MaxValue);
|
|
foreach (byte[] data in data_items) {
|
|
process_message(data, ref rv);
|
|
}
|
|
return rv;
|
|
|
|
}
|
|
|
|
public void subscribe(params string[] tags) {
|
|
HashSet<string> this_call_tags = new HashSet<string>();
|
|
foreach (string tag in tags) {
|
|
if (_tags.Contains(tag)) {
|
|
bts.Log.Gen.warn($"SingleServerSubscriberCore.subscribe tag: {tag} has already " +
|
|
$"been subscribed to server: {_connection.server_url}");
|
|
} else {
|
|
_tags.Add(tag);
|
|
this_call_tags.Add(tag);
|
|
}
|
|
}
|
|
_subscribe(this_call_tags.ToArray());
|
|
}
|
|
|
|
public void unsubscribe(params string[] tags) {
|
|
List<string> unsub_tags = new List<string>();
|
|
foreach (string tag in tags) {
|
|
if (_tags.Contains(tag)) {
|
|
_tags.Remove(tag);
|
|
unsub_tags.Add(tag);
|
|
} else {
|
|
bts.Log.Gen.warn($"tag {tag} is being unsubscribed from server {_connection.server_url}, eventhough " +
|
|
"it was never subscribed");
|
|
}
|
|
}
|
|
UnSubMetaData unsub = new UnSubMetaData(_connection.client_url, unsub_tags.ToArray());
|
|
Stream str = new MemoryStream();
|
|
new MetaData(unsub).save(str);
|
|
str.Position = 0;
|
|
// this is where data is actually sent out
|
|
_connection.send(str);
|
|
}
|
|
|
|
#region IDisposable Members
|
|
|
|
public void Dispose() {
|
|
///remove the subscriber
|
|
//myConnection.send(new PubSubMessage(new MetaData(new RemoveSubscriberMetaData(myConnection.clientURL))).toStream());
|
|
_connection.Dispose();
|
|
|
|
//clear the list of tags you've subscribed to. This way, if you're ever used again,
|
|
//you have to send all the subscription metadata out again.
|
|
_tags.Clear();
|
|
}
|
|
|
|
#endregion
|
|
|
|
// The only place where a tag is subsribed from the Pubsub server. the subscriptions are wrapped into SubMetaData,
|
|
// which has a to_stream method
|
|
private void _subscribe(params string[] tags) {
|
|
SubMetaData sub = new SubMetaData(_connection.client_url, tags);
|
|
Stream str = new MemoryStream();
|
|
new MetaData(sub).save(str);
|
|
str.Position = 0;
|
|
_connection.send(str);
|
|
}
|
|
|
|
private void _handle_server_reboot(string server_url, bool connection) {
|
|
bts.Log.Gen.info($"SingleServerSubscriberCore._handle_server_reboot connection: {connection} server_url: {server_url}");
|
|
if (connection) {
|
|
_subscribe(_tags.ToArray());
|
|
} else {
|
|
bts.Log.Gen.warn($"SingleServerSubscriberCore._handle_server_reboot connection to {server_url} broke....attempting to reconnect...");
|
|
}
|
|
//let anyone watching you know that your connection has changed.
|
|
if (onPubSubConnectionChange != null)
|
|
onPubSubConnectionChange(connection, server_url);
|
|
}
|
|
|
|
// processes one message
|
|
internal static void process_message(byte[] data, ref List<PubSubUpdate> updates) {
|
|
bts.Log.Gen.info($"SingleServerSubscriberCore.process_message 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"]));
|
|
md.copyTo(op);
|
|
if (op is PubMetaData) {
|
|
PubMetaData p = op as PubMetaData;
|
|
//BCG.BlueTrace.WriteBlueCommReceiveW(p.tag, p.publishTimeTicks);
|
|
|
|
//check to see if this publish took a while to get here
|
|
p.warnIfLatencyExceedsThreshold();
|
|
//find out if we need to decompress this data
|
|
Stream realBody = bodyStream;
|
|
if (p.isCompressed) {
|
|
long compressedSize = bodyStream.Length - bodyStream.Position;
|
|
if (compressedSize == 0) {
|
|
string error_msg = "Error: tag " + p.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
|
|
bts.Log.Gen.error(error_msg);
|
|
return;
|
|
}
|
|
realBody = StreamDecompressor.decompressStream(bodyStream);
|
|
realBody.Position = bodyPosition;
|
|
}
|
|
updates.Add(new PubSubUpdate(p.tag, p.type, realBody));
|
|
} else if (op is TransactionMetaData) {
|
|
TransactionMetaData tr = op as TransactionMetaData;
|
|
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 {
|
|
bts.Log.assert_d(false, $"Unknown message received: {md} only Pub and TransactionMetaData are legal");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|