Octave: Change default port number and add custom port number to Preferences

This commit is contained in:
Magne Sjaastad
2024-06-03 07:46:58 +02:00
parent 3f535e5b62
commit 1c899df063
35 changed files with 113 additions and 39 deletions

View File

@@ -1076,7 +1076,7 @@ QStringList RiaApplication::octaveArguments() const
arguments.append( "--path" );
arguments << QApplication::applicationDirPath();
if ( !m_preferences->octaveShowHeaderInfoWhenExecutingScripts )
if ( !m_preferences->octaveShowHeaderInfoWhenExecutingScripts() )
{
// -q
// Don't print the usual greeting and version message at startup.
@@ -1119,6 +1119,19 @@ QProcessEnvironment RiaApplication::octaveProcessEnvironment() const
penv.insert( "LD_LIBRARY_PATH", ldPath );
#endif
// Set the environment variable for the port number used by Octave plugins
// The plugins can access the port number using riOctavePlugin::portNumber()
// If the port number is not set in preferences, the plugins will use the default port number
const QString key = QString::fromStdString( riOctavePlugin::portNumberKey() );
if ( !m_preferences->octavePortNumber().isEmpty() )
{
penv.insert( key, m_preferences->octavePortNumber() );
}
else
{
penv.remove( key );
}
return penv;
}

View File

@@ -118,11 +118,14 @@ RiaPreferences::RiaPreferences()
m_octaveExecutable.uiCapability()->setUiEditorTypeName( caf::PdmUiFilePathEditor::uiEditorTypeName() );
m_octaveExecutable.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );
CAF_PDM_InitField( &octaveShowHeaderInfoWhenExecutingScripts,
CAF_PDM_InitField( &m_octaveShowHeaderInfoWhenExecutingScripts,
"octaveShowHeaderInfoWhenExecutingScripts",
false,
"Show Text Header When Executing Scripts" );
caf::PdmUiNativeCheckBoxEditor::configureFieldForEditor( &octaveShowHeaderInfoWhenExecutingScripts );
caf::PdmUiNativeCheckBoxEditor::configureFieldForEditor( &m_octaveShowHeaderInfoWhenExecutingScripts );
CAF_PDM_InitFieldNoDefault( &m_octavePortNumber, "octavePortNumber", "Octave Port Number" );
m_octavePortNumber.uiCapability()->setUiEditorTypeName( caf::PdmUiCheckBoxAndTextEditor::uiEditorTypeName() );
CAF_PDM_InitField( &m_pythonExecutable, "pythonExecutable", QString( "python" ), "Python Executable Location" );
m_pythonExecutable.uiCapability()->setUiEditorTypeName( caf::PdmUiFilePathEditor::uiEditorTypeName() );
@@ -449,7 +452,8 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
{
caf::PdmUiGroup* octaveGroup = uiOrdering.addNewGroup( "Octave" );
octaveGroup->add( &m_octaveExecutable );
octaveGroup->add( &octaveShowHeaderInfoWhenExecutingScripts );
octaveGroup->add( &m_octaveShowHeaderInfoWhenExecutingScripts );
octaveGroup->add( &m_octavePortNumber );
#ifdef ENABLE_GRPC
caf::PdmUiGroup* pythonGroup = uiOrdering.addNewGroup( "Python" );
@@ -1008,6 +1012,24 @@ QString RiaPreferences::octaveExecutable() const
return m_octaveExecutable().trimmed();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaPreferences::octaveShowHeaderInfoWhenExecutingScripts() const
{
return m_octaveShowHeaderInfoWhenExecutingScripts();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString RiaPreferences::octavePortNumber() const
{
if ( m_octavePortNumber().first ) return m_octavePortNumber().second;
return {};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -119,7 +119,11 @@ public:
// Script paths
QString pythonExecutable() const;
// Octave
QString octaveExecutable() const;
bool octaveShowHeaderInfoWhenExecutingScripts() const;
QString octavePortNumber() const;
QString loggerFilename() const;
int loggerFlushInterval() const;
@@ -137,7 +141,6 @@ public:
caf::PdmField<QString> scriptDirectories;
caf::PdmField<QString> scriptEditorExecutable;
caf::PdmField<bool> octaveShowHeaderInfoWhenExecutingScripts;
caf::PdmField<bool> showPythonDebugInfo;
caf::PdmField<QString> ssihubAddress;
@@ -211,8 +214,12 @@ private:
caf::PdmField<caf::FilePath> m_gridCalculationExpressionFolder;
caf::PdmField<caf::FilePath> m_summaryCalculationExpressionFolder;
// Script paths
caf::PdmField<QString> m_octaveExecutable;
// Octave
caf::PdmField<QString> m_octaveExecutable;
caf::PdmField<bool> m_octaveShowHeaderInfoWhenExecutingScripts;
caf::PdmField<std::pair<bool, QString>> m_octavePortNumber;
// Python
caf::PdmField<QString> m_pythonExecutable;
// Logging

View File

@@ -56,15 +56,24 @@ RiaSocketServer::RiaSocketServer( QObject* parent )
m_nextPendingConnectionTimer->setInterval( 100 );
m_nextPendingConnectionTimer->setSingleShot( true );
if ( !m_tcpServer->listen( QHostAddress::LocalHost, 40001 ) )
int portNumber = riOctavePlugin::defaultPortNumber;
if ( !RiaPreferences::current()->octavePortNumber().isEmpty() )
{
QString txt = "Disabled communication with Octave due to another ResInsight process running.";
portNumber = RiaPreferences::current()->octavePortNumber().toInt();
}
if ( !m_tcpServer->listen( QHostAddress::LocalHost, portNumber ) )
{
QString txt = QString( "Not able to communicate with Octave plugins. Failed to use port number : %1" ).arg( portNumber );
RiaLogging::warning( txt );
return;
}
QString txt = QString( "Octave is using port: %1" ).arg( portNumber );
RiaLogging::info( txt );
connect( m_nextPendingConnectionTimer, SIGNAL( timeout() ), this, SLOT( slotNewClientConnection() ) );
connect( m_tcpServer, SIGNAL( newConnection() ), this, SLOT( slotNewClientConnection() ) );
}

View File

@@ -16,9 +16,21 @@
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QDataStream>
namespace riOctavePlugin
{
const int qtDataStreamVersion = QDataStream::Qt_4_0;
// https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
// Use a port number in the dynamic/private range (49152-65535)
const int defaultPortNumber = 52025;
inline const std::string portNumberKey()
{
return "RESINSIGHT_OCTAVE_PORT_NUMBER";
}
} // namespace riOctavePlugin