Aggiunta preliminare MessagePipe
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
|
||||
namespace SteamWare
|
||||
{
|
||||
/// <summary>
|
||||
/// Classe gestione PubSub messaggi REDIS
|
||||
/// </summary>
|
||||
public class MessagePipe
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
private bool enableLog = false;
|
||||
private IConnectionMultiplexer redis;
|
||||
private IDatabase redisDb;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Protected Fields
|
||||
|
||||
/// <summary>
|
||||
/// Obj x gestione log
|
||||
/// </summary>
|
||||
protected static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
#endregion Protected Fields
|
||||
|
||||
#region Public Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Init canale di comunicazione PubSub via REDIS
|
||||
/// </summary>
|
||||
/// <param name="channelName"></param>
|
||||
/// <param name="enableLog"></param>
|
||||
public MessagePipe(string channelName, bool enableLog = false)
|
||||
{
|
||||
_channel = channelName;
|
||||
redis = memLayer.ML.connRedis;
|
||||
redisDb = redis.GetDatabase();
|
||||
this.enableLog = enableLog;
|
||||
// aggiungo sottoscrittore
|
||||
setupSubscriber();
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Events
|
||||
|
||||
/// <summary>
|
||||
/// Messaggio veicolato dal canale
|
||||
/// </summary>
|
||||
public event EventHandler EA_NewMessage = delegate { };
|
||||
|
||||
#endregion Public Events
|
||||
|
||||
#region Private Properties
|
||||
|
||||
/// <summary>
|
||||
/// Canale associato al gestore pipeline messaggi
|
||||
/// </summary>
|
||||
private string _channel { get; set; } = "";
|
||||
|
||||
#endregion Private Properties
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private void setupSubscriber()
|
||||
{
|
||||
ISubscriber sub = redis.GetSubscriber();
|
||||
//Subscribe to the channel named messages
|
||||
sub.Subscribe(_channel, (channel, message) =>
|
||||
{
|
||||
Log.Trace($"ch {channel} | {message}");
|
||||
// messaggio
|
||||
PubSubEventArgs mea = new PubSubEventArgs(message);
|
||||
// se qualcuno ascolta sollevo evento nuovo valore...
|
||||
if (EA_NewMessage != null)
|
||||
{
|
||||
EA_NewMessage(this, mea);
|
||||
}
|
||||
});
|
||||
Log.Info($"Subscribed {_channel}");
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
#region Public Methods
|
||||
|
||||
/// <summary>
|
||||
/// Invio messaggio sul canale + salvataggio in cache REDIS
|
||||
/// </summary>
|
||||
/// <param name="memKey">Chiave REDIS x salvare valore</param>
|
||||
/// <param name="message"></param>
|
||||
public bool saveAndSendMessage(string memKey, string message)
|
||||
{
|
||||
bool answ = false;
|
||||
// invio notifica tramite il canale richiesto
|
||||
answ = sendMessage(message);
|
||||
if (redisDb != null)
|
||||
{
|
||||
redisDb.StringSetAsync(memKey, message);
|
||||
if (enableLog)
|
||||
{
|
||||
Log.Info($"Redis Cache Key: {memKey}");
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invio messaggio sul canale
|
||||
/// </summary>
|
||||
/// <param name="newMess"></param>
|
||||
/// <returns></returns>
|
||||
public bool sendMessage(string newMess)
|
||||
{
|
||||
bool answ = false;
|
||||
ISubscriber sub = redis.GetSubscriber();
|
||||
sub.Publish(_channel, newMess);
|
||||
return answ;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evento messaggio PubSub Redis
|
||||
/// </summary>
|
||||
public class PubSubEventArgs : EventArgs
|
||||
{
|
||||
#region Public Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Costruttore
|
||||
/// </summary>
|
||||
/// <param name="messaggio"></param>
|
||||
public PubSubEventArgs(string messaggio)
|
||||
{
|
||||
this.newMessage = messaggio;
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Properties
|
||||
|
||||
/// <summary>
|
||||
/// Messaggio trasportato
|
||||
/// </summary>
|
||||
public string newMessage { get; set; } = "";
|
||||
|
||||
#endregion Public Properties
|
||||
}
|
||||
}
|
||||
@@ -289,6 +289,7 @@
|
||||
<Compile Include="log2note.cs" />
|
||||
<Compile Include="logger.cs" />
|
||||
<Compile Include="memLayer.cs" />
|
||||
<Compile Include="MessagePipe.cs" />
|
||||
<Compile Include="NetworkConnection.cs" />
|
||||
<Compile Include="pdfUtils.cs" />
|
||||
<Compile Include="reportGenObj.cs" />
|
||||
|
||||
Generated
+92
@@ -92,6 +92,13 @@
|
||||
this.label9 = new System.Windows.Forms.Label();
|
||||
this.clockTimer = new System.Windows.Forms.Timer(this.components);
|
||||
this.LogTimer = new System.Windows.Forms.Timer(this.components);
|
||||
this.tabPage8 = new System.Windows.Forms.TabPage();
|
||||
this.label12 = new System.Windows.Forms.Label();
|
||||
this.txtChannelName = new System.Windows.Forms.TextBox();
|
||||
this.btnSendMessage = new System.Windows.Forms.Button();
|
||||
this.txtMessaggio = new System.Windows.Forms.TextBox();
|
||||
this.btnStartSub = new System.Windows.Forms.Button();
|
||||
this.lblChannerReceive = new System.Windows.Forms.Label();
|
||||
this.groupBox1.SuspendLayout();
|
||||
this.tabControl1.SuspendLayout();
|
||||
this.tabPage1.SuspendLayout();
|
||||
@@ -105,6 +112,7 @@
|
||||
this.grpPublisher.SuspendLayout();
|
||||
this.groupBox2.SuspendLayout();
|
||||
this.tabPage7.SuspendLayout();
|
||||
this.tabPage8.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// groupBox1
|
||||
@@ -278,6 +286,7 @@
|
||||
this.tabControl1.Controls.Add(this.tabPage5);
|
||||
this.tabControl1.Controls.Add(this.tabPage6);
|
||||
this.tabControl1.Controls.Add(this.tabPage7);
|
||||
this.tabControl1.Controls.Add(this.tabPage8);
|
||||
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
|
||||
this.tabControl1.Location = new System.Drawing.Point(0, 0);
|
||||
this.tabControl1.Name = "tabControl1";
|
||||
@@ -777,6 +786,80 @@
|
||||
this.LogTimer.Interval = 10;
|
||||
this.LogTimer.Tick += new System.EventHandler(this.LogTimer_Tick);
|
||||
//
|
||||
// tabPage8
|
||||
//
|
||||
this.tabPage8.Controls.Add(this.lblChannerReceive);
|
||||
this.tabPage8.Controls.Add(this.btnStartSub);
|
||||
this.tabPage8.Controls.Add(this.txtMessaggio);
|
||||
this.tabPage8.Controls.Add(this.btnSendMessage);
|
||||
this.tabPage8.Controls.Add(this.txtChannelName);
|
||||
this.tabPage8.Controls.Add(this.label12);
|
||||
this.tabPage8.Location = new System.Drawing.Point(4, 22);
|
||||
this.tabPage8.Name = "tabPage8";
|
||||
this.tabPage8.Padding = new System.Windows.Forms.Padding(3);
|
||||
this.tabPage8.Size = new System.Drawing.Size(768, 353);
|
||||
this.tabPage8.TabIndex = 7;
|
||||
this.tabPage8.Text = "PubSub (REDIS)";
|
||||
this.tabPage8.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// label12
|
||||
//
|
||||
this.label12.AutoSize = true;
|
||||
this.label12.Location = new System.Drawing.Point(20, 17);
|
||||
this.label12.Name = "label12";
|
||||
this.label12.Size = new System.Drawing.Size(77, 13);
|
||||
this.label12.TabIndex = 0;
|
||||
this.label12.Text = "Channel Name";
|
||||
//
|
||||
// txtChannelName
|
||||
//
|
||||
this.txtChannelName.Location = new System.Drawing.Point(103, 14);
|
||||
this.txtChannelName.Name = "txtChannelName";
|
||||
this.txtChannelName.Size = new System.Drawing.Size(152, 20);
|
||||
this.txtChannelName.TabIndex = 1;
|
||||
this.txtChannelName.Text = "myTestChannel";
|
||||
//
|
||||
// btnSendMessage
|
||||
//
|
||||
this.btnSendMessage.Location = new System.Drawing.Point(23, 62);
|
||||
this.btnSendMessage.Name = "btnSendMessage";
|
||||
this.btnSendMessage.Size = new System.Drawing.Size(74, 23);
|
||||
this.btnSendMessage.TabIndex = 2;
|
||||
this.btnSendMessage.Text = "Send";
|
||||
this.btnSendMessage.UseVisualStyleBackColor = true;
|
||||
this.btnSendMessage.Click += new System.EventHandler(this.btnSendMessage_Click);
|
||||
//
|
||||
// txtMessaggio
|
||||
//
|
||||
this.txtMessaggio.Location = new System.Drawing.Point(103, 62);
|
||||
this.txtMessaggio.Name = "txtMessaggio";
|
||||
this.txtMessaggio.Size = new System.Drawing.Size(152, 20);
|
||||
this.txtMessaggio.TabIndex = 4;
|
||||
this.txtMessaggio.Text = "...";
|
||||
this.txtMessaggio.TextChanged += new System.EventHandler(this.txtMessaggio_TextChanged);
|
||||
//
|
||||
// btnStartSub
|
||||
//
|
||||
this.btnStartSub.Location = new System.Drawing.Point(619, 17);
|
||||
this.btnStartSub.Name = "btnStartSub";
|
||||
this.btnStartSub.Size = new System.Drawing.Size(141, 23);
|
||||
this.btnStartSub.TabIndex = 5;
|
||||
this.btnStartSub.Text = "Start Channel Sub";
|
||||
this.btnStartSub.UseVisualStyleBackColor = true;
|
||||
this.btnStartSub.Click += new System.EventHandler(this.btnStartSub_Click);
|
||||
//
|
||||
// lblChannerReceive
|
||||
//
|
||||
this.lblChannerReceive.AutoSize = true;
|
||||
this.lblChannerReceive.BackColor = System.Drawing.Color.Black;
|
||||
this.lblChannerReceive.ForeColor = System.Drawing.Color.Gold;
|
||||
this.lblChannerReceive.Location = new System.Drawing.Point(360, 67);
|
||||
this.lblChannerReceive.MinimumSize = new System.Drawing.Size(400, 100);
|
||||
this.lblChannerReceive.Name = "lblChannerReceive";
|
||||
this.lblChannerReceive.Size = new System.Drawing.Size(400, 100);
|
||||
this.lblChannerReceive.TabIndex = 6;
|
||||
this.lblChannerReceive.Text = "...";
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
@@ -810,6 +893,8 @@
|
||||
this.groupBox2.PerformLayout();
|
||||
this.tabPage7.ResumeLayout(false);
|
||||
this.tabPage7.PerformLayout();
|
||||
this.tabPage8.ResumeLayout(false);
|
||||
this.tabPage8.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
@@ -879,6 +964,13 @@
|
||||
private System.Windows.Forms.Label lblOutUpdMan;
|
||||
private System.Windows.Forms.Button btnReadUpdMan;
|
||||
private System.Windows.Forms.Button btnDownload;
|
||||
private System.Windows.Forms.TabPage tabPage8;
|
||||
private System.Windows.Forms.Button btnStartSub;
|
||||
private System.Windows.Forms.TextBox txtMessaggio;
|
||||
private System.Windows.Forms.Button btnSendMessage;
|
||||
private System.Windows.Forms.TextBox txtChannelName;
|
||||
private System.Windows.Forms.Label label12;
|
||||
private System.Windows.Forms.Label lblChannerReceive;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -293,5 +293,46 @@ namespace TestBench
|
||||
}
|
||||
|
||||
#endregion Private Methods
|
||||
|
||||
protected MessagePipe MPipeChannel = new MessagePipe("testCh");
|
||||
|
||||
private void btnStartSub_Click(object sender, EventArgs e)
|
||||
{
|
||||
// avvio un nuovo messaggePipe x il canale richiesto...
|
||||
MPipeChannel = new MessagePipe(txtChannelName.Text, false);
|
||||
MPipeChannel.EA_NewMessage += MPipeChannel_EA_NewMessage;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendLine("--------------------------------------");
|
||||
sb.AppendLine($" Start PubSub | {DateTime.Now:HH:mm:ss}");
|
||||
sb.AppendLine("--------------------------------------");
|
||||
lblChannelReceive.Text = sb.ToString();
|
||||
lblChannelReceive.Refresh();
|
||||
}
|
||||
|
||||
private void MPipeChannel_EA_NewMessage(object sender, EventArgs e)
|
||||
{
|
||||
// accodo messaggio...
|
||||
PubSubEventArgs currArgs = (PubSubEventArgs)e;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendLine($"{currArgs.newMessage} | RECEIVED: {DateTime.Now:HH:mm:ss.ffffff}");
|
||||
string oldMess = lblChannelReceive.Text;
|
||||
lblChannelReceive.Text = $"{sb.ToString()}{oldMess}";
|
||||
lblChannelReceive.Refresh();
|
||||
}
|
||||
|
||||
protected string testoMessaggio
|
||||
{
|
||||
get => $"SEND: {DateTime.Now:HH:mm:ss.ffffff} | {txtMessaggio.Text}";
|
||||
}
|
||||
|
||||
private void btnSendMessage_Click(object sender, EventArgs e)
|
||||
{
|
||||
MPipeChannel.sendMessage(testoMessaggio);
|
||||
}
|
||||
|
||||
private void txtMessaggio_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
//MPipeChannel.sendMessage(testoMessaggio);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user