Added riGetCaseGroups

Added Qt DataStream version to riSettings
VisualStudio: Copy generated *.oct files into Application folder
VisualStudio: ResInsight is dependent on octave_plugin
p4#: 21602
This commit is contained in:
Magne Sjaastad 2013-05-13 21:15:38 +02:00
parent cad25bcd59
commit 65710a8e08
5 changed files with 220 additions and 4 deletions

View File

@ -248,9 +248,10 @@ void RiaSocketServer::readCommandFromOctave()
bool isSetProperty = args[0] == "SetProperty"; // SetProperty [casename/index] PropertyName
bool isGetCellInfo = args[0] == "GetActiveCellInfo"; // GetActiveCellInfo [casename/index]
bool isGetGridDim = args[0] == "GetMainGridDimensions"; // GetMainGridDimensions [casename/index]
bool isGetCurrentCase = args[0] == "GetCurrentCase";
bool isGetCurrentCase = args[0] == "GetCurrentCase";
bool isGetCaseGroups = args[0] == "GetCaseGroups";
if (!(isGetProperty || isSetProperty || isGetCellInfo || isGetGridDim || isGetCurrentCase))
if (!(isGetProperty || isSetProperty || isGetCellInfo || isGetGridDim || isGetCurrentCase || isGetCaseGroups))
{
m_errorMessageDialog->showMessage(tr("ResInsight SocketServer: \n") + tr("Unknown command: %1").arg(args[0].data()));
terminateCurrentConnection();
@ -298,6 +299,47 @@ void RiaSocketServer::readCommandFromOctave()
return;
}
else if (isGetCaseGroups)
{
if (RiaApplication::instance()->project())
{
std::vector<QString> groupNames;
std::vector<qint64> groupIds;
size_t caseGroupCount = RiaApplication::instance()->project()->caseGroups().size();
quint64 byteCount = 0;
for (size_t i = 0; i < caseGroupCount; i++)
{
RimIdenticalGridCaseGroup* cg = RiaApplication::instance()->project()->caseGroups()[i];
QString caseGroupName = cg->name;
qint64 caseGroupId = cg->groupId;
byteCount += caseGroupName.size() * sizeof(QChar);
byteCount += sizeof(qint64);
groupNames.push_back(caseGroupName);
groupIds.push_back(caseGroupId);
}
socketStream << (quint64)byteCount;
socketStream << (quint64)caseGroupCount;
for (size_t i = 0; i < caseGroupCount; i++)
{
socketStream << groupNames[i];
socketStream << groupIds[i];
}
}
else
{
// ERROR
}
return;
}
if (reservoir == NULL)
{

View File

@ -6,6 +6,7 @@ set(CPP_SOURCES
riGetActiveCellInfo.cpp
riGetMainGridDimensions.cpp
riGetCurrentCase.cpp
riGetCaseGroups.cpp
)
if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
@ -101,9 +102,26 @@ else()
"${CMAKE_CURRENT_BINARY_DIR}/riGetActiveCellInfo.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetMainGridDimensions.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCurrentCase.oct"
"${CMAKE_CURRENT_BINARY_DIR}/riGetCaseGroups.oct"
SOURCES ${CPP_SOURCES}
)
# Copy Octave generated *.oct files to application folder, will make it possible to use Octave functions
# directly from the location of the ResInsight binaries
if (MSVC)
foreach (oct_bin ${OCTAVE_BINARY_OCT_FILES})
add_custom_command(TARGET octave_plugins POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${oct_bin}"
"${CMAKE_CURRENT_BINARY_DIR}/../ApplicationCode/$<CONFIGURATION>"
)
endforeach( oct_bin )
endif(MSVC)
# Make ResInsight dependant on Octave, makes it easiser to debug Octave functionality by compiling ResInsight
add_dependencies(ResInsight octave_plugins)
endif()
if (PRIVATE_INSTALL)
@ -126,3 +144,5 @@ else (PRIVATE_INSTALL)
DESTINATION ${OCTAVE_SITE_OCT_DIR}
)
endif (PRIVATE_INSTALL)

View File

@ -0,0 +1,151 @@
#include <QtNetwork>
#include <octave/oct.h>
#include "riSettings.h"
void getCaseGroups(std::vector<QString>& groupNames, std::vector<int>& groupIds, const QString &hostName, quint16 port)
{
QString serverName = hostName;
quint16 serverPort = port;
const int timeout = riOctavePlugin::timeOutMilliSecs;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);
if (!socket.waitForConnected(timeout))
{
error((("Connection: ") + socket.errorString()).toLatin1().data());
return;
}
// Create command and send it:
QString command("GetCaseGroups");
QByteArray cmdBytes = command.toLatin1();
QDataStream socketStream(&socket);
socketStream.setVersion(riOctavePlugin::qtDataStreamVersion);
socketStream << (qint64)(cmdBytes.size());
socket.write(cmdBytes);
// Get response. First wait for the header
while (socket.bytesAvailable() < (int)(2*sizeof(quint64)))
{
if (!socket.waitForReadyRead(timeout))
{
error((("Waiting for data: ") + socket.errorString()).toLatin1().data());
return;
}
OCTAVE_QUIT;
}
quint64 byteCount;
socketStream >> byteCount;
quint64 groupCount;
socketStream >> groupCount;
// Get response. Read all data for command
while (socket.bytesAvailable() < (int)byteCount)
{
if (!socket.waitForReadyRead(timeout))
{
error((("Waiting for data: ") + socket.errorString()).toLatin1().data());
return;
}
OCTAVE_QUIT;
}
quint64 group = 0;
while (group < groupCount)
{
QString caseGroupName;
qint64 caseGroupId;
socketStream >> caseGroupName;
socketStream >> caseGroupId;
groupNames.push_back(caseGroupName);
groupIds.push_back(caseGroupId);
group++;
}
return;
}
DEFUN_DLD (riGetCaseGroups, args, nargout,
"Usage:\n"
"\n"
" riGetCaseGroups()\n"
"\n"
"Returns a vector of all the case groups in the current ResInsight project"
)
{
int nargin = args.length ();
if (nargin > 0)
{
error("riGetCaseGroups: Too many arguments, this function does not take any arguments.\n");
print_usage();
}
else if (nargout == 0 || nargout > 2)
{
error("riGetCaseGroups: Wrong number of output arguments, expects one or two output arguments.\n");
print_usage();
}
else
{
std::vector<QString> groupNames;
std::vector<int> groupIds;
getCaseGroups(groupNames, groupIds, "127.0.0.1", 40001);
int caseGroupCount = groupNames.size();
if (caseGroupCount == 0)
{
return octave_value_list();
}
int maxStringLength = 0;
for (size_t i = 0; i < caseGroupCount; i++)
{
if (groupNames[i].length() > maxStringLength)
{
maxStringLength = groupNames[i].length();
}
}
int32NDArray octave_groupIds;
dim_vector dv (1, 1);
dv(0) = caseGroupCount;
octave_groupIds.resize(dv);
charMatrix ch;
ch.resize(caseGroupCount, maxStringLength);
octave_value_list retval;
for (size_t i = 0; i < caseGroupCount; i++)
{
ch.insert(groupNames[i].toLatin1().data(), i, 0);
octave_groupIds(i) = groupIds[i];
}
if (nargout >= 1)
{
retval(0) = octave_groupIds;
}
if (nargout >= 2)
{
retval(1) = octave_value (ch, true, '\'');
}
return retval;
}
return octave_value_list ();
}

View File

@ -8,7 +8,7 @@ void getCurrentCase(int& currentCaseId, const QString &hostName, quint16 port)
QString serverName = hostName;
quint16 serverPort = port;
const int Timeout = riOctave::timeOutMilliSecs;
const int Timeout = riOctavePlugin::timeOutMilliSecs;
QTcpSocket socket;
socket.connectToHost(serverName, serverPort);

View File

@ -16,7 +16,10 @@
//
/////////////////////////////////////////////////////////////////////////////////
namespace riOctave
namespace riOctavePlugin
{
const int timeOutMilliSecs = 5000;
const int qtDataStreamVersion = QDataStream::Qt_4_0;
}