9.6: FgfsServer


#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using FgfsSharp;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

#endregion

namespace FgfsSharp
{
//The server should run in it's own thread and fetch continuosly Flightgear-data
//TODO: Documentation
class FgfsServer
{
#region Privates

private IPAddress _ip;
private int _port;
private FgfsDataHelper _helper;
private ArrayList _ipList;
private static FgfsServer _uniqueServer;

#endregion

#region Properties

public IPAddress Ip
{
get { return _ip; }
private set { _ip = value; }
}

public int Port
{
get { return _port; }
private set { _port = value; }
}

private FgfsDataHelper Helper
{
get { return _helper; }
set { _helper = value; }
}

public ArrayList IpList
{
get { return _ipList; }
private set { _ipList = value; ;}
}

#endregion

#region Constructor

private FgfsServer()
{
Console.WriteLine("Constructor: FgfsServer");
IpList = DeterminePossibleIPs();
}

#endregion

#region Public Methods

public static FgfsServer GetInstance()
{
if (_uniqueServer == null)
{
_uniqueServer = new FgfsServer();
}

return _uniqueServer;
}

//transforms string in ipaddress --
public void StartServer(string ip, int port, FgfsDataHelper helper)
{
IPAddress ipAddress = IPAddress.Parse(ip);
this.StartServer(ipAddress, port, helper);
}

//starts the server @ ip:port
public void StartServer(IPAddress ip, int port, FgfsDataHelper helper)
{
this.Ip = ip;
this.Port = port;
this.Helper = helper;

//start a new thread on the run-method
Thread _serverThread = new Thread(new ThreadStart(Run));
_serverThread.Name = "ServerThread";
_serverThread.Priority = ThreadPriority.Normal;
_serverThread.Start();

Console.WriteLine("starting Server...");
}

#endregion

#region Private Methods

public static ArrayList DeterminePossibleIPs()
{
ArrayList aL = new ArrayList();
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress address in hostEntry.AddressList)
{
aL.Add(address.ToString());
}
aL.Add(IPAddress.Loopback.ToString());
return aL;
}

private void Run()
{
//create a socket, waiting for Flightgear to connect
TcpListener tcpListener = new TcpListener(this.Ip, this.Port);
tcpListener.Start();
Socket fgfsSocket = tcpListener.AcceptSocket();
Console.WriteLine("Client connected");

NetworkStream netStream = new NetworkStream(fgfsSocket);
StreamReader incomingStream = new StreamReader(netStream);

try
{
do
{
Helper.UpdateDataObject(incomingStream);
//Helper.PrintObject();
}
while (fgfsSocket.Connected);
}
catch (Exception e)
{
Console.WriteLine("Damn: " + e.Message);
}
finally
{
incomingStream.Close();
netStream.Close();
fgfsSocket.Close();
tcpListener.Stop();
}
}

#endregion

}
}

Advertisements

One Response to “9.6: FgfsServer”

  1. javad Sadi Says:

    hi
    may you send me your project source code?
    thank
    javad sadi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: