done and done. wow, way easier.

This commit is contained in:
Adam R. Grey 2021-06-29 08:28:32 -04:00
parent 0636157a02
commit a6d0ce16a1
21 changed files with 28 additions and 54 deletions

View File

@ -12,7 +12,7 @@ namespace franz
{ {
public class Telefranz public class Telefranz
{ {
static class _TelefranzConsumers<T> where T : silver_messages.message, new() { static class _TelefranzConsumers<T> where T : silver_messages.message {
internal static readonly Dictionary<Action<T>, Action<KafkaRecord<string, string>>> wrappings internal static readonly Dictionary<Action<T>, Action<KafkaRecord<string, string>>> wrappings
= new Dictionary<Action<T>, Action<KafkaRecord<string, string>>>(); = new Dictionary<Action<T>, Action<KafkaRecord<string, string>>>();
} }
@ -78,11 +78,10 @@ namespace franz
}); });
} }
public void addHandler<T>(Action<T> theAction) where T : silver_messages.message, new() public void addHandler<T>(Action<T> theAction) where T : silver_messages.message
{ {
var dummy = new T(); Console.WriteLine($"typeof(T) reports as {typeof(T)}.");
Console.WriteLine($"created dummy, it reports as {dummy}. typeof(T) reports as {typeof(T)}. Topic is {dummy.topic}"); var topic = typeof(T).ToString();
var topic = dummy.topic;
Action<KafkaRecord<string, string>> wrapped = (kr) => Action<KafkaRecord<string, string>> wrapped = (kr) =>
{ {
@ -99,7 +98,7 @@ namespace franz
Console.WriteLine("message didn't deserialize for this handler"); Console.WriteLine("message didn't deserialize for this handler");
} }
}; };
if(!topicConsumers.ContainsKey(topic)){ if(!topicConsumers.ContainsKey(typeof(T).ToString())){
Console.WriteLine($"making consumer for {handling_group}, topic {topic}"); Console.WriteLine($"making consumer for {handling_group}, topic {topic}");
topicConsumers[topic] = new KafkaConsumer<string, string>(topic, clusterClient); topicConsumers[topic] = new KafkaConsumer<string, string>(topic, clusterClient);
topicConsumers[topic].ConsumeFromLatest(); topicConsumers[topic].ConsumeFromLatest();
@ -109,22 +108,21 @@ namespace franz
_TelefranzConsumers<T>.wrappings[theAction] = wrapped; _TelefranzConsumers<T>.wrappings[theAction] = wrapped;
topicSubscribers[topic]++; topicSubscribers[topic]++;
} }
public void removeHandler<T>(Action<T> theAction) where T : silver_messages.message, new() public void removeHandler<T>(Action<T> theAction) where T : silver_messages.message
{ {
var dummy = new T(); topicConsumers[typeof(T).ToString()].MessageReceived -= _TelefranzConsumers<T>.wrappings[theAction];
topicConsumers[dummy.topic].MessageReceived -= _TelefranzConsumers<T>.wrappings[theAction];
_TelefranzConsumers<T>.wrappings.Remove(theAction); _TelefranzConsumers<T>.wrappings.Remove(theAction);
topicSubscribers[dummy.topic]--; topicSubscribers[typeof(T).ToString()]--;
if(topicSubscribers[dummy.topic] == 0) if(topicSubscribers[typeof(T).ToString()] == 0)
{ {
topicConsumers[dummy.topic].StopConsume(); topicConsumers[typeof(T).ToString()].StopConsume();
} }
} }
public void ProduceMessage<T>(T message) where T : silver_messages.message public void ProduceMessage<T>(T message) where T : silver_messages.message
{ {
Console.WriteLine($"producing message {message}"); Console.WriteLine($"producing message {message}");
clusterClient.Produce(message.topic, message.ToString()); clusterClient.Produce(typeof(T).ToString(), message.ToString());
} }
} }
} }

View File

@ -4,7 +4,7 @@ using silver_messages;
namespace silver_messages.directorial namespace silver_messages.directorial
{ {
public class check_complete : message_directorial public class check_complete : silver_messages.message
{ {
public string check { get; set; } public string check { get; set; }
public string result { get; set; } public string result { get; set; }

View File

@ -4,7 +4,7 @@ using silver_messages;
namespace silver_messages.directorial namespace silver_messages.directorial
{ {
public class command_completed : message_directorial public class command_completed : silver_messages.message
{ {
//name of the command that was called //name of the command that was called
public string command { get; set; } public string command { get; set; }

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using silver_messages; using silver_messages;
namespace silver_messages.directorial namespace silver_messages.directorial
{ {
public class command_error : message_directorial public class command_error : silver_messages.message
{ {
//name of the command that was called //name of the command that was called
public string command { get; set; } public string command { get; set; }

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using silver_messages; using silver_messages;
namespace silver_messages.directorial namespace silver_messages.directorial
{ {
public class command_expired : message_directorial public class command_expired : silver_messages.message
{ {
//name of the command that was called //name of the command that was called
public string command { get; set; } public string command { get; set; }

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using silver_messages; using silver_messages;
namespace silver_messages.directorial namespace silver_messages.directorial
{ {
public class command_output : message_directorial public class command_output : silver_messages.message
{ {
//name of the command that was called //name of the command that was called
public string command { get; set; } public string command { get; set; }

View File

@ -7,7 +7,7 @@ namespace silver_messages.directorial
/* /*
* run it and send a check_complete * run it and send a check_complete
*/ */
public class execute_check : message_directorial public class execute_check : silver_messages.message
{ {
public string check { get; set; } public string check { get; set; }
public List<string> args { get; set; } = new List<string>(); public List<string> args { get; set; } = new List<string>();

View File

@ -12,7 +12,7 @@ namespace silver_messages.directorial
* command_error as you get them, and if it ends some other way, send command_completed * command_error as you get them, and if it ends some other way, send command_completed
* with empty stdout and stderr. * with empty stdout and stderr.
*/ */
public class execute_command : message_directorial public class execute_command : silver_messages.message
{ {
public string command { get; set; } public string command { get; set; }
public List<string> args { get; set; } = new List<string>(); public List<string> args { get; set; } = new List<string>();

View File

@ -1,7 +0,0 @@
namespace silver_messages
{
public abstract class message_directorial : silver_messages.message
{
public override string topic => "silver_messages_directorial";
}
}

View File

@ -1,7 +0,0 @@
namespace silver_messages
{
public class message_global : silver_messages.message
{
public override string topic => "silver_messages_global";
}
}

View File

@ -2,7 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace silver_messages.global namespace silver_messages.global
{ {
public class report : message_global public class report : silver_messages.message
{ {
//your name //your name
public string name { get; set; } public string name { get; set; }

View File

@ -4,7 +4,7 @@ using silver_messages;
namespace silver_messages.global namespace silver_messages.global
{ {
public class restart : message_global public class restart : silver_messages.message
{ {
//check if this is you (i.e., your handling group). If so, restart. Yourself, not the hardware. //check if this is you (i.e., your handling group). If so, restart. Yourself, not the hardware.
public string name { get; set; } public string name { get; set; }

View File

@ -5,6 +5,6 @@ using silver_messages;
namespace silver_messages.global namespace silver_messages.global
{ {
//if you receive this, respond with a report //if you receive this, respond with a report
public class sound_off : message_global { } public class sound_off : silver_messages.message { }
} }

View File

@ -4,7 +4,7 @@ using silver_messages;
namespace silver_messages.global namespace silver_messages.global
{ {
public class stop : message_global public class stop : silver_messages.message
{ {
//check if this is you (i.e., your handling group). If so, stop. Yourself, not the hardware. //check if this is you (i.e., your handling group). If so, stop. Yourself, not the hardware.
public string name { get; set; } public string name { get; set; }

View File

@ -9,10 +9,7 @@ namespace silver_messages
public abstract class message : IMemorySerializable public abstract class message : IMemorySerializable
{ {
public string type { get { return this.GetType().ToString(); } } public string type { get { return this.GetType().ToString(); } }
public abstract string topic { get; }
public override string ToString() public override string ToString()
{ {
return JsonConvert.SerializeObject(this); return JsonConvert.SerializeObject(this);

View File

@ -1,7 +0,0 @@
namespace silver_messages
{
public abstract class message_yt : silver_messages.message
{
public override string topic => "silver_messages_youtube";
}
}

View File

@ -8,7 +8,7 @@ namespace silver_messages.youtube
* for all the scheduled, not-yet released videos, what required metadata * for all the scheduled, not-yet released videos, what required metadata
* do they need * do they need
*/ */
public class metadata_needed : message_yt public class metadata_needed : silver_messages.message
{ {
//key is a yt id //key is a yt id
public Dictionary<string, yt_metadata> needed{get;set;} = new Dictionary<string, yt_metadata>(); public Dictionary<string, yt_metadata> needed{get;set;} = new Dictionary<string, yt_metadata>();

View File

@ -8,5 +8,5 @@ namespace silver_messages.youtube
* check all the scheduled, not-yet released videos for required metadata. * check all the scheduled, not-yet released videos for required metadata.
* respond with a metadata_needed - empty if none. * respond with a metadata_needed - empty if none.
*/ */
public class request_metadata_needed : message_yt { } public class request_metadata_needed : silver_messages.message { }
} }

View File

@ -7,7 +7,7 @@ namespace silver_messages.youtube
/* /*
* someone will send as much metadata as they have - set non-null fields to the provided values * someone will send as much metadata as they have - set non-null fields to the provided values
*/ */
public class update_metadata : message_yt public class update_metadata : silver_messages.message
{ {
public string yt_id { get; set; } public string yt_id { get; set; }
public yt_metadata metadata { get; set; } public yt_metadata metadata { get; set; }

View File

@ -7,7 +7,7 @@ namespace silver_messages.youtube
/** if you receive this, start the upload (of filename with metadata) /** if you receive this, start the upload (of filename with metadata)
* and then respond with an upload_started * and then respond with an upload_started
*/ */
public class upload : message_yt public class upload : silver_messages.message
{ {
public string filename { get; set; } public string filename { get; set; }
public yt_metadata metadata { get; set; } public yt_metadata metadata { get; set; }

View File

@ -4,7 +4,7 @@ using System.IO;
using silver_messages; using silver_messages;
namespace silver_messages.youtube namespace silver_messages.youtube
{ {
public class upload_started : message_yt public class upload_started : silver_messages.message
{ {
public string filename { get; set; } public string filename { get; set; }
public string yt_id { get; set; } public string yt_id { get; set; }