From: g0dil Date: Mon, 6 Nov 2006 22:07:28 +0000 (+0000) Subject: First complete implementation of Connection class X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=3c4b5e4e5f828331621e3c2d816a245cf731aa0f;p=mediaserv.git First complete implementation of Connection class --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2755c62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +mediaserv +libScheduler.a +libSocket.a +libUtils.a +libServer.a diff --git a/SConstruct b/SConstruct new file mode 100644 index 0000000..b970018 --- /dev/null +++ b/SConstruct @@ -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 index 0000000..66f0501 --- /dev/null +++ b/Server/Connection.cc @@ -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" + + +// Local Variables: +// mode: c++ +// End: diff --git a/Server/Connection.cci b/Server/Connection.cci new file mode 100644 index 0000000..c09e778 --- /dev/null +++ b/Server/Connection.cci @@ -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_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/Server/Connection.hh b/Server/Connection.hh new file mode 100644 index 0000000..0a40fa3 --- /dev/null +++ b/Server/Connection.hh @@ -0,0 +1,84 @@ +// $Id$ +// +// Copyright (C) 2006 + +#ifndef HH_Connection_ +#define HH_Connection_ 1 + +// Custom includes +#include +#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::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 + + +// Local Variables: +// mode: c++ +// End: diff --git a/Server/Connection.test.cc b/Server/Connection.test.cc new file mode 100644 index 0000000..76fcb4f --- /dev/null +++ b/Server/Connection.test.cc @@ -0,0 +1,28 @@ +// $Id$ +// +// Copyright (C) 2006 + +// Unit tests + +//#include "Connection.test.hh" +//#include "Connection.test.ih" + +// Custom includes +#include "Connection.hh" + +#include +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +BOOST_AUTO_UNIT_TEST(connection) +{} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/Server/SConscript b/Server/SConscript new file mode 100644 index 0000000..88ce6da --- /dev/null +++ b/Server/SConscript @@ -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 index 0000000..d2c8c5a --- /dev/null +++ b/Server/main.test.cc @@ -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 +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + + + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// End: diff --git a/main.cc b/main.cc new file mode 100644 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" + + +// Local Variables: +// mode: c++ +// End: