Get SpatialOS

Sites

Menu
These are the docs for 13.2, an old version of SpatialOS. 13.3 is the newest →

Connecting to SpatialOS

All code examples in this section assume you have defined a MyComponents function as described in Providing components, and set up the following preamble:

#include <improbable/standard_library.h>
#include <improbable/worker.h>
#include <example.h>

using namespace improbable;

Before it can interact with the simulated world, the worker must connect to SpatialOS.

There are two ways of doing this: using the worker::Connection and using the worker::Locator.

Connecting a managed worker

To connect a managed worker, instantiate a worker::Connection object directly. Note that worker::Connection objects are not thread-safe.

This should be used to connect a server-worker, or a worker connecting for debugging purposes via spatial cloud connect external.

The connection is made through the receptionist service of the target deployment; the IP and port for that deployment are passed as arguments to the worker. TTo have these values filled in automatically, use the IMPROBABLE_RECEPTIONIST_HOST and IMPROBABLE_RECEPTIONIST_PORT placeholders in your worker’s launch configuration.

Connecting an external/client worker

Use the worker::Locator object to enumerate cloud deployments and connect to a chosen deployment with authentication.

This is typically used to connect clients to a cloud deployment.

Values for UseExternalIP

You need to use the correct value for the UseExternalIp field in NetworkParameters. The table below summarizes the connection possibilities:

Using worker::Connection directly Using worker::Locator
UseExternalIp == true Local client connecting via spatial cloud connect external proxy External client connecting to cloud deployment
UseExternalIp == false Managed cloud worker; local client connecting to local deployment N/A

Example connection setup

The example below illustrates a very basic connection setup, where the function takes one argument specifying the worker’s own ID, and an optional second one representing the receptionist IP address. If the latter is not provided, the worker connects to localhost. In either case, it uses TCP and connects using the internal IP address.

worker::Connection GetConnection(const std::string& worker_id, const std::string& hostname) {
  worker::ConnectionParameters parameters;
  parameters.WorkerType = "CppDocsWorker";
  parameters.Network.ConnectionType = worker::NetworkConnectionType::kTcp;
  parameters.Network.UseExternalIp = false;

  return worker::Connection::ConnectAsync(MyComponents(), hostname, 7777, worker_id, parameters)
      .Get();
}

Worker attributes

A worker is assigned some static attributes when it connects to the SpatialOS runtime:

  • a worker ID which is a unique identifier string for this particular worker.
  • a list of worker attributes that are used when to determine to which worker the authority over a component can be delegated, either automatically or via by the developer through ACLs.

Get the values of these configuration settings using Connection.GetWorkerId() and Connection.GetWorkerAttributes().

Search results

Was this page helpful?

Thanks for letting us know!

Thanks for your feedback

Need more help? Ask on the forums