如何在同一个类和其他类中的不同对象之间传递信息作为C#中的事件 [英] How to pass information between different objects from the same class and other classes as events in C#

查看:110
本文介绍了如何在同一个类和其他类中的不同对象之间传递信息作为C#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绘制了一个拓扑,其中每个节点都是SensorNode类的对象,蓝色链接表示每个节点与其邻居之间的链接,其中节点周围的圆圈表示每个节点的传输范围。接收器也是类Sink的对象。我需要在它们之间实例化消息传递和通信,但我不知道我应该使用什么机制在这些对象(传感器节点)之间执行消息传递,其中每个节点都有其唯一ID,接收器具有固定ID,在我的代码中为1因为我只使用一个接收器。



以下是我仍然坚持如何实现发送接收的类,并且在使这种通信适用方面是前进的在这些不同的对象之间...



1 - SensorNode类



I draw the a topology where each node is an object of class SensorNode, the blue links indicate the links between each node with its neighbors where the circles around the nodes represent the transmission range for each node. The sink also is an object of class Sink. I need to instantiate messaging and communication between them but I have no idea what mechanisms should I used to perform message passing between theses objects (sensor nodes) where each node has its unique ID, the sink has a fixed ID which is 1 in my code because I use only a single sink.

The following are the classes where I am still stuck to how to implement send receive, and forward in terms of making this communication applicable between these different objects...

1 - Class "SensorNode"

class SensorNode
    {
       public int snID;
       public string snName;
       public int snDepth;
       public DateTime schedulingTime;
       public double holdingTime;
       public double energy;

       public List<int> queue11 = new List<int>();
       public List<DateTime> queue12 = new List<DateTime>();

       public List<Packet> queue21 = new List<Packet>();
       public List<DateTime> queue22 = new List<DateTime>();

       public SensorNode(int id,string name,int depth, double energy)
       {
           this.snID = id;
           this.snName = name;
           this.snDepth = depth;
           this.energy = energy;
       }

       public void insertHistoryQueue(int packetID, DateTime receivingTime)
       {
           queue11.Add(packetID);
           queue12.Add(receivingTime);
       }

       public void insertPriorityQueue(Packet packet, DateTime schedulingTime)
       {
           queue21.Add(packet);
           queue22.Add(schedulingTime);
       }

       public DateTime schedulingTimeCalculations(double holdingTime, DateTime systemTime)       
       {
           schedulingTime = DateTime.FromOADate(holdingTime).Date + systemTime.TimeOfDay;

           return schedulingTime; 
       }

       public double holdingTimeCalculations(double alpha, double depth, double beta)
       {
           holdingTime = alpha * depth + beta;

           return holdingTime; 
       }

       public void receive(Packet packet)
       {
       }

       public void forward(Packet packet, int neighborID)
       {
       }

       public void remove()
       {
       }

       public void sendDirect(int rxID, Packet packet)
       {
       }
    }





2 - Sink类






2 - Class "Sink"


class Sink
    {
        public string name;
        public int sinkID;
        public int sinkX;
        public int sinkY;

        public List<Packet> queue1 = new List<Packet>();
        public List<DateTime> queue2 = new List<DateTime>();

        public Sink(string name, int Id , int xLocation, int yLocation)
        {
            this.name = name;
            this.sinkID = Id;
            this.sinkX = xLocation;
            this.sinkY = yLocation;
        }

        public void insert(Packet packet, DateTime receivingTime)
        {
            queue1.Add(packet);
            queue2.Add(receivingTime);
        }
    }









任意想法,我需要你的建议和帮助,因为我不知道如何在这些对象(传感器节点)之间以及传感器节点和接收器之间传递信息。在C#中负责此应用程序的库是什么?



我尝试了什么:



开始节点之间的通信(事件)





Any idea, I need your suggestions and your help as I do not have an idea how to pass information between these objects (sensor nodes) and between the sensor nodes and the sink. What is the library which is responsible for this application in C#?

What I have tried:

To start communication between nodes (events)

推荐答案

NotPolitcallyCorrect写的是正确的。也许下面的内容可能会给你一些起点。



What NotPolitcallyCorrect wrote is right. Perhaps the following might give you some starting point.

void Main()
{
	var s1 = new SensorNode(1, "don't care", 0, 0);
	var s2 = new SensorNode(2, "don't care", 0, 0);
	
	var sink = new Sink("don't care", 3, 0, 0);

	sink.AddSensor(s1);
	sink.AddSensor(s2);
	
	s1.SendNewData();
	s2.SendNewData();
	s1.SendNewData();

}


public class EventArgs<T> : EventArgs
{
	private readonly T _value;

	public EventArgs(T value)
	{
		_value = value;
	}

	public T Value
	{
		get { return _value; }
	}
}

public class Packet
{ 
	public int SensorId { get; set; }
	public string theData { get; set; }
	public Packet(int id, string data)
	{
		SensorId = id;
		theData = data;
	}
}

public interface ISensorNode
{ 
	event EventHandler<EventArgs<Packet>> DataSend;
}


class SensorNode : ISensorNode
{
	public event EventHandler<EventArgs<Packet>> DataSend;
	public int Id { get; set; }
	public SensorNode(int id,string name,int depth, double energy)
	{
		Id = id;
	}

	public void SendNewData()
	{
		Packet p = new Packet(Id, "123.4");
		var handler = DataSend;
		var args = new EventArgs<Packet>(p);
		handler?.Invoke(this, args);
	}
	
}


class Sink
{ 
	public Sink(string name, int Id , int xLocation, int yLocatio)
	{
		
	}

	public void AddSensor(ISensorNode newSensor)
	{		
		newSensor.DataSend += OnDataReceived;
	}

	private void OnDataReceived(object sender, EventArgs<Packet> e)
	{
		Packet packed = e.Value;
		Console.WriteLine(


从ID为{packed.SensorId}的传感器接收数据{packed.theData};
}
}
"received data {packed.theData} from sensor with id {packed.SensorId}"); } }


这篇关于如何在同一个类和其他类中的不同对象之间传递信息作为C#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆