Revision: 40619
          http://brlcad.svn.sourceforge.net/brlcad/?rev=40619&view=rev
Author:   davidloman
Date:     2010-09-20 19:08:31 +0000 (Mon, 20 Sep 2010)

Log Message:
-----------
First round of fixes to the NetMsgFactory.  Converted it back to singleton as 
it is no longer necessary to have one NetMsgFactory per Portal.  Since libPkg 
only calls the callback fn when a full netMsg has arrived, there is no need for 
NetMsgFactory to have an internal buffer any longer.

Modified Paths:
--------------
    rt^3/trunk/include/NetMsgFactory.h
    rt^3/trunk/src/libNet/NetMsgFactory.cxx

Modified: rt^3/trunk/include/NetMsgFactory.h
===================================================================
--- rt^3/trunk/include/NetMsgFactory.h  2010-09-20 18:55:28 UTC (rev 40618)
+++ rt^3/trunk/include/NetMsgFactory.h  2010-09-20 19:08:31 UTC (rev 40619)
@@ -26,39 +26,25 @@
 #ifndef __NETMSGFACTORY_H__
 #define __NETMSGFACTORY_H__
 
+#include "brlcad/pkg.h"
 #include "libutility.h"
 #include "NetMsg.h"
 
+
 #include <QtCore/QByteArray>
-#include <QtCore/QQueue>
-#include <QtCore/QBuffer>
-#include <QtCore/QMutex>
+#include <QtCore/QString>
+#include <QtCore/QDataStream>
 
 class NetMsgFactory
 {
-
 public:
+  static NetMsgFactory* getInstance();
+  virtual  ~NetMsgFactory();
+  NetMsg* deserializeNetMsg(QByteArray& data, QString origin);
 
-       NetMsgFactory();
-       virtual ~NetMsgFactory();
-
-       bool addData(QByteArray& data);
-       NetMsg* makeMsg();
-       void printBufferStatus(bool extended);
-
-       void setPortalName(QString portalName);
-       QString getPortalName();
 private:
-       Logger* log;
-       QString portalName;
-
-       QMutex* bufferLock;
-       QBuffer* intBuffer;
-       quint64 limit;
-
-       void compactBuffer();
-       static NetMsg* buildMsgByType(quint32 type, QDataStream* qds,
-                       QString portalName);
+  NetMsgFactory();
+  static NetMsgFactory* pInstance;
 };
 
 #endif

Modified: rt^3/trunk/src/libNet/NetMsgFactory.cxx
===================================================================
--- rt^3/trunk/src/libNet/NetMsgFactory.cxx     2010-09-20 18:55:28 UTC (rev 
40618)
+++ rt^3/trunk/src/libNet/NetMsgFactory.cxx     2010-09-20 19:08:31 UTC (rev 
40619)
@@ -23,6 +23,7 @@
  *
  */
 #include "NetMsgTypes.h"
+#include "Portal.h"
 
 #include "NetMsgFactory.h"
 #include "GenericOneStringMsg.h"
@@ -37,213 +38,81 @@
 #include "SessionInfoMsg.h"
 #include "TypeOnlyMsg.h"
 
-#include <QtCore/QMutexLocker>
+NetMsgFactory* NetMsgFactory::pInstance = NULL;
 
-NetMsgFactory::NetMsgFactory()
+NetMsgFactory*
+NetMsgFactory::getInstance()
 {
-    this->log = Logger::getInstance();
-    this->portalName = "NotSetYet";
-
-    this->intBuffer = new QBuffer();
-    this->intBuffer->open(QIODevice::ReadWrite);
-    this->limit = 0;
-
-    this->bufferLock = new QMutex();
+  if (NetMsgFactory::pInstance == NULL) {
+    NetMsgFactory::pInstance = new NetMsgFactory();
+  }
+  return NetMsgFactory::pInstance;
 }
 
-NetMsgFactory::~NetMsgFactory()
+NetMsgFactory::NetMsgFactory()
 {
-    // this->intBuffer->close();
-    // delete this->intBuffer;
-    delete this->bufferLock ;
 }
 
-bool NetMsgFactory::addData(QByteArray& data)
+NetMsgFactory::~NetMsgFactory()
 {
-    QMutexLocker locker(this->bufferLock);
-
-    //Copy data into internal buffer
-    quint32 retVal = this->intBuffer->write(data);
-
-    if (retVal == -1) {
-       this->intBuffer->reset();
-       return false;
-    }
-
-    else {
-       this->limit += retVal;
-       return true;
-    }
 }
 
-
-NetMsg* NetMsgFactory::makeMsg()
+NetMsg*
+NetMsgFactory::deserializeNetMsg(QByteArray& data, QString origin)
 {
-    QMutexLocker(this->bufferLock);
+  QDataStream* qds = new QDataStream(data);
 
-    if (this->limit < 8) {
-       //dont have enough data in the buffer
-       //std::cout << "Factory failed: InitialSize < 8\n";
-       return false;
-    }
+  quint16 msgType = 0;
+  *qds >> msgType;
 
-    quint32 bufPos = this->intBuffer->pos();
+  //TODO Replace this with a map for registration scheme
+  switch (msgType)
+    {
+  case TEST_GENERIC_4BYTE_MSG:
+    return new GenericFourBytesMsg(qds, origin);
 
-    //First test to see if we have LEN data by PEEKing
-    this->intBuffer->reset();
-    QByteArray qbaLen = this->intBuffer->peek(8);
-    QDataStream qdsLen(qbaLen);
+  case TEST_GENERIC_2BYTE_MSG:
+    return new GenericTwoBytesMsg(qds, origin);
 
-    quint32 len;
-    qdsLen >> len;
+  case TEST_GENERIC_1BYTE_MSG:
+    return new GenericOneByteMsg(qds, origin);
 
-    quint32 msgType;
-    qdsLen >> msgType;
+  case TEST_GENERIC_MULTIBYTE_MSG:
+    return new GenericMultiByteMsg(qds, origin);
 
-    if (this->limit < (len + 4)) {
-       //dont have enough data in the buffer
-       //std::cout << "Factory failed: Size < LEN + 4 (" << (len + 4) << "\n";
-       return NULL;
-    }
+  case TEST_GENERIC_1STRING_MSG:
+    return new GenericOneStringMsg(qds, origin);
 
-    //reset
-    this->intBuffer->reset();
-
-    //Wrap up the buffer in a DataStream
-    QDataStream* qds = new QDataStream(this->intBuffer);
-    NetMsg* msg = this->buildMsgByType(msgType, qds, this->portalName);
-
-    if (msg == NULL) {
-       log->logINFO("NetMsgFactory","Factory failed: msgType lookup Failure:" 
+ QString::number(msgType));
-    } else {
-       this->compactBuffer();
+  case FAILURE:
+    return new GenericOneByteMsg(qds, origin);
+  case SUCCESS:
+    return new GenericOneByteMsg(qds, origin);
+  case REMGSHOSTNAMESET:
+    return new GenericOneStringMsg(qds, origin);
+  case DISCONNECTREQ:
+    return new TypeOnlyMsg(qds, origin);
+  case NEWHOSTONNET:
+    return new GenericOneStringMsg(qds, origin);
+    //    case FULLHOSTLISTREQ:
+    // return new NetMsg(qds, origin);
+    //    case FULLHOSTLIST:
+    // return new NetMsg(qds, origin);
+  case NEWSESSIONREQ:
+    return new NewSessionReqMsg(qds, origin);
+  case SESSIONINFO:
+    return new SessionInfoMsg(qds, origin);
+  case GEOMETRYREQ:
+    return new GeometryReqMsg(qds, origin);
+  case GEOMETRYMANIFEST:
+    return new GeometryManifestMsg(qds, origin);
+  case GEOMETRYCHUNK:
+    return new GeometryChunkMsg(qds, origin);
+  default:
+    return NULL;
     }
-    return msg;
-}
-void NetMsgFactory::printBufferStatus(bool extended)
-{
-    std::cout << "\n";
-    std::cout << "Buffer pos: " << this->intBuffer->pos() << "\n";
-    std::cout << "Buffer limit: " << this->limit << "\n";
-    std::cout << "Buffer size: " << this->intBuffer->size() << "\n";
 
-    if (extended) {
-       quint64 pos = this->intBuffer->pos();
-       this->intBuffer->reset();
-       QByteArray tData = intBuffer->readAll();
-
-       for (quint32 i = 0; i < this->intBuffer->size(); ++i) {
-           std::cout << tData[i] << ", ";
-       }
-       this->intBuffer->seek(pos);
-       std::cout << "\n";
-    }
-    std::cout << "\n";
 }
 
-void NetMsgFactory::compactBuffer()
-{
-    //Keep only the data between buffer.pos() and LIMIT;
-    //if we have used ALL of the buffer up:
-
-    //this->printBufferStatus();
-
-    if (this->intBuffer->atEnd()) {
-       this->intBuffer->reset();
-       this->limit = 0;
-       return;
-    }
-
-    //if we are at LIMIT, then reset:
-    if (this->intBuffer->pos() == this->limit) {
-       this->intBuffer->reset();
-       this->limit = 0;
-       return;
-    }
-
-    //if we are past LIMIT, then something is terrabad wrong:
-    if (this->intBuffer->pos() > this->limit) {
-       std::cerr << "Buffer Overrun\n";
-       this->intBuffer->reset();
-       this->limit = 0;
-       return;
-    }
-
-    //Copy the good data out
-    quint64 dataLen = this->limit - this->intBuffer->pos();
-    QByteArray tempData = this->intBuffer->read(dataLen);
-
-    //Rewind & write data back in
-    this->intBuffer->reset();
-    this->intBuffer->write(tempData);
-    this->limit = tempData.size();
-
-    //this->printBufferStatus();
-
-
-}
-
-NetMsg* NetMsgFactory::buildMsgByType(quint32 type, QDataStream* qds,
-       QString portalName)
-{
-    switch (type) {
-
-    case TEST_GENERIC_4BYTE_MSG:
-       return new GenericFourBytesMsg(qds, portalName);
-
-    case TEST_GENERIC_2BYTE_MSG:
-       return new GenericTwoBytesMsg(qds, portalName);
-
-    case TEST_GENERIC_1BYTE_MSG:
-       return new GenericOneByteMsg(qds, portalName);
-
-    case TEST_GENERIC_MULTIBYTE_MSG:
-       return new GenericMultiByteMsg(qds, portalName);
-
-    case TEST_GENERIC_1STRING_MSG:
-       return new GenericOneStringMsg(qds, portalName);
-
-
-    case FAILURE:
-       return new GenericOneByteMsg(qds, portalName);
-    case SUCCESS:
-       return new GenericOneByteMsg(qds, portalName);
-    case REMGSHOSTNAMESET:
-       return new GenericOneStringMsg(qds, portalName);
-    case DISCONNECTREQ:
-       return new TypeOnlyMsg(qds, portalName);
-    case NEWHOSTONNET:
-       return new GenericOneStringMsg(qds, portalName);
-//    case FULLHOSTLISTREQ:
-//     return new NetMsg(qds, portalName);
-//    case FULLHOSTLIST:
-//     return new NetMsg(qds, portalName);
-    case NEWSESSIONREQ:
-       return new NewSessionReqMsg(qds, portalName);
-    case SESSIONINFO:
-       return new SessionInfoMsg(qds, portalName);
-    case GEOMETRYREQ:
-       return new GeometryReqMsg(qds, portalName);
-    case GEOMETRYMANIFEST:
-       return new GeometryManifestMsg(qds, portalName);
-    case GEOMETRYCHUNK:
-       return new GeometryChunkMsg(qds, portalName);
-    default:
-       return NULL;
-    }
-
-}
-
-void NetMsgFactory::setPortalName(QString portalName)
-{
-    this->portalName = portalName;
-}
-
-QString NetMsgFactory::getPortalName()
-{
-    return this->portalName;
-}
-
 // Local Variables: ***
 // mode: C++ ***
 // tab-width: 8 ***


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to