SoarGroup / Soar

Soar, a general cognitive architecture for systems that exhibit intelligent behavior.
http://soar.eecs.umich.edu
Other
322 stars 70 forks source link

Test if internet listener socket creation succeeded? #474

Open Bryan-Stearns opened 3 weeks ago

Bryan-Stearns commented 3 weeks ago

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 commented 3 weeks ago

How about kernel.IsConnectionClosed? https://github.com/SoarGroup/Soar/blob/bdf5f02c33c103c896178f42f106b2176b18cbce/Core/ClientSML/src/sml_ClientKernel.cpp#L150

Bryan-Stearns commented 3 weeks ago

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 commented 3 weeks ago

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!).