You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2012/02/11 20:09:30 UTC

svn commit: r1243124 - in /thrift/trunk/lib/cpp/src/qt: TQIODeviceTransport.cpp TQIODeviceTransport.h TQTcpServer.cpp TQTcpServer.h

Author: roger
Date: Sat Feb 11 19:09:30 2012
New Revision: 1243124

URL: http://svn.apache.org/viewvc?rev=1243124&view=rev
Log:
THRIFT-1348 C++ Qt bindings
Patch: Doug Rosvick
qt-cleanup.patch applied

Modified:
    thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.cpp
    thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.h
    thrift/trunk/lib/cpp/src/qt/TQTcpServer.cpp
    thrift/trunk/lib/cpp/src/qt/TQTcpServer.h

Modified: thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.cpp
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.cpp?rev=1243124&r1=1243123&r2=1243124&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.cpp (original)
+++ thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.cpp Sat Feb 11 19:09:30 2012
@@ -1,155 +1,157 @@
 #include "TQIODeviceTransport.h"
 
-#include <transport/TBufferTransports.h>
-
 #include <QAbstractSocket>
+#include <QIODevice>
 
-#include <iostream>
+#include <transport/TBufferTransports.h>
 
+using boost::shared_ptr;
+  
 namespace apache { namespace thrift { namespace transport {
-  using boost::shared_ptr;
-
-  TQIODeviceTransport::TQIODeviceTransport(shared_ptr<QIODevice> dev)
-    : dev_(dev)
-  {
-  }
-
-  TQIODeviceTransport::~TQIODeviceTransport()
-  {
-    dev_->close();
-  }
-
-  void TQIODeviceTransport::open()
-  {
-    if (!isOpen()) {
-      throw TTransportException(TTransportException::NOT_OPEN,
-                                "open(): underlying QIODevice isn't open");
-    }
-  }
 
-  bool TQIODeviceTransport::isOpen()
-  {
-    return dev_->isOpen();
-  }
-
-  bool TQIODeviceTransport::peek()
-  {
-    return dev_->bytesAvailable() > 0;
-  }
-
-  void TQIODeviceTransport::close()
-  {
-    dev_->close();
-  }
-
-  uint32_t TQIODeviceTransport::readAll(uint8_t* buf, uint32_t len) {
-    uint32_t requestLen = len;
-    while (len) {
-      uint32_t readSize;
-      try {
-        readSize = read(buf, len);
-      } catch (...) {
-        if (len != requestLen) {
-          // something read already
-          return requestLen - len;
-        }
-        // error but nothing read yet
-        throw;
-      }
-      if (readSize == 0) {
-        // dev_->waitForReadyRead(50);
-      } else {
-        buf += readSize;
-        len -= readSize;
+TQIODeviceTransport::TQIODeviceTransport(shared_ptr<QIODevice> dev)
+  : dev_(dev)
+{
+}
+
+TQIODeviceTransport::~TQIODeviceTransport()
+{
+  dev_->close();
+}
+
+void TQIODeviceTransport::open()
+{
+  if (!isOpen()) {
+    throw TTransportException(TTransportException::NOT_OPEN,
+                              "open(): underlying QIODevice isn't open");
+  }
+}
+
+bool TQIODeviceTransport::isOpen()
+{
+  return dev_->isOpen();
+}
+
+bool TQIODeviceTransport::peek()
+{
+  return dev_->bytesAvailable() > 0;
+}
+
+void TQIODeviceTransport::close()
+{
+  dev_->close();
+}
+
+uint32_t TQIODeviceTransport::readAll(uint8_t* buf, uint32_t len)
+{
+  uint32_t requestLen = len;
+  while (len) {
+    uint32_t readSize;
+    try {
+      readSize = read(buf, len);
+    } catch (...) {
+      if (len != requestLen) {
+        // something read already
+        return requestLen - len;
       }
+      // error but nothing read yet
+      throw;
+    }
+    if (readSize == 0) {
+      dev_->waitForReadyRead(50);
+    } else {
+      buf += readSize;
+      len -= readSize;
     }
-    return requestLen;
   }
+  return requestLen;
+}
 
-  uint32_t TQIODeviceTransport::read(uint8_t* buf, uint32_t len)
-  {
-    uint32_t actualSize;
-    qint64 readSize;
+uint32_t TQIODeviceTransport::read(uint8_t* buf, uint32_t len)
+{
+  uint32_t actualSize;
+  qint64 readSize;
 
-    if (!dev_->isOpen()) {
-      throw TTransportException(TTransportException::NOT_OPEN,
-                                "read(): underlying QIODevice is not open");
-    }
+  if (!dev_->isOpen()) {
+    throw TTransportException(TTransportException::NOT_OPEN,
+                              "read(): underlying QIODevice is not open");
+  }
 
-    actualSize = (uint32_t)std::min((qint64)len, dev_->bytesAvailable());
-    readSize = dev_->read(reinterpret_cast<char *>(buf), len);
+  actualSize = (uint32_t)std::min((qint64)len, dev_->bytesAvailable());
+  readSize = dev_->read(reinterpret_cast<char *>(buf), actualSize);
 
-    if (readSize < 0) {
-      QAbstractSocket* socket;
-      if ((socket = qobject_cast<QAbstractSocket* >(dev_.get()))) {
-        throw TTransportException(TTransportException::UNKNOWN,
-                                  "Failed to read() from QAbstractSocket",
-                                  socket->error());
-      }
+  if (readSize < 0) {
+    QAbstractSocket* socket;
+    if ((socket = qobject_cast<QAbstractSocket* >(dev_.get()))) {
       throw TTransportException(TTransportException::UNKNOWN,
-                                "Failed to read from from QIODevice");
+                                "Failed to read() from QAbstractSocket",
+                                socket->error());
     }
-
-    return (uint32_t)readSize;
+    throw TTransportException(TTransportException::UNKNOWN,
+                              "Failed to read from from QIODevice");
   }
 
-  void TQIODeviceTransport::write(const uint8_t* buf, uint32_t len)
-  {
-    while (len) {
-      uint32_t written = write_partial(buf, len);
-      len -= written;
-      // dev_->waitForBytesWritten(50);
-    }
+  return (uint32_t)readSize;
+}
+
+void TQIODeviceTransport::write(const uint8_t* buf, uint32_t len)
+{
+  while (len) {
+    uint32_t written = write_partial(buf, len);
+    len -= written;
+    dev_->waitForBytesWritten(50);
   }
+}
 
-  uint32_t TQIODeviceTransport::write_partial(const uint8_t* buf, uint32_t len)
-  {
-    qint64 written;
-
-    if (!dev_->isOpen()) {
-      throw TTransportException(TTransportException::NOT_OPEN,
-                                "write_partial(): underlying QIODevice is not open");
-    }
+uint32_t TQIODeviceTransport::write_partial(const uint8_t* buf, uint32_t len)
+{
+  qint64 written;
 
-    written = dev_->write(reinterpret_cast<const char*>(buf), len);
-    if (written < 0) {
-      QAbstractSocket* socket;
-      if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
-        throw TTransportException(TTransportException::UNKNOWN,
-                                  "write_partial(): failed to write to QAbstractSocket", socket->error());
-      }
+  if (!dev_->isOpen()) {
+    throw TTransportException(TTransportException::NOT_OPEN,
+                              "write_partial(): underlying QIODevice is not open");
+  }
 
+  written = dev_->write(reinterpret_cast<const char*>(buf), len);
+  if (written < 0) {
+    QAbstractSocket* socket;
+    if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
       throw TTransportException(TTransportException::UNKNOWN,
-                                "write_partial(): failed to write to underlying QIODevice");
+                                "write_partial(): failed to write to QAbstractSocket", socket->error());
     }
 
-    return (uint32_t)written;
+    throw TTransportException(TTransportException::UNKNOWN,
+                              "write_partial(): failed to write to underlying QIODevice");
   }
 
-  void TQIODeviceTransport::flush()
-  {
-    if (!dev_->isOpen()) {
-      throw TTransportException(TTransportException::NOT_OPEN,
-                                "flush(): underlying QIODevice is not open");
-    }
+  return (uint32_t)written;
+}
 
-    QAbstractSocket* socket;
-
-    if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
-      socket->flush();
-    } else {
-      dev_->waitForBytesWritten(1);
-    }
+void TQIODeviceTransport::flush()
+{
+  if (!dev_->isOpen()) {
+    throw TTransportException(TTransportException::NOT_OPEN,
+                              "flush(): underlying QIODevice is not open");
   }
 
-  uint8_t* TQIODeviceTransport::borrow(uint8_t* buf, uint32_t* len)
-  {
-    return NULL;
-  }
+  QAbstractSocket* socket;
 
-  void TQIODeviceTransport::consume(uint32_t len)
-  {
-    throw TTransportException(TTransportException::UNKNOWN);
+  if ((socket = qobject_cast<QAbstractSocket*>(dev_.get()))) {
+    socket->flush();
+  } else {
+    dev_->waitForBytesWritten(1);
   }
+}
+
+uint8_t* TQIODeviceTransport::borrow(uint8_t* buf, uint32_t* len)
+{
+  return NULL;
+}
+
+void TQIODeviceTransport::consume(uint32_t len)
+{
+  throw TTransportException(TTransportException::UNKNOWN);
+}
+
 }}} // apache::thrift::transport
 

Modified: thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.h
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.h?rev=1243124&r1=1243123&r2=1243124&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.h (original)
+++ thrift/trunk/lib/cpp/src/qt/TQIODeviceTransport.h Sat Feb 11 19:09:30 2012
@@ -1,47 +1,44 @@
 #ifndef _THRIFT_ASYNC_TQIODEVICE_TRANSPORT_H_
 #define _THRIFT_ASYNC_TQIODEVICE_TRANSPORT_H_ 1
 
-#include <QIODevice>
-
 #include <boost/shared_ptr.hpp>
 
 #include <transport/TVirtualTransport.h>
 
-namespace apache { namespace thrift { namespace protocol {
-class TProtocol;
-}}} // apache::thrift::protocol
+class QIODevice;
 
 namespace apache { namespace thrift { namespace transport {
 
-  class TQIODeviceTransport : public apache::thrift::transport::TVirtualTransport<TQIODeviceTransport> {
-    public:
-      explicit TQIODeviceTransport(boost::shared_ptr<QIODevice> dev);
-      ~TQIODeviceTransport();
-
-      void open();
-
-      bool isOpen();
-    
-      bool peek();
-    
-      void close();
-    
-      uint32_t readAll(uint8_t *buf, uint32_t len);
-
-      uint32_t read(uint8_t* buf, uint32_t len);
-
-      void write(const uint8_t* buf, uint32_t len);
-
-      uint32_t write_partial(const uint8_t* buf, uint32_t len);
-
-      void flush();
-
-      uint8_t* borrow(uint8_t* buf, uint32_t* len);
-      void consume(uint32_t len);
-
-    private:
-      boost::shared_ptr<QIODevice> dev_;
-  };
+/**
+ *  Transport that operates on a QIODevice (socket, file, etc).
+ */
+class TQIODeviceTransport : public apache::thrift::transport::TVirtualTransport<TQIODeviceTransport> {
+ public:
+  explicit TQIODeviceTransport(boost::shared_ptr<QIODevice> dev);
+  virtual ~TQIODeviceTransport();
+
+  void open();
+  bool isOpen();
+  bool peek();
+  void close();
+
+  uint32_t readAll(uint8_t *buf, uint32_t len);
+  uint32_t read(uint8_t* buf, uint32_t len);
+
+  void write(const uint8_t* buf, uint32_t len);
+  uint32_t write_partial(const uint8_t* buf, uint32_t len);
+
+  void flush();
+
+  uint8_t* borrow(uint8_t* buf, uint32_t* len);
+  void consume(uint32_t len);
+
+ private:
+   TQIODeviceTransport(const TQIODeviceTransport&);
+   TQIODeviceTransport& operator=(const TQIODeviceTransport&);
+   
+   boost::shared_ptr<QIODevice> dev_;
+};
 }}} // apache::thrift::transport
 
 #endif // #ifndef _THRIFT_ASYNC_TQIODEVICE_TRANSPORT_H_

Modified: thrift/trunk/lib/cpp/src/qt/TQTcpServer.cpp
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/qt/TQTcpServer.cpp?rev=1243124&r1=1243123&r2=1243124&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/qt/TQTcpServer.cpp (original)
+++ thrift/trunk/lib/cpp/src/qt/TQTcpServer.cpp Sat Feb 11 19:09:30 2012
@@ -1,11 +1,13 @@
-#include "TQTcpServer.h"
 
-#include <protocol/TProtocol.h>
-#include <async/TAsyncProcessor.h>
+#include "TQTcpServer.h"
+#include "TQIODeviceTransport.h"
 
 #include <QTcpSocket>
 
-#include "TQIODeviceTransport.h"
+#include <tr1/functional>
+
+#include <protocol/TProtocol.h>
+#include <async/TAsyncProcessor.h>
 
 using boost::shared_ptr;
 using apache::thrift::protocol::TProtocol;
@@ -91,8 +93,7 @@ void TQTcpServer::beginDecode()
   QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender()));
   Q_ASSERT(connection);
 
-  if (ctxMap_.find(connection) == ctxMap_.end())
-  {
+  if (ctxMap_.find(connection) == ctxMap_.end()) {
     qWarning("[TQTcpServer] Got data on an unknown QTcpSocket");
     return;
   }
@@ -119,8 +120,7 @@ void TQTcpServer::socketClosed()
   QTcpSocket* connection(qobject_cast<QTcpSocket*>(sender()));
   Q_ASSERT(connection);
 
-  if (ctxMap_.find(connection) == ctxMap_.end())
-  {
+  if (ctxMap_.find(connection) == ctxMap_.end()) {
     qWarning("[TQTcpServer] Unknown QTcpSocket closed");
     return;
   }
@@ -130,8 +130,7 @@ void TQTcpServer::socketClosed()
 
 void TQTcpServer::finish(shared_ptr<ConnectionContext> ctx, bool healthy)
 {
-  if (!healthy)
-  {
+  if (!healthy) {
     qWarning("[TQTcpServer] Processor failed to process data successfully");
     ctxMap_.erase(ctx->connection_.get());
   }

Modified: thrift/trunk/lib/cpp/src/qt/TQTcpServer.h
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/cpp/src/qt/TQTcpServer.h?rev=1243124&r1=1243123&r2=1243124&view=diff
==============================================================================
--- thrift/trunk/lib/cpp/src/qt/TQTcpServer.h (original)
+++ thrift/trunk/lib/cpp/src/qt/TQTcpServer.h Sat Feb 11 19:09:30 2012
@@ -6,10 +6,7 @@
 
 #include <boost/shared_ptr.hpp>
 
-#include <tr1/functional>
-
 namespace apache { namespace thrift { namespace protocol {
-class TProtocol;
 class TProtocolFactory;
 }}} // apache::thrift::protocol
 
@@ -17,6 +14,11 @@ namespace apache { namespace thrift { na
 
 class TAsyncProcessor;
 
+/**
+ *  Server that uses Qt to listen for connections.
+ *  Simply give it a QTcpServer that is listening, along with an async
+ *  processor and a protocol factory, and then run the Qt event loop.
+ */
 class TQTcpServer : public QObject {
  Q_OBJECT
  public:
@@ -32,6 +34,9 @@ class TQTcpServer : public QObject {
   void socketClosed();
 
  private:
+  TQTcpServer(const TQTcpServer&);
+  TQTcpServer& operator=(const TQTcpServer&);
+  
   class ConnectionContext;
 
   void finish(boost::shared_ptr<ConnectionContext> ctx, bool healthy);