Added socket data transfer

Added one common class used to transfer data using socket. This class is
used both from the Octave plugins and from the socket server code in
ResInsight
This commit is contained in:
Magne Sjaastad
2014-04-14 14:36:47 +02:00
parent 1693ae25dd
commit 32bfb65a9d
6 changed files with 158 additions and 78 deletions

View File

@@ -58,6 +58,7 @@ set( SOCKET_INTERFACE_FILES
SocketInterface/RiaPropertyDataCommands.cpp
SocketInterface/RiaWellDataCommands.cpp
SocketInterface/RiaSocketTools.cpp
SocketInterface/RiaSocketDataTransfer.cpp
)
@@ -153,6 +154,8 @@ list( REMOVE_ITEM RAW_SOURCES
Application/RiaImageCompareReporter.cpp
Application/RiaRegressionTest.cpp
SocketInterface/RiaSocketDataTransfer.cpp
FileInterface/RifEclipseInputFileTools.cpp
FileInterface/RifEclipseOutputFileTools.cpp
FileInterface/RifEclipseRestartFilesetAccess.cpp

View File

@@ -0,0 +1,102 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Statoil ASA, Ceetron Solutions AS
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaSocketDataTransfer.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaSocketDataTransfer::writeBlockDataToSocket(QTcpSocket* socket, const char* data, quint64 bytesToWrite, QStringList& errorMessages)
{
quint64 bytesWritten = 0;
int blockCount = 0;
quint64 maxBlockSize = doubleValueCountInBlock() * sizeof(double);
while (bytesWritten < bytesToWrite)
{
quint64 byteCountToWrite = qMin(bytesToWrite - bytesWritten, maxBlockSize);
qint64 actuallyBytesWritten = socket->write(data + bytesWritten, byteCountToWrite);
if (actuallyBytesWritten < 0)
{
errorMessages.push_back("Error detected when writing data, error string from socket");
errorMessages.push_back(socket->errorString());
return false;
}
bytesWritten += actuallyBytesWritten;
blockCount++;
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaSocketDataTransfer::readBlockDataFromSocket(QTcpSocket* socket, char* data, quint64 bytesToRead, QStringList& errorMessages)
{
quint64 bytesRead = 0;
int blockCount = 0;
quint64 maxBlockSize = doubleValueCountInBlock() * sizeof(double);
while (bytesRead < bytesToRead)
{
if (socket->bytesAvailable())
{
quint64 byteCountToRead = qMin(bytesToRead - bytesRead, maxBlockSize);
qint64 actuallyBytesRead = socket->read(data + bytesRead, byteCountToRead);
if (actuallyBytesRead < 0)
{
errorMessages.push_back("Error detected when reading data, error string from socket");
errorMessages.push_back(socket->errorString());
return false;
}
bytesRead += actuallyBytesRead;
blockCount++;
}
else
{
if (!socket->waitForReadyRead())
{
errorMessages.push_back("Waited for data for %1 milli seconds.");
errorMessages.push_back(socket->errorString());
return false;
}
}
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t RiaSocketDataTransfer::doubleValueCountInBlock()
{
return 2000;
}

View File

@@ -0,0 +1,36 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) Statoil ASA, Ceetron Solutions AS
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include <QTcpSocket>
#include <QStringList>
//==================================================================================================
/// Utility class used for transfer of data using QTcpSocket
///
/// As the compile configuration for octave plugins is quite complex,
// the octave plugins includes the cpp-file to be able to compile only one file per plugin
//==================================================================================================
class RiaSocketDataTransfer
{
public:
static size_t doubleValueCountInBlock();
public:
static bool writeBlockDataToSocket(QTcpSocket* socket, const char* data, quint64 bytesToWrite, QStringList& errorMessages);
static bool readBlockDataFromSocket(QTcpSocket* socket, char* data, quint64 bytesToRead, QStringList& errorMessages);
};

View File

@@ -38,8 +38,9 @@
#include "RimInputPropertyCollection.h"
#include <QTcpSocket>
#include "RiaSocketDataTransfer.h"
#include <QTcpSocket>
//--------------------------------------------------------------------------------------------------
///
@@ -104,89 +105,28 @@ void RiaSocketTools::getCaseInfoFromCase(RimCase* rimCase, qint64& caseId, QStri
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaSocketTools::writeBlockData(RiaSocketServer* server, QTcpSocket* socket, const char* data, quint64 bytesToWrite)
{
cvf::Timer timer;
quint64 bytesWritten = 0;
int blockCount = 0;
quint64 maxBlockSize = RiaApplication::instance()->preferences()->blockSize();
while (bytesWritten < bytesToWrite)
{
quint64 byteCountToWrite = qMin(bytesToWrite - bytesWritten, maxBlockSize);
qint64 actuallyBytesWritten = socket->write(data + bytesWritten, byteCountToWrite);
if (actuallyBytesWritten < 0)
{
if (server)
{
QString errorMessage = "Error detected when writing data, error string from socket : \n" + socket->errorString();
server->errorMessageDialog()->showMessage(errorMessage);
}
return false;
}
bytesWritten += actuallyBytesWritten;
blockCount++;
}
QStringList errorMessages;
bool writeSucceded = RiaSocketDataTransfer::writeBlockDataToSocket(socket, data, bytesToWrite, errorMessages);
if (server)
{
for (int i = 0; i < errorMessages.size(); i++)
{
server->errorMessageDialog()->showMessage(errorMessages[i]);
}
double totalTimeMS = timer.time() * 1000.0;
QString resultInfo = QString("Total time '%1 ms'\nTotal bytes written . %2\nNumber of blocks : %3\nBlock size : %4").arg(totalTimeMS).arg(bytesWritten).arg(blockCount).arg(maxBlockSize);
QString resultInfo = QString("Total time '%1 ms'").arg(totalTimeMS);
server->errorMessageDialog()->showMessage(resultInfo);
}
return true;
return writeSucceded;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaSocketTools::readBlockData(QTcpSocket& socket, char* data, quint64 bytesToRead, QStringList& errorMessages)
{
quint64 bytesRead = 0;
int blockCount = 0;
quint64 maxBlockSize = RiaApplication::instance()->preferences()->blockSize();
while (bytesRead < bytesToRead)
{
if (socket.bytesAvailable())
{
quint64 byteCountToRead = qMin(bytesToRead - bytesRead, maxBlockSize);
qint64 actuallyBytesRead = socket.read(data + bytesRead, byteCountToRead);
if (actuallyBytesRead < 0)
{
errorMessages.push_back("Error detected when writing data, error string from socket");
errorMessages.push_back(socket.errorString());
return false;
}
bytesRead += actuallyBytesRead;
blockCount++;
}
else
{
if (!socket.waitForReadyRead())
{
errorMessages.push_back("Waited for data for %1 milli seconds.");
errorMessages.push_back(socket.errorString());
return false;
}
}
}
return true;
}

View File

@@ -28,5 +28,4 @@ public:
static void getCaseInfoFromCase(RimCase* rimCase, qint64& caseId, QString& caseName, QString& caseType, qint64& caseGroupId);
static bool writeBlockData(RiaSocketServer* server, QTcpSocket* socket, const char* data, quint64 bytesToWrite);
static bool readBlockData(QTcpSocket& socket, char* data, quint64 bytesToRead, QStringList& errorMessages);
};