Console: Implement ObjectDirectory proxy
[senf.git] / Console / Server.cc
index 5790d83..4e37e68 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/iostreams/device/file_descriptor.hpp>
 #include <boost/iostreams/stream.hpp>
+#include <boost/bind.hpp>
 #include "../Utils/senfassert.hh"
 #include "../Utils/membind.hh"
 #include "../Utils/Logger/SenfLog.hh"
@@ -103,7 +104,7 @@ prefix_ void senf::console::Server::removeClient(Client & client)
 prefix_ senf::console::Client::Client(ClientHandle handle, std::string const & name)
     : handle_ (handle), name_ (name), out_(::dup(handle.fd()))
 {
-    out_ << name_ << "# " << std::flush;
+    showPrompt();
     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
                                         senf::membind(&Client::clientData, this) );
 }
@@ -125,32 +126,36 @@ prefix_ void senf::console::Client::clientData(ReadHelper<ClientHandle>::ptr hel
         return;
     }
 
-#   warning fix Client::clientData implementation
-    // Remove the 'dup' needed here so we don't close the same fd twice (see Client constructor)
-    // Make output non-blocking
-    // Don't register a new ReadHelper every round
-
     std::string data (tail_ + helper->data());
     tail_ = helper->tail();
     boost::trim(data); // Gets rid of superfluous  \r or \n characters
 
-    if (data == "exit") {
+    try {
+        if (! parser_.parse(data, boost::bind<void>(boost::ref(executor_), _1, boost::ref(out_))))
+            out_ << "syntax error" << std::endl;
+    }
+    catch (Executor::ExitException &) {
         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
         stopClient();
         return;
     }
-        
-    ParseCommandInfo command;
-    if (parser_.parseCommand(data, command))
-        executor_(command, out_);
-    else 
-        out_ << "syntax error" << std::endl;
-
-    out_ << name_ << "# " << std::flush;
+    catch (std::exception & ex) {
+        out_ << ex.what() << std::endl;
+    }
+    catch (...) {
+        out_ << "unidentified error (unknown exception thrown)" << std::endl;
+    }
+
+    showPrompt();
     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
                                         senf::membind(&Client::clientData, this) );
 }
 
+prefix_ void senf::console::Client::showPrompt()
+{
+    out_ << name_ << ":" << executor_.cwd().path() << "# " << std::flush;
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Server.mpp"