From 65710a8e0833707340f78f0e7b28711b4d8a0224 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Mon, 13 May 2013 21:15:38 +0200 Subject: [PATCH] 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 --- .../SocketInterface/RiaSocketServer.cpp | 46 +++++- OctavePlugin/CMakeLists.txt | 20 +++ OctavePlugin/riGetCaseGroups.cpp | 151 ++++++++++++++++++ OctavePlugin/riGetCurrentCase.cpp | 2 +- OctavePlugin/riSettings.h | 5 +- 5 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 OctavePlugin/riGetCaseGroups.cpp diff --git a/ApplicationCode/SocketInterface/RiaSocketServer.cpp b/ApplicationCode/SocketInterface/RiaSocketServer.cpp index b0088309b0..4f3877736e 100644 --- a/ApplicationCode/SocketInterface/RiaSocketServer.cpp +++ b/ApplicationCode/SocketInterface/RiaSocketServer.cpp @@ -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 groupNames; + std::vector 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) { diff --git a/OctavePlugin/CMakeLists.txt b/OctavePlugin/CMakeLists.txt index 41c814697c..590d0f1d19 100644 --- a/OctavePlugin/CMakeLists.txt +++ b/OctavePlugin/CMakeLists.txt @@ -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/$" + ) + 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) + + diff --git a/OctavePlugin/riGetCaseGroups.cpp b/OctavePlugin/riGetCaseGroups.cpp new file mode 100644 index 0000000000..a302c4fbbe --- /dev/null +++ b/OctavePlugin/riGetCaseGroups.cpp @@ -0,0 +1,151 @@ +#include +#include + +#include "riSettings.h" + +void getCaseGroups(std::vector& groupNames, std::vector& 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 groupNames; + std::vector 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 (); +} + diff --git a/OctavePlugin/riGetCurrentCase.cpp b/OctavePlugin/riGetCurrentCase.cpp index 4a44b8c459..b3fdc72522 100644 --- a/OctavePlugin/riGetCurrentCase.cpp +++ b/OctavePlugin/riGetCurrentCase.cpp @@ -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); diff --git a/OctavePlugin/riSettings.h b/OctavePlugin/riSettings.h index 953b96d633..957294d05e 100644 --- a/OctavePlugin/riSettings.h +++ b/OctavePlugin/riSettings.h @@ -16,7 +16,10 @@ // ///////////////////////////////////////////////////////////////////////////////// -namespace riOctave +namespace riOctavePlugin { const int timeOutMilliSecs = 5000; + + const int qtDataStreamVersion = QDataStream::Qt_4_0; + } \ No newline at end of file