2: ‘tower, do you read me?’

start using the incoming information!
In the last section we configured a .xml file. If you have used the –httpd- option, you know about the tons of information FlightGear is offering. But let’s look a little more detailed at our information, specified by the .xml on the previous page.

FlightGear’s output should look something like this:


59|9|0|0||newline
63|15|-3|10||newline
64|21|-5|7||newline
65|23|-4|6|Foxtrott Zero: You're clear to land|newline
...

Do you remember the line_separator and var_separator nodes in our .xml? As you can see they do exactly what they’re expected to: they separate each piece of incoming data. Form left to right that is:
Speed, Altitude, Roll, Pitch, Messages (e.g. from tower). We’ll fetch the messages, because later we’ll use some simple text-2-speech mechanism. Roll and pitch values can be below zero, speed and altitude shouldn’t…
This is just a quick notation to give you an idea of how the string looks like and I’ll have to double-check it sometime to verify everything is in the right place. Later we’ll look at how our program handles the incoming socket stream.

in the next few steps…
we’ll create a new C#- Project that itself is divided (by threads) in two parts: The main application and a server. For a first impression, here’s the class diagram:

As you can see, this is not a 100% UML compatible diagram, but it should give you the idea how the program works and what classes will come around our way, while we’re creating it. Again it’s time for a small remark: The following code snippets are taken from the program I coded without doing any rework on it. It may contain some errors and also may not be serious well designed software at all. In other (perl) words: The program does what it’s supposed to.

fire up visual studio and…
yes, you guessed it: Start a new project. I used VS2005 for the whole stuff. Alternatively you can also use the free VS-Express Edition or SharpDevelop, but due to the fact that you’re reading this I’m sure you’re aware of your options.
One last remark: I’m running Vista on my Mac-Book in a virtual machine (to name it: Parallels). For this reason the project was developed on .NET 3.0, but I’m only using .NET 2.0 features – this should be no problem ’cause Microsoft didn’t change a single line of .NET 2.0 code in today’s shipped 3.0 version. Enough chitchat, let’s start:

shiftctrln.png

starts a new project. For the project type we’ll choose Windows Application, so that VS creates the basic classes for us. Name the project and hit the ok button to create it.

Hint: While we only want to develop a desktop application without a console output, during coding it may be helpful to have such an output. So point your mouse to Project -> Properties, change Output type to Console Application and we can access the console easily with a System.Console.WriteLine(“Something good happened…”);

enableconsole.png

Creating a server
Initially, let’s clarify something: While I’m talking about a server I don’t mean some full-featured whatever server. In this’ tutorials context the server is simply a class that opends a port on a chooseable IP-Address, nothing more…I named it server ’cause it sounds a lot better! Let’s get started:

additem.png

Right click in your solution-explorer and add a new item.

addnewclass.png

Select class and name it.

We only need one server here, so we’ll make it a singleton. In C# that’s done by a private constructor and a public getInstance()– method. Additionally there’s a static private property of our class-type. For this reason, it’s not possible to instantiate more than one object of our class. I kept the singleton pattern simple here, there are more considerations to take but for our application it’s alright.

The _uniqueServer property is our static property that hold’s the one and only instance:

zz23e252c4.png

In the private constructor nothing happends but calling a private method which look’s up the IP’s available on this machine and stores it in an ArrayList. Note the code at the end of this tutorial for further info.

This method makes the singelton possible: Either it creates a new instance of our class or it returns the existing one. Note that it has to be static and public.

Since we let run this server as a thread, we create a run()– method:

zz4e05e617.png

We’re using a try/catch block, because some things can go wrong here. The helper object does nothing more than writing data to the DataObject. While we’re using threads here, it locks the DataObject when writing to it – safety first! As you can see in the code above there’s a while loop – while the socket is connected it runs and runs and…therefore we’ll let this thing run in a thread:

threadstart.png

That’s all for the server. Let’s look how this all begins…

Advertisements

%d bloggers like this: