First complete implementation of Connection class
g0dil [Mon, 6 Nov 2006 22:07:28 +0000 (22:07 +0000)]
.gitignore [new file with mode: 0644]
SConstruct [new file with mode: 0644]
Server/Connection.cc [new file with mode: 0644]
Server/Connection.cci [new file with mode: 0644]
Server/Connection.hh [new file with mode: 0644]
Server/Connection.test.cc [new file with mode: 0644]
Server/SConscript [new file with mode: 0644]
Server/main.test.cc [new file with mode: 0644]
main.cc [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..2755c62
--- /dev/null
@@ -0,0 +1,5 @@
+mediaserv
+libScheduler.a
+libSocket.a
+libUtils.a
+libServer.a
diff --git a/SConstruct b/SConstruct
new file mode 100644 (file)
index 0000000..b970018
--- /dev/null
@@ -0,0 +1,25 @@
+import sys, glob
+sys.path.append('satscons')
+import SatSCons
+
+###########################################################################
+
+SatSCons.UseBoost();
+SatSCons.UseSTLPort();
+SatSCons.UseDoxygen();
+env = SatSCons.MakeEnvironment();
+
+env.Append(
+   CPPPATH = [ '#' ],
+   LIBS = [ 'iberty' ]
+)
+
+Export('env')
+
+SConscript(glob.glob("*/SConscript"))
+
+SatSCons.StandardTargets(env)
+SatSCons.GlobalTargets(env)
+
+SatSCons.Binary(env, binary='mediaserv', sources=SatSCons.GlobSources(),
+               LIBS = [ 'Server', 'Scheduler', 'Socket', 'Utils' ])
diff --git a/Server/Connection.cc b/Server/Connection.cc
new file mode 100644 (file)
index 0000000..66f0501
--- /dev/null
@@ -0,0 +1,66 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Definition of non-inline non-template functions
+
+#include "Connection.hh"
+//#include "Connection.ih"
+
+// Custom includes
+
+//#include "Connection.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+prefix_ g0dil::mediaserv::Connection::Connection(int fileFd, unsigned bytesPerSecond,
+                                                 ClientHandle client, unsigned bufferMSecs)
+    : fileFd_(fileFd), bytesPerSecond_(bytesPerSecond), client_(client),
+      bufferMSecs_(bufferMSecs), bytesWritten_(0), start_(satcom::lib::now()),
+      bufferSize_(0), terminate_(false)
+{
+    registerCallback();
+    fillBuffer();
+}
+
+prefix_ void g0dil::mediaserv::Connection::fillBuffer()
+{
+    bufferSize_ = ::read(fileFd_,buffer_,packetSize);
+    if (bufferSize_ < packetSize)
+       terminate_ = true;
+    // TODO: Check error;
+}
+
+prefix_ void g0dil::mediaserv::Connection::callback(ClientHandle client,
+                                                    satcom::lib::Scheduler::EventId)
+{
+    unsigned target (targetBytes());
+    if (target - bytesWritten_ >= bufferSize_) {
+       if (client_.write(buffer_,bufferSize_) < bufferSize_)
+           // TODO: Destroy client handle ..
+           ;
+       bytesWritten_ += bufferSize_;
+       if (terminate_) {
+           unregisterCallback();
+           // TODO: Destroy client handle
+       } else
+           fillBuffer();
+    } else {
+       unregisterCallback();
+       registerTimeout(((bufferSize_-(target-bytesWritten_))*1000)/bytesPerSecond_);
+    }
+}
+
+prefix_ void g0dil::mediaserv::Connection::timeout()
+{
+    registerCallback();
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "Connection.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Server/Connection.cci b/Server/Connection.cci
new file mode 100644 (file)
index 0000000..c09e778
--- /dev/null
@@ -0,0 +1,40 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Definition of inline non-template functions
+
+// Custom includes
+#include "Utils/membind.hh"
+
+#define prefix_ inline
+///////////////////////////////cci.p///////////////////////////////////////
+
+prefix_ void g0dil::mediaserv::Connection::registerCallback()
+{
+    satcom::lib::Scheduler::instance().add(client_,satcom::lib::membind(&Connection::callback,this),
+                                          satcom::lib::Scheduler::EV_WRITE);
+}
+
+prefix_ void g0dil::mediaserv::Connection::unregisterCallback()
+{
+    satcom::lib::Scheduler::instance().remove(client_,satcom::lib::Scheduler::EV_WRITE);
+}
+
+prefix_ void g0dil::mediaserv::Connection::registerTimeout(unsigned timeout)
+{
+    satcom::lib::Scheduler::instance().timeout(timeout,satcom::lib::membind(&Connection::timeout,this));
+}
+
+prefix_ unsigned g0dil::mediaserv::Connection::targetBytes()
+{
+    return ((bufferMSecs_ + ((satcom::lib::now()-start_)/1000u))*bytesPerSecond_)/1000u;
+}
+
+///////////////////////////////cci.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Server/Connection.hh b/Server/Connection.hh
new file mode 100644 (file)
index 0000000..0a40fa3
--- /dev/null
@@ -0,0 +1,84 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+#ifndef HH_Connection_
+#define HH_Connection_ 1
+
+// Custom includes
+#include <boost/utility.hpp>
+#include "Utils/MicroTime.hh"
+#include "Scheduler/Scheduler.hh"
+#include "Socket/ClientSocketHandle.hh"
+#include "Socket/CommunicationPolicy.hh"
+#include "Socket/FramingPolicy.hh"
+#include "Socket/BufferingPolicy.hh"
+#include "Socket/ReadWritePolicy.hh"
+
+//#include "Connection.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace g0dil {
+namespace mediaserv {
+
+    class Connection : boost::noncopyable
+    {
+    public:
+        ///////////////////////////////////////////////////////////////////////////
+        // Types
+
+       typedef satcom::lib::ClientSocketHandle<
+           satcom::lib::MakeSocketPolicy<satcom::lib::ConnectedCommunicationPolicy,
+                                         satcom::lib::StreamFramingPolicy,
+                                         satcom::lib::WriteablePolicy,
+                                         satcom::lib::SocketBufferingPolicy>::policy> ClientHandle;
+
+       static const unsigned packetSize = 1400;
+
+        ///////////////////////////////////////////////////////////////////////////
+        ///\name Structors and default members
+        ///@{
+
+        Connection(int fileFd, unsigned bytesPerSecond, ClientHandle client,
+                  unsigned bufferMSecs);
+
+        ///@}
+
+    protected:
+
+    private:
+       void registerCallback();
+       void unregisterCallback();
+       void registerTimeout(unsigned timeout);
+       unsigned targetBytes();
+       void fillBuffer();
+       
+       void callback(ClientHandle client, satcom::lib::Scheduler::EventId);
+       void timeout();
+       
+
+       int fileFd_;
+       unsigned bytesPerSecond_;
+       ClientHandle client_;
+       unsigned bufferMSecs_;
+
+       unsigned bytesWritten_;
+       satcom::lib::MicroTime start_;
+       unsigned bufferSize_;
+       char buffer_[packetSize];
+       bool terminate_;
+    };
+
+}}
+
+///////////////////////////////hh.e////////////////////////////////////////
+#include "Connection.cci"
+//#include "Connection.ct"
+//#include "Connection.cti"
+//#include "Connection.mpp"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Server/Connection.test.cc b/Server/Connection.test.cc
new file mode 100644 (file)
index 0000000..76fcb4f
--- /dev/null
@@ -0,0 +1,28 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Unit tests
+
+//#include "Connection.test.hh"
+//#include "Connection.test.ih"
+
+// Custom includes
+#include "Connection.hh"
+
+#include <boost/test/auto_unit_test.hpp>
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+BOOST_AUTO_UNIT_TEST(connection)
+{}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/Server/SConscript b/Server/SConscript
new file mode 100644 (file)
index 0000000..88ce6da
--- /dev/null
@@ -0,0 +1,9 @@
+Import('env')
+import SatSCons
+
+###########################################################################
+
+SatSCons.StandardTargets(env)
+
+SatSCons.Lib(env, library = 'Server', sources = SatSCons.GlobSources(),
+             LIBS = [ 'Socket', 'Scheduler', 'Utils' ])
diff --git a/Server/main.test.cc b/Server/main.test.cc
new file mode 100644 (file)
index 0000000..d2c8c5a
--- /dev/null
@@ -0,0 +1,27 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Unit tests
+
+//#include "main.test.hh"
+//#include "main.test.ih"
+
+// Custom includes
+
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// End:
diff --git a/main.cc b/main.cc
new file mode 100644 (file)
index 0000000..9922bce
--- /dev/null
+++ b/main.cc
@@ -0,0 +1,28 @@
+// $Id$
+//
+// Copyright (C) 2006 
+
+// Definition of non-inline non-template functions
+
+//#include "main.hh"
+//#include "main.ih"
+
+// Custom includes
+
+//#include "main.mpp"
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+int main(int argc, char** argv)
+{
+    return 0;
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+//#include "main.mpp"
+
+\f
+// Local Variables:
+// mode: c++
+// End: