The network Event Sender/Receiver in C#

Do you have questions about writing plugins or scripts in Python? Meet the coders here.

The network Event Sender/Receiver in C#

Postby miljbee » Tue Nov 23, 2010 10:19 am

Hello,

I am currently rewritting some part of my Home Automation System with C#.

I needed bidirectionnal dialog with EG.

So I wrote the network event receiver and sender in C#.
They will enable to exchange events in both directions between EG and your own C# App.


It might not be the cleannest code you would expect since I haven't a lot of experience with C#. I learn it by myself, and it isn't always easy. Also, you won't find much comments in the code, but I think it's simple enough for you to understand !

Regards
Attachments
EG_NetSendrRcvr.zip
(43.82 KiB) Downloaded 210 times
miljbee
Experienced User
 
Posts: 140
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Re: The network Event Sender/Receiver in C#

Postby jwka » Sun Dec 12, 2010 5:26 pm

Hey miljbee,

could you provide a little bit of documentation aside? Being not a c# programmer, I have no idea how I could utilize your stuff in eg.

Thanks
jwka
jwka
 
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: The network Event Sender/Receiver in C#

Postby miljbee » Mon Dec 13, 2010 9:06 am

Hello,

You might find this code useful if you need to develop some functionality outside EG with C#, but still need to have you C# prog communicate with EG.

The provided code implements a network sender and a network receiver in C#. It is ready to be included in any C# Code.
It speaks threw the network with the network event receiver plugin provided with eg, and also with the network event sender plugin also provided with eg (see the plugins list)

Here is how to use it :
You will find an EG_Network_Event_Receiver_Sender class. This class implements both the server and the client. It means that you can send event to eg with it and also that eg can send event to your C# App.

When you create a new EG_Network_Event_Receiver_Sender object, the server starts automatically :
Code: Select all
EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);

The parameters are :
    "localIp:Port" : localIp is the Ip Adress on which the server will listen. if you put *, it will listen on any interface of your computer.
    "password" : password is the password needed to connect to your server
    false : specify if your server supports enduring events or not (the network event sender/receiver of eg always generate enduring events, but in a manner that is useless. It's up to you to decide if you need it or not).
Once the server is started, if a client sends him an eg event, it will fire an event (I mean a C# event). To have your code react to this event, you need to subscribe to it.

This is done by writting this :
Code: Select all
eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);

When you write this, you tell C# that when the server receives an event from an eg client, it shoud run eg_Event_FromNetworkEventSender
So you need to write an eg_Event_FromNetworkEventSender method :
Code: Select all
        static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
        {
            Console.WriteLine("Event : " + e.name);
            foreach (string k in e.payload)
            {
                Console.WriteLine("  payload = " + k);
            }
            Console.WriteLine("");
        }

In this example, the event is just printed to the console, but you might code whatever you need here. It's up to you to decide. You get the name of the event in e.name and the payload in e.payload.

The EG_Network_Event_Receiver_Sender also implements a client.
Once you have created you EG_Network_Event_Receiver_Sender object, you can just call :
Code: Select all
eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })

This will actually send testEvent to the server that listen on 127.0.0.1:25632. The last param is the payload which will be send with the event. It must be a string array.
If all went fine, this method will return "success". Otherwise, it will return a string that indicates where things gets wrong :
"Error while trying to receive the \"accept\"" which might reveal a password problem.


If you directly run my code without modifying it, it will :
    Create the server :
    Code: Select all
                EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);

    Then subscribe to the events that the server might fire :
    Code: Select all
                eg.EnduringEvent_FromNetworkEventSender_Start += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_Start_Handler(eg_EnduringEvent_FromNetworkEventSender_Start);
                eg.EnduringEvent_FromNetworkEventSender_End += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_End_Handler(eg_EnduringEvent_FromNetworkEventSender_End);
                eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);

    Then wait a few miliseconds to let the server start
    Code: Select all
                System.Threading.Thread.Sleep(500);

    Then send and event to the server which has just started and report any error :
    Code: Select all
                if ("success" != (status = eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })))
                {
                    Console.WriteLine(status);
                }


    Since we created a server that doesn't support enduring events (false parameter) the server will only fire Event_FromNetworkEventSender. When this event is fired, we told the server to run
    Code: Select all
            static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
            {
                Console.WriteLine("Event : " + e.name);
                foreach (string k in e.payload)
                {
                    Console.WriteLine("  payload = " + k);
                }
                Console.WriteLine("");
            }

    So the event that we have just sent is simply printed to the console.

    The demo is over, so the server is stopped properly before the program terminates :
    Code: Select all
                eg.StopLocalNetworkEventReceiver();




I hope these short explanations will help you. Let me know if you need more informations.

Regards,
miljbee
Experienced User
 
Posts: 140
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France


Return to Coding Corner

Who is online

Users browsing this forum: No registered users and 3 guests