user defined parsing/data splitting

ZeroEffect

Junior Contributor
Joined
Oct 24, 2004
Messages
204
Location
Detroit, MI
I have been thinking about this for awhile, most of the applications i make receive UDP data or send UDP data. As I write the application it all ways is taylored to the sending or receiveing application. But if the sending or receiving application changes I have to retool the app I made and reinstall it.

How would I go about make a setting in a config file that would be the format of the incomming data. Am i making sence? here is an example..

Sending application "A" sends data out like this

|^info1~info2~info3~info4|

How would i make a user changable setting in my config file to represent that?

UDPRecString=??????

thanks for any help you may be able to give me.

ZeroEffect
 
What you want to do is make a plugin. To do this you define an interface that provides the contract information that plugins must comply with. It could be something as simple as:
C#:
interface IUDPStringParser
{
    public MyClass[] ParseIncomingString(string incomingString);
}
You then compile this interface into your assembly. Plugins reference your assembly and make a class that implements your interface. Next, in your .config file, you specify something like:
Code:
<UDPStringParser>
  <Assembly>MyPlugin.dll</Assembly>
  <Class>MyPluginClass</Class>
</UDPStringParser>
When your application loads it reflects on the "MyPlugin.dll" assembly and creates an instance of "MyPlugin" class (which has implemented the "IUDPStringParser" interface). You can then call the "ParseIncomingString" method when you get a message sent to you and get back an array of "MyClass" (which would be whatever you wanted).

This way the functionality that changes somewhat frequently is loosely coupled from the rest of the application. You simply need to compile a new DLL and update the .config file when you want to make a change.

The most difficult thing about this is understanding and working with Reflection.
 
Gill Bates said:
What you want to do is make a plugin. To do this you define an interface that provides the contract information that plugins must comply with. It could be something as simple as:
C#:
interface IUDPStringParser
{
    public MyClass[] ParseIncomingString(string incomingString);
}
You then compile this interface into your assembly. Plugins reference your assembly and make a class that implements your interface. Next, in your .config file, you specify something like:
Code:
<UDPStringParser>
  <Assembly>MyPlugin.dll</Assembly>
  <Class>MyPluginClass</Class>
</UDPStringParser>
When your application loads it reflects on the "MyPlugin.dll" assembly and creates an instance of "MyPlugin" class (which has implemented the "IUDPStringParser" interface). You can then call the "ParseIncomingString" method when you get a message sent to you and get back an array of "MyClass" (which would be whatever you wanted).

This way the functionality that changes somewhat frequently is loosely coupled from the rest of the application. You simply need to compile a new DLL and update the .config file when you want to make a change.

The most difficult thing about this is understanding and working with Reflection.

I understand the idea of a plugin dll, but i have never made a dll before. I code in VB.net Ibelieve you can make dll files in vb.net. I understand how to reference it in the assembly. Now in the coding of the dll all I do is just breakdown the know incoming string and assign it to the correct variables? I See you say I'd get an array back from the received data. So data comes in and I call the interface like this?

Visual Basic:
Dim tempData() as Array
tempData = ParseIncomingString(incommingData)
'then assign the received data

Where would I start with the coding of the dll file?

Thanks for your Help

ZeroEffect
 
By DLL I mean a "Class Library". VB.NET has this available as a type of project.

By using a class library you can decouple the application logic that has a need to be somewhat dynamic.

In your main application assembly you would do something like:
Visual Basic:
' *** These get compiled into Assembly #1 ***
    ' the "contract" that plugin classes must implement
    Public Interface IUDPStringParser

        Function ParseUDPString(ByVal UDPString As String) As MyClassAboutUDPStrings()

    End Interface

    Public Class MyClassAboutUDPStrings

        ' stick whatever here...

    End Class
The plugin assembly (MyPlugin.dll) would reference Assembly #1 and implement the interface:
Visual Basic:
    Public Class MyPluginClass
        Implements IUDPStringParser

        Public Function ParseUDPString(ByVal UDPString As String) As MyClassAboutUDPStrings() Implements IUDPStringParser.ParseUDPString

            ' do whatever to UDPString

            Dim list As New System.Collections.ArrayList()
            list.Add(New MyClassAboutUDPStrings)

            Return list.ToArray

        End Function

    End Class
Using the settings in the .config file, you would then create an instance of the class that implements your interface:
Visual Basic:
' use reflection to create an instance of "MyPluginClass" in the "MyPlugin.dll" assembly
        ' this is the tricky part.

        Dim array = plugin.ParseUDPString("|^info1~info2~info3~info4|")

        ' do whatever with the items in array...
 
I may have misrepresented the .config file. By config file i ment like an INI file. Moving on.

So this would go into my main program
Visual Basic:
--------------------------------------------------------------------------------
' *** These get compiled into Assembly #1 ***
    ' the "contract" that plugin classes must implement
    Public Interface IUDPStringParser

        Function ParseUDPString(ByVal UDPString As String) As MyClassAboutUDPStrings()

    End Interface

    Public Class MyClassAboutUDPStrings

        ' stick whatever here...

    End Class

This is a new class that I can compile into a Dll

Visual Basic:
    Public Class MyPluginClass
        Implements IUDPStringParser

        Public Function ParseUDPString(ByVal UDPString As String) As MyClassAboutUDPStrings() Implements IUDPStringParser.ParseUDPString

            ' do whatever to UDPString

            Dim list As New System.Collections.ArrayList()
            list.Add(New MyClassAboutUDPStrings)

            Return list.ToArray

        End Function

    End Class

And this I can add the array part to my main program and the string in quotes I could retrieve from an INI file.

Visual Basic:
' use reflection to create an instance of "MyPluginClass" in the "MyPlugin.dll" assembly
        ' this is the tricky part.

        Dim array = plugin.ParseUDPString("|^info1~info2~info3~info4|")

        ' do whatever with the items in array...

Correct? you'll have to for give me my focus is not as great as it normally is I have a two month old at home who is having a bad day.

ZeroEffect
 
Looks right. I have intentionally left out the part about the Reflection. But, have a look in the System.Reflection namespace and you should be able to figure it out.
 
Oh NO!

Ok I have started to add/build this into a new project I am working on and I have run into one issue. In VB.Net I can't seem to find where I make a "Class Library" using VB.Net. But there is a light at the end of the tunnel. I do have C#.Net so I can make the DLL that way. But I am going to need way more help in coding this. I know very little about C#. So I am kinda stuck. I'll be looking through the forum for examples and how to code in C#.

So my main application is VB.Net and the DLL will be C#. The DLL's jo is to split/parse data and return it as an array.

ANY and I do mean ANY help is welcome,

Thank you,

ZeroEffect
 
I Love MSDN

Ok I found an artical with an example on the MSDN site. This is what I have come up with.
Code:
	public class SplitData
	{
		public SplitData(ByVal strData)
			{
			string delimStr = "~";
			char [] delimiter = delimStr.ToCharArray();
			string arrData = strData;
			string [] split = null;

			split = arrData.Split(delimiter, x);

			return split;
			}
	}

So here is what happens.

Main application recieves a string. That string is then "sent" to the dll to be processed. Dll returns an array back to the main application for processing.

So does it look like I am missing anything from the dll.

Thanks

ZeroEffect
 
Back
Top