📄 publish.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using PublishSubscribe;
using Ticker;
/// usings for remoting
/// need to add the System.Runtime.Remoting reference
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace PublishSubscribe
{
/// <summary>
/// Main publish class that displays connection and subscription information
/// It also controls the publishing of new information to subscribers which
/// is controlled by a timer and has responsibility for the TCP channels that
/// allow remote access to the publisher
/// </summary>
public class PublishService : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.TextBox m_NumSubscriptions;
private System.Windows.Forms.TextBox m_TimerCalls;
/// <summary>
///声明一个发布者实例
///由于通道的模式为sington,该实例将为服务端和所有客户端共享的实例
/// delcare a member of the publisher class
/// </summary>
private static Publisher NewPublisher;
/// <summary>
/// 声明一个计数器,用于定时发布消息给订阅者
/// Set a timer to control the flow of the publications
/// </summary>
private Timer timer;
/// <summary>
/// 目前已经发布的次数
/// The number of timer calls. Needs to be static as it is incremented in the timer function
/// </summary>
private int nTimerCalls = 0;
/// <summary>
/// 声明一个TCP通道
/// The TCP Server Channel
/// </summary>
private TcpChannel Channel;
/// <summary>
/// 订阅者的数目
/// The number of subscriptions
/// </summary>
private int nSubscriptions = 0;
/// <summary>
/// 计数器事件响应函数
/// 用于定时发布消息给订阅者
/// The timer event to generate publications
/// </summary>
/// <param name="PassedObject">The Object that created the Timer</param>
/// <param name="TimerArgs">The Event Arguments that are passed to the Timer function</param>
private void TimerEvent( object PassedObject, EventArgs TimerArgs )
{
nTimerCalls++;
m_TimerCalls.Text = nTimerCalls.ToString();
if( NewPublisher != null )
NewPublisher.PublishNewEvents();
m_NumSubscriptions.Text = ( ( object )NewPublisher.NumSubscriptions ).ToString();
}
public PublishService()
{
//
//初始化窗口控件
//Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
///创建一个计数器实例
timer = new Timer();
/// 设置计数器的事件响应函数
/// 需要注意,该响应函数的参数必须包含一个对象和一个事件处理句柄
timer.Tick += new EventHandler( TimerEvent );
///设置计数间隔
///set the interval
timer.Interval = 5000;
///开始计数
///start the timer
timer.Start();
///创建一个通道实例,该通道使用端口8090
Channel = new TcpChannel( 8090 );
///注册通道
///register the channel with the runtime
ChannelServices.RegisterChannel( Channel );
///配置通道属性,通道名称为“PublishSubscribe”
///该通道传递的对象类型为Publisher,采用Singleton模式
///register the remote type and pass in a uri identifying string
RemotingConfiguration.RegisterWellKnownServiceType( typeof( Publisher ), "PublishSubscribe", WellKnownObjectMode.Singleton );
///获得一个对象实例
///get a remote instance to the singleton publisher object
try
{
NewPublisher = ( Publisher )Activator.GetObject( typeof( Publisher ), "tcp://localhost:8090/PublishSubscribe" );
}
///无法获得则捕捉异常,异常情况有可能是:指定的url地址不可达或者指定获得的对象定义不对
catch( NullReferenceException nullExp )
{
MessageBox.Show( "The url for the object is invalid " + nullExp.Message );
}
catch( RemotingException remExp )
{
MessageBox.Show( "The object type is not defined properly, it needs to be derived for a remoting class " + remExp.Message );
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.m_NumSubscriptions = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.m_TimerCalls = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(10, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(185, 19);
this.label1.TabIndex = 0;
this.label1.Text = "订阅者的数目";
//
// m_NumSubscriptions
//
this.m_NumSubscriptions.Location = new System.Drawing.Point(195, 9);
this.m_NumSubscriptions.Name = "m_NumSubscriptions";
this.m_NumSubscriptions.ReadOnly = true;
this.m_NumSubscriptions.Size = new System.Drawing.Size(128, 21);
this.m_NumSubscriptions.TabIndex = 1;
this.m_NumSubscriptions.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(10, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(128, 27);
this.label2.TabIndex = 2;
this.label2.Text = "发布次数";
//
// m_TimerCalls
//
this.m_TimerCalls.Location = new System.Drawing.Point(195, 37);
this.m_TimerCalls.Name = "m_TimerCalls";
this.m_TimerCalls.ReadOnly = true;
this.m_TimerCalls.Size = new System.Drawing.Size(128, 21);
this.m_TimerCalls.TabIndex = 3;
this.m_TimerCalls.Text = "";
//
// PublishService
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(614, 164);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.m_TimerCalls,
this.label2,
this.m_NumSubscriptions,
this.label1});
this.MaximizeBox = false;
this.Name = "PublishService";
this.Text = "发布者";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new PublishService());
}
private void OnPublishData(object sender, System.EventArgs e)
{
NewPublisher.PublishNewEvents();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -