Index: /trunk/FACT++/src/fsc.cc
===================================================================
--- /trunk/FACT++/src/fsc.cc	(revision 11033)
+++ /trunk/FACT++/src/fsc.cc	(revision 11033)
@@ -0,0 +1,304 @@
+#include <iostream>
+#include <string>
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/asio/deadline_timer.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+using boost::lexical_cast;
+
+#include "Time.h"
+#include "Converter.h"
+
+#include "HeadersFTM.h"
+
+using namespace std;
+using namespace FTM;
+
+namespace ba    = boost::asio;
+namespace bs    = boost::system;
+namespace dummy = ba::placeholders;
+
+using boost::lexical_cast;
+using ba::ip::tcp;
+
+// ------------------------------------------------------------------------
+
+
+// ------------------------------------------------------------------------
+
+class tcp_connection : public ba::ip::tcp::socket, public boost::enable_shared_from_this<tcp_connection>
+{
+private:
+
+    /*
+    void AsyncRead(ba::mutable_buffers_1 buffers)
+    {
+        ba::async_read(*this, buffers,
+                       boost::bind(&tcp_connection::HandleReceivedData, shared_from_this(),
+                                   dummy::error, dummy::bytes_transferred));
+    }*/
+
+    void AsyncWrite(const ba::const_buffers_1 &buffers)
+    {
+        ba::async_write(*this, buffers,
+                        boost::bind(&tcp_connection::HandleSentData, shared_from_this(),
+                                    dummy::error, dummy::bytes_transferred));
+    }
+    void AsyncWait(ba::deadline_timer &timer, int seconds,
+                               void (tcp_connection::*handler)(const bs::error_code&))// const
+    {
+        timer.expires_from_now(boost::posix_time::seconds(seconds));
+        timer.async_wait(boost::bind(handler, shared_from_this(), dummy::error));
+    }
+
+    ba::deadline_timer fTriggerSendData;
+
+    // The constructor is prvate to force the obtained pointer to be shared
+    tcp_connection(ba::io_service& ioservice) : ba::ip::tcp::socket(ioservice),
+        fTriggerSendData(ioservice)
+    {
+    }
+
+    // Callback when writing was successfull or failed
+    void HandleSentData(const boost::system::error_code& error, size_t bytes_transferred)
+    {
+        cout << "Data sent: (transmitted=" << bytes_transferred << ") rc=" << error.message() << " (" << error << ")" << endl;
+    }
+
+    stringstream fBuffer;
+
+    void SendData()
+    {
+        fBuffer.str("");
+        fBuffer <<
+            "status: 00000538 \n"
+            "time_s: 764.755 \n"
+            "VOLTAGES \n"
+            " \n"
+            "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111  00001111 \n"
+            "  done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111  00001111 \n"
+            "values:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 \n"
+            "RESISTANCES \n"
+            " \n"
+            "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
+            "  done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
+            "values: \n"
+            "1000.16 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "1197.07 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "558.59 677.92 817.26 989.39 1200.35 1503.06 1799.90 2204.18 \n"
+            "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+            "end.\n";
+
+        AsyncWrite(ba::buffer(ba::const_buffer(fBuffer.str().c_str(), fBuffer.str().length())));
+    }
+
+    void TriggerSendData(const boost::system::error_code &ec)
+    {
+        if (!is_open())
+        {
+            // For example: Here we could schedule a new accept if we
+            // would not want to allow two connections at the same time.
+            return;
+        }
+
+        if (ec==ba::error::basic_errors::operation_aborted)
+            return;
+
+        // Check whether the deadline has passed. We compare the deadline
+        // against the current time since a new asynchronous operation
+        // may have moved the deadline before this actor had a chance
+        // to run.
+        if (fTriggerSendData.expires_at() > ba::deadline_timer::traits_type::now())
+            return;
+
+        // The deadline has passed.
+        SendData();
+
+        AsyncWait(fTriggerSendData, 1, &tcp_connection::TriggerSendData);
+    }
+
+
+public:
+    typedef boost::shared_ptr<tcp_connection> shared_ptr;
+
+    static shared_ptr create(ba::io_service& io_service)
+    {
+        return shared_ptr(new tcp_connection(io_service));
+    }
+
+    void start()
+    {
+        AsyncWait(fTriggerSendData, 1, &tcp_connection::TriggerSendData);
+    }
+};
+
+
+class tcp_server : public tcp::acceptor
+{
+public:
+    tcp_server(ba::io_service& ioservice, int port) :
+        tcp::acceptor(ioservice, tcp::endpoint(tcp::v4(), port))
+
+    {
+        // We could start listening for more than one connection
+        // here, but since there is only one handler executed each time
+        // it would not make sense. Before one handle_accept is not
+        // finished no new handle_accept will be called.
+        // Workround: Start a new thread in handle_accept
+        start_accept();
+    }
+
+private:
+    void start_accept()
+    {
+        cout << "Start accept..." << flush;
+        tcp_connection::shared_ptr new_connection = tcp_connection::create(/*acceptor_.*/io_service());
+
+        // This will accept a connection without blocking
+        async_accept(*new_connection,
+                     boost::bind(&tcp_server::handle_accept,
+                                 this,
+                                 new_connection,
+                                 ba::placeholders::error));
+
+        cout << "start-done." << endl;
+    }
+
+    void handle_accept(tcp_connection::shared_ptr new_connection, const boost::system::error_code& error)
+    {
+        // The connection has been accepted and is now ready to use
+
+        // not installing a new handler will stop run()
+        cout << "Handle accept..." << flush;
+        if (!error)
+        {
+            new_connection->start();
+
+            // The is now an open connection/server (tcp_connection)
+            // we immediatly schedule another connection
+            // This allowed two client-connection at the same time
+            start_accept();
+        }
+        cout << "handle-done." << endl;
+    }
+};
+
+int main(int argc, const char **argv)
+{
+    //try
+    {
+        ba::io_service io_service;
+
+        int Port = argc==2 ? lexical_cast<int>(argv[1]) : 5000;
+
+        tcp_server server(io_service, Port);
+        //  ba::add_service(io_service, &server);
+        //  server.add_service(...);
+        //cout << "Run..." << flush;
+
+        // Calling run() from a single thread ensures no concurrent access
+        // of the handler which are called!!!
+        io_service.run();
+
+        //cout << "end." << endl;
+    }
+    /*catch (std::exception& e)
+    {
+        std::cerr << e.what() << std::endl;
+    }*/
+
+    return 0;
+}
+/*  ====================== Buffers ===========================
+
+char d1[128]; ba::buffer(d1));
+std::vector<char> d2(128); ba::buffer(d2);
+boost::array<char, 128> d3; by::buffer(d3);
+
+// --------------------------------
+char d1[128];
+std::vector<char> d2(128);
+boost::array<char, 128> d3;
+
+boost::array<mutable_buffer, 3> bufs1 = {
+   ba::buffer(d1),
+   ba::buffer(d2),
+   ba::buffer(d3) };
+sock.read(bufs1);
+
+std::vector<const_buffer> bufs2;
+bufs2.push_back(boost::asio::buffer(d1));
+bufs2.push_back(boost::asio::buffer(d2));
+bufs2.push_back(boost::asio::buffer(d3));
+sock.write(bufs2);
+
+
+// ======================= Read functions =========================
+
+ba::async_read_until --> delimiter
+
+streambuf buf; // Ensure validity until handler!
+by::async_read(s, buf, ....);
+
+ba::async_read(s, ba:buffer(data, size), handler);
+ // Single buffer
+ boost::asio::async_read(s,
+                         ba::buffer(data, size),
+ compl-func -->          ba::transfer_at_least(32),
+                         handler);
+
+ // Multiple buffers
+boost::asio::async_read(s, buffers,
+ compl-func -->         boost::asio::transfer_all(),
+                        handler);
+                        */
+
+// ================= Others ===============================
+
+        /*
+        strand   Provides serialised handler execution.
+        work     Class to inform the io_service when it has work to do.
+
+
+io_service::
+dispatch   Request the io_service to invoke the given handler.
+poll       Run the io_service's event processing loop to execute ready
+           handlers.
+poll_one   Run the io_service's event processing loop to execute one ready
+           handler.
+post       Request the io_service to invoke the given handler and return
+           immediately.
+reset      Reset the io_service in preparation for a subsequent run()
+           invocation.
+run        Run the io_service's event processing loop.
+run_one    Run the io_service's event processing loop to execute at most
+           one handler.
+stop       Stop the io_service's event processing loop.
+wrap       Create a new handler that automatically dispatches the wrapped
+           handler on the io_service.
+
+strand::         The io_service::strand class provides the ability to
+                 post and dispatch handlers with the guarantee that none
+                 of those handlers will execute concurrently.
+
+dispatch         Request the strand to invoke the given handler.
+get_io_service   Get the io_service associated with the strand.
+post             Request the strand to invoke the given handler and return
+                 immediately.
+wrap             Create a new handler that automatically dispatches the
+                 wrapped handler on the strand.
+
+work::           The work class is used to inform the io_service when
+                 work starts and finishes. This ensures that the io_service's run() function will not exit while work is underway, and that it does exit when there is no unfinished work remaining.
+get_io_service   Get the io_service associated with the work.
+work             Constructor notifies the io_service that work is starting.
+
+*/
+
+
Index: /trunk/FACT++/src/fscctrl.cc
===================================================================
--- /trunk/FACT++/src/fscctrl.cc	(revision 11033)
+++ /trunk/FACT++/src/fscctrl.cc	(revision 11033)
@@ -0,0 +1,595 @@
+#include <boost/bind.hpp>
+#include <boost/array.hpp>
+#if BOOST_VERSION < 104400
+#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
+#undef BOOST_HAS_RVALUE_REFS
+#endif
+#endif
+#include <boost/thread.hpp>
+#include <boost/asio/error.hpp>
+#include <boost/asio/deadline_timer.hpp>
+
+#include "Dim.h"
+#include "Event.h"
+#include "Shell.h"
+#include "StateMachineDim.h"
+#include "Connection.h"
+#include "Configuration.h"
+#include "Console.h"
+#include "Converter.h"
+
+#include "tools.h"
+
+#include "LocalControl.h"
+
+
+namespace ba    = boost::asio;
+namespace bs    = boost::system;
+namespace dummy = ba::placeholders;
+
+using namespace std;
+
+// ------------------------------------------------------------------------
+
+class ConnectionFSC : public Connection
+{
+    boost::asio::streambuf fBuffer;
+
+    bool fIsVerbose;
+
+protected:
+/*
+    virtual void UpdateError()
+    {
+        if (!fIsVerbose)
+            return;
+
+        Out() << endl << kRed << "Error received:" << endl;
+        Out() << fError;
+        if (fIsHexOutput)
+            Out() << Converter::GetHex<uint16_t>(fError, 16) << endl;
+    }
+*/
+
+private:
+    void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int /*type*/)
+    {
+        // Do not schedule a new read if the connection failed.
+        if (bytes_received==0 || err)
+        {
+            if (err==ba::error::eof)
+                Warn("Connection closed by remote host (FTM).");
+
+            // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
+            // 125: Operation canceled
+            if (err && err!=ba::error::eof &&                     // Connection closed by remote host
+                err!=ba::error::basic_errors::not_connected &&    // Connection closed by remote host
+                err!=ba::error::basic_errors::operation_aborted)  // Connection closed by us
+            {
+                ostringstream str;
+                str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
+                Error(str);
+            }
+            PostClose(err!=ba::error::basic_errors::operation_aborted);
+            return;
+        }
+
+        istream is(&fBuffer);
+
+        string buffer;
+        while (getline(is, buffer, '\n'))
+        {
+            cout << "Line: " << buffer << endl;
+        }
+        cout << endl;
+/*
+"status: 00000538 \n"
+"time_s: 764.755 \n"
+"VOLTAGES \n"
+" \n"
+"enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111  00001111 \n"
+"  done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111  00001111 \n"
+"values:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 \n"
+"RESISTANCES \n"
+" \n"
+"enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
+"  done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
+"values: \n"
+"1000.16 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"1197.07 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+" 558.59  677.92  817.26  989.39 1200.35 1503.06 1799.90 2204.18 \n"
+"3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
+"end.\n";
+
+*/
+        StartRead();
+
+
+    }
+
+    void StartRead()
+    {
+        ba::async_read_until(*this, fBuffer, "end.\n",
+                             boost::bind(&ConnectionFSC::HandleReceivedData, this,
+                                         dummy::error, dummy::bytes_transferred, 0));
+    }
+
+    // This is called when a connection was established
+    void ConnectionEstablished()
+    {
+        PostMessage("m", 1);
+
+        fBuffer.prepare(100000);
+        StartRead();
+    }
+
+/*
+    void HandleReadTimeout(const bs::error_code &error)
+    {
+        if (error==ba::error::basic_errors::operation_aborted)
+            return;
+
+        if (error)
+        {
+            ostringstream str;
+            str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
+            Error(str);
+
+            PostClose();
+            return;
+
+        }
+
+        if (!is_open())
+        {
+            // For example: Here we could schedule a new accept if we
+            // would not want to allow two connections at the same time.
+            return;
+        }
+
+        // Check whether the deadline has passed. We compare the deadline
+        // against the current time since a new asynchronous operation
+        // may have moved the deadline before this actor had a chance
+        // to run.
+        if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
+            return;
+
+        Error("Timeout reading data from "+URL());
+
+        PostClose();
+    }
+*/
+
+public:
+    ConnectionFSC(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
+        fIsVerbose(true)
+    {
+        SetLogStream(&imp);
+    }
+
+    void SetVerbose(bool b)
+    {
+        fIsVerbose = b;
+    }
+};
+
+//const uint16_t ConnectionFSC::kMaxAddr = 0xfff;
+
+// ------------------------------------------------------------------------
+
+#include "DimDescriptionService.h"
+
+class ConnectionDimFSC : public ConnectionFSC
+{
+private:
+
+    DimDescribedService fDimPassport;
+
+    template<class T>
+        void Update(DimDescribedService &svc, const T &data) const
+    {
+        //cout << "Update: " << svc.getName() << " (" << sizeof(T) << ")" << endl;
+        svc.setData(const_cast<T*>(&data), sizeof(T));
+        svc.updateService();
+    }
+
+    /*
+    void UpdateFirstHeader()
+    {
+        ConnectionFSC::UpdateFirstHeader();
+
+        const FTM::DimPassport data(fHeader);
+        Update(fDimPassport, data);
+    }*/
+
+public:
+    ConnectionDimFSC(ba::io_service& ioservice, MessageImp &imp) :
+        ConnectionFSC(ioservice, imp),
+        fDimPassport      ("FSC_CONTROL/PASSPORT",        "I:1", "")
+
+//        DimService Update     (SERVER_NAME"/Time",        "F", Val+TIME_OFF, sizeof(float));
+//        DimService Temperature(SERVER_NAME"/Temperature", "F", Val+TEMP_OFF, sizeof(float)*TEMP_NUM);
+//        DimService Humidity   (SERVER_NAME"/Humindity",   "F", Val+HUMI_OFF, sizeof(float)*HUMI_NUM);
+//        DimService Voltage    (SERVER_NAME"/Voltage",     "F", Val+VOLT_OFF, sizeof(float)*VOLT_NUM);
+//        DimService Current    (SERVER_NAME"/Current",     "F", Val+CURR_OFF, sizeof(float)*CURR_NUM);
+    {
+    }
+
+    // A B [C] [D] E [F] G H [I] J K [L] M N O P Q R [S] T U V W [X] Y Z
+};
+
+// ------------------------------------------------------------------------
+
+template <class T, class S>
+class StateMachineFSC : public T, public ba::io_service, public ba::io_service::work
+{
+    int Wrap(boost::function<void()> f)
+    {
+        f();
+        return T::GetCurrentState();
+    }
+
+    boost::function<int(const EventImp &)> Wrapper(boost::function<void()> func)
+    {
+        return boost::bind(&StateMachineFSC::Wrap, this, func);
+    }
+
+private:
+    S fFSC;
+
+    enum states_t
+    {
+        kStateDisconnected = 1,
+        kStateConnected    = 2,
+
+        kCmdTest
+    };
+
+    int Disconnect()
+    {
+        // Close all connections
+        fFSC.PostClose(false);
+
+        /*
+         // Now wait until all connection have been closed and
+         // all pending handlers have been processed
+         poll();
+         */
+
+        return T::GetCurrentState();
+    }
+
+    int Reconnect(const EventImp &evt)
+    {
+        // Close all connections to supress the warning in SetEndpoint
+        fFSC.PostClose(false);
+
+        // Now wait until all connection have been closed and
+        // all pending handlers have been processed
+        poll();
+
+        if (evt.GetBool())
+            fFSC.SetEndpoint(evt.GetString());
+
+        // Now we can reopen the connection
+        fFSC.PostClose(true);
+
+        return T::GetCurrentState();
+    }
+
+    int Execute()
+    {
+        // Dispatch (execute) at most one handler from the queue. In contrary
+        // to run_one(), it doesn't wait until a handler is available
+        // which can be dispatched, so poll_one() might return with 0
+        // handlers dispatched. The handlers are always dispatched/executed
+        // synchronously, i.e. within the call to poll_one()
+        poll_one();
+
+        return fFSC.IsConnected() ? kStateConnected : kStateDisconnected;
+    }
+
+    bool CheckEventSize(size_t has, const char *name, size_t size)
+    {
+        if (has==size)
+            return true;
+
+        ostringstream msg;
+        msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
+        T::Fatal(msg);
+        return false;
+    }
+
+    int SetVerbosity(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
+            return T::kSM_FatalError;
+
+        fFSC.SetVerbose(evt.GetBool());
+
+        return T::GetCurrentState();
+    }
+
+public:
+    StateMachineFSC(ostream &out=cout) :
+        T(out, "FSC_CONTROL"), ba::io_service::work(static_cast<ba::io_service&>(*this)),
+        fFSC(*this, *this)
+    {
+        // ba::io_service::work is a kind of keep_alive for the loop.
+        // It prevents the io_service to go to stopped state, which
+        // would prevent any consecutive calls to run()
+        // or poll() to do nothing. reset() could also revoke to the
+        // previous state but this might introduce some overhead of
+        // deletion and creation of threads and more.
+
+        // State names
+        AddStateName(kStateDisconnected, "Disconnected",
+                     "FSC board not connected via ethernet.");
+
+        AddStateName(kStateConnected, "Connected",
+                     "Ethernet connection to FSC established.");
+
+        // Verbosity commands
+        T::AddEvent("SET_VERBOSE", "B")
+            (boost::bind(&StateMachineFSC::SetVerbosity, this, _1))
+            ("set verbosity state"
+             "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
+
+        // Conenction commands
+        AddEvent("DISCONNECT", kStateConnected)
+            (boost::bind(&StateMachineFSC::Disconnect, this))
+            ("disconnect from ethernet");
+
+        AddEvent("RECONNECT", "O", kStateDisconnected, kStateConnected)
+            (boost::bind(&StateMachineFSC::Reconnect, this, _1))
+            ("(Re)connect ethernet connection to FTM, a new address can be given"
+             "|[host][string]:new ethernet address in the form <host:port>");
+
+        fFSC.StartConnect();
+    }
+
+    void SetEndpoint(const string &url)
+    {
+        fFSC.SetEndpoint(url);
+    }
+
+    bool SetConfiguration(const Configuration &conf)
+    {
+        SetEndpoint(conf.Get<string>("addr"));
+
+        fFSC.SetVerbose(!conf.Get<bool>("quiet"));
+
+        return true;
+    }
+};
+
+// ------------------------------------------------------------------------
+
+void RunThread(StateMachineImp *io_service)
+{
+    // This is necessary so that the StateMachien Thread can signal the
+    // Readline to exit
+    io_service->Run();
+    Readline::Stop();
+}
+
+template<class S, class T>
+int RunDim(Configuration &conf)
+{
+    WindowLog wout;
+
+    /*
+    static Test shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
+
+    WindowLog &win  = shell.GetStreamIn();
+    WindowLog &wout = shell.GetStreamOut();
+    */
+
+    ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
+
+
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
+
+    // Start io_service.Run to use the StateMachineImp::Run() loop
+    // Start io_service.run to only use the commandHandler command detaching
+    StateMachineFSC<S, T> io_service(wout);
+    if (!io_service.SetConfiguration(conf))
+        return -1;
+
+    io_service.Run();
+
+    /*
+    shell.SetReceiver(io_service);
+
+    boost::thread t(boost::bind(RunThread, &io_service));
+    // boost::thread t(boost::bind(&StateMachineFSC<S>::Run, &io_service));
+
+    shell.Run();                 // Run the shell
+    io_service.Stop();           // Signal Loop-thread to stop
+    // io_service.Close();       // Obsolete, done by the destructor
+
+    // Wait until the StateMachine has finished its thread
+    // before returning and destroying the dim objects which might
+    // still be in use.
+    t.join();
+    */
+
+    return 0;
+}
+
+template<class T, class S, class R>
+int RunShell(Configuration &conf)
+{
+    static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
+
+    WindowLog &win  = shell.GetStreamIn();
+    WindowLog &wout = shell.GetStreamOut();
+
+    if (conf.Has("log"))
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
+
+    StateMachineFSC<S, R> io_service(wout);
+    if (!io_service.SetConfiguration(conf))
+        return -1;
+
+    shell.SetReceiver(io_service);
+
+    boost::thread t(boost::bind(RunThread, &io_service));
+    // boost::thread t(boost::bind(&StateMachineFSC<S>::Run, &io_service));
+
+    shell.Run();                 // Run the shell
+    io_service.Stop();           // Signal Loop-thread to stop
+    // io_service.Close();       // Obsolete, done by the destructor
+
+    // Wait until the StateMachine has finished its thread
+    // before returning and destroying the dim objects which might
+    // still be in use.
+    t.join();
+
+    return 0;
+}
+
+void SetupConfiguration(Configuration &conf)
+{
+    const string n = conf.GetName()+".log";
+
+    po::options_description config("Program options");
+    config.add_options()
+        ("dns",       var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
+        ("log,l",     var<string>(n), "Write log-file")
+        ("no-dim,d",  po_bool(),      "Disable dim services")
+        ("console,c", var<int>(),     "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
+        ;
+
+    po::options_description control("FTM control options");
+    control.add_options()
+        ("addr,a",        var<string>("localhost:5000"),  "Network address of FTM")
+        ("quiet,q",       po_bool(),  "Disable printing contents of all received messages (except dynamic data) in clear text.")
+        ;
+
+    conf.AddEnv("dns", "DIM_DNS_NODE");
+
+    conf.AddOptions(config);
+    conf.AddOptions(control);
+}
+
+/*
+ Extract usage clause(s) [if any] for SYNOPSIS.
+ Translators: "Usage" and "or" here are patterns (regular expressions) which
+ are used to match the usage synopsis in program output.  An example from cp
+ (GNU coreutils) which contains both strings:
+  Usage: cp [OPTION]... [-T] SOURCE DEST
+    or:  cp [OPTION]... SOURCE... DIRECTORY
+    or:  cp [OPTION]... -t DIRECTORY SOURCE...
+ */
+void PrintUsage()
+{
+    cout <<
+        "The ftmctrl controls the FTM (FACT Trigger Master) board.\n"
+        "\n"
+        "The default is that the program is started without user intercation. "
+        "All actions are supposed to arrive as DimCommands. Using the -c "
+        "option, a local shell can be initialized. With h or help a short "
+        "help message about the usuage can be brought to the screen.\n"
+        "\n"
+        "Usage: fscctrl [-c type] [OPTIONS]\n"
+        "  or:  fscctrl [OPTIONS]\n";
+    cout << endl;
+}
+
+void PrintHelp()
+{
+    /* Additional help text which is printed after the configuration
+     options goes here */
+
+    /*
+     cout << "bla bla bla" << endl << endl;
+     cout << endl;
+     cout << "Environment:" << endl;
+     cout << "environment" << endl;
+     cout << endl;
+     cout << "Examples:" << endl;
+     cout << "test exam" << endl;
+     cout << endl;
+     cout << "Files:" << endl;
+     cout << "files" << endl;
+     cout << endl;
+     */
+}
+
+int main(int argc, const char* argv[])
+{
+    Configuration conf(argv[0]);
+    conf.SetPrintUsage(PrintUsage);
+    SetupConfiguration(conf);
+
+    po::variables_map vm;
+    try
+    {
+        vm = conf.Parse(argc, argv);
+    }
+#if BOOST_VERSION > 104000
+    catch (po::multiple_occurrences &e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
+        return -1;
+    }
+#endif
+    catch (exception& e)
+    {
+        cerr << "Program options invalid due to: " << e.what() << endl;
+        return -1;
+    }
+
+    if (conf.HasVersion() || conf.HasPrint())
+        return -1;
+
+    if (conf.HasHelp())
+    {
+        PrintHelp();
+        return -1;
+    }
+
+    Dim::Setup(conf.Get<string>("dns"));
+
+    //try
+    {
+        // No console access at all
+        if (!conf.Has("console"))
+        {
+            if (conf.Get<bool>("no-dim"))
+                return RunDim<StateMachine, ConnectionFSC>(conf);
+            else
+                return RunDim<StateMachineDim, ConnectionDimFSC>(conf);
+        }
+        // Cosole access w/ and w/o Dim
+        if (conf.Get<bool>("no-dim"))
+        {
+            if (conf.Get<int>("console")==0)
+                return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
+            else
+                return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
+        }
+        else
+        {
+            if (conf.Get<int>("console")==0)
+                return RunShell<LocalShell, StateMachineDim, ConnectionDimFSC>(conf);
+            else
+                return RunShell<LocalConsole, StateMachineDim, ConnectionDimFSC>(conf);
+        }
+    }
+    /*catch (std::exception& e)
+    {
+        cerr << "Exception: " << e.what() << endl;
+        return -1;
+    }*/
+
+    return 0;
+}
