Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test if internet listener socket creation succeeded? #474

Open
Bryan-Stearns opened this issue Jun 4, 2024 · 3 comments
Open

Test if internet listener socket creation succeeded? #474

Bryan-Stearns opened this issue Jun 4, 2024 · 3 comments

Comments

@Bryan-Stearns
Copy link
Contributor

I had been hoping there was a way to test if a kernel was successful at creating a listener socket.
If one creates a kernel with a port that is already taken, socket creation will fail and print to the terminal a corresponding message.

I can't seem to find any way of detecting that failure using SML, however.

If I try to use kernel.HadError() or kernel.GetLastErrorDescription(), it shows as no error.
If I try to use kernel.GetAllConnectionInfo() and kernel.GetConnectionInfo(0) or kernel.GetConnection(), what I get back doesn't seem to be usable. (The functions I see in sml_ClientConnection.h don't seem to be available on the returned object, and the only functions my debugger shows as available for that object are cryptic and don't seem like they're meant to be used by an SML coder.)
And kernel.GetConnectionStatus(char const* pConnectionName) requires a connection name, and I see no indication where that might come from.

So maybe I'm just missing something. But if this info is not inspectable, it would be nice to make it inspectable.

@garfieldnate
Copy link
Collaborator

How about kernel.IsConnectionClosed?

return !GetConnection() || GetConnection()->IsClosed() ;

@Bryan-Stearns
Copy link
Contributor Author

How about kernel.IsConnectionClosed?

Sadly no good. It returns False for me whether the internet socket failed or not.

In more detail, the following code:

SOAR_PORT = 12121
print(f"Creating Soar Kernel using port {SOAR_PORT}")
kernel = sml.Kernel.CreateKernelInNewThread(SOAR_PORT)

print(kernel.HadError())
print(kernel.GetLastErrorDescription())
print(kernel.IsConnectionClosed())

Yields the following output if I already have a debugger open that's claimed port 12121:

Creating Soar Kernel using port 12121
Error: Error binding the listener socket to its port number
Failed to create the internet listener socket.  Shutting down listener thread.
False
No Error
False

@garfieldnate
Copy link
Collaborator

Thanks, I see what you mean now. The kernel is created successfully, meaning that the SML connection (which is just a communication API, not necessarily involving a network) is created successfully, as well.

You have to dive pretty far into the call to CreateKernelInNewThread() to find it, but the message is printed when sml_ListenerThread in the new kernel starts. The thread then stops running immediately without setting any kind of error flag. If we add an error flag (and I think we should), we would need to pipe it up through many layers: sml_ConnectionManager, sml_KernelSML, KernelSMLInterface, sml_EmbeddedConnection, sml_Connection, and finally sml_ClientKernel.

I was going to suggest, as a very last resort, that you try agent.SpawnDebugger() followed by kernel.GetNumberConnections(), but that appears to always be returning 0 for me, so I don't think it's reliable (not that it was a satisfactory workaround anyway!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants