using bts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
namespace BCG {
///
/// Deals with the subscriptions (and getting data) from one single
/// 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).
///
internal interface ISingleServerSubscriberCore : IDisposable {
IEnumerable get_updates(TimeSpan timeout);
event PubSubConnectionChange onPubSubConnectionChange;
void subscribe(params string[] tags);
void unsubscribe(params string[] tags);
}
internal class SingleServerSubscriberCore : IDisposable, ISingleServerSubscriberCore {
private readonly IPubSubServerConnection _connection;
private SyncQueue _in_data_queue = new SyncQueue();
private HashSet _tags = new HashSet();
public event PubSubConnectionChange? onPubSubConnectionChange;
public SingleServerSubscriberCore(RemoteConnectionInfo server_rci, PubSubServerConnectionFactory connection_factory) {
bts.Log.Gen.info($"SingleServerSubscriberCore constructed for server_url: {server_rci}");
_connection = connection_factory.get_connection(server_rci);
_connection.register_connection_callback(new PubSubServerConnectionStatusChange(_handle_server_reboot));
_connection.register_data_callback(_on_data);
}
// gets as many data items that it can get in
// timeout milliseconds
public IEnumerable get_updates(TimeSpan timeout) {
_connection.doWork();
//List rv = new List();
List conn_data = _in_data_queue.dequeueMultipleElements(TimeSpan.Zero, 1024); // TODO, max_elements
if (conn_data.Count > 0) {
bts.Log.Gen.info($"SingleServerSubscriberCore.get_updates n_updates: {conn_data.Count}");
}
List rv = new List();
foreach (var m in conn_data) {
var msg = new PubSubServerMessage();
msg.construct_deserialize(m);
if (_tags.Contains(msg.tag)) {
rv.Add(msg);
} else {
Log.Gen.warn($"SingleServerSubscriberCore.get_updates msg: {msg} does not have a subscription");
}
}
return rv;
}
private void _on_data(string sender, byte[] data) {
var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data, 4);
bts.Log.Gen.info($"SingleServerSubscriberCore._on_data sender: {sender} data len: {data.Length} data: {data_txt}");
_in_data_queue.enqueue(data);
// TODO: enqueue, and dequeue in get_updates
}
public void subscribe(params string[] tags) {
HashSet this_call_tags = new HashSet();
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 unsub_tags = new List();
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");
}
}
bts.PubSubClientMessage msg = new bts.PubSubClientMessage() {
msg_type = bts.PubSubMessageType.UNSUBSCRIIBE,
unsub_msg = new bts.UnsubscribeMsg() {
tags = unsub_tags.ToArray(),
connection = _connection.client_url,
subscribing_app = ClientInfo.thisClient.processString
}
};
_connection.send(msg);
}
#region IDisposable Members
public void Dispose() {
///remove the subscriber
//myConnection.send(new PubSubMessage(new MetaData(new RemoveSubscriberMetaData(myConnection.clientURL))).toStream());
// unsub all ?
_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[] tag_args) {
// coonnection and app
bts.PubSubClientMessage msg = new bts.PubSubClientMessage() {
msg_type = bts.PubSubMessageType.SUBSCRIBE,
sub_msg = new bts.SubscribeMsg() {
connection = _connection.client_url,
subscribing_app = ClientInfo.thisClient.processString,
tags = tag_args
}
};
Log.Gen.info($"SingleServerSubscriberCore._subscribe sending msg: {msg}");
_connection.send(msg);
}
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 updates) {
// bts.Log.Gen.info($"SingleServerSubscriberCore.process_message data_len: {data.Length}");
// if (data != null && data.Length > 0) {
// MemoryStream str = new MemoryStream(data);
// bts.PubSubServerMessage serv_msg = new bts.PubSubServerMessage();
// serv_msg.consruct_deserialize(str);
// updates.Add(serv_msg);
// }
// }
}
}