Introduction

BrainStim can be externally controlled over a network by enabling a Internal Network Interface by enabling a Internal Network Interface, see the Network Server setting of the Settings Dialog. After enabling it and restarting BrainStim the Internal Network Interface Server will automatically listen to a port number of an IP-Address for incoming messages. These messages are then directly threated and executed as QtScript code.

Example

Let's try to make use of this Internal Network Interface by enabling it and control one instance of a BrainStim process by another.

Important! If we want to run two instances of BrainStim processes simultaneously we first have to make sure that this is possible by enabling the Allow multiple instances of BrainStim setting in the Settings Dialog.

  1. Start (the first instance) of BrainStim.
  2. Enable/check the BrainStim setting Allow multiple instances of BrainStim in the Settings Dialog.
  3. Enable/check the BrainStim setting Enable the Internal Network Interface at startup in the Settings Dialog, leave the IP-Address field empty and Port set to '0' for auto-discovery-assignment.
  4. Restart BrainStim, you might get a Windows security dialog asking for your configuration and permission to allow network traffic to be handled by our above enabled service, make sure to allow this.
  5. After BrainStim started you might have noticed the following message to appear inside the 'default' OutputLog tab:
     
    Debug: "Network Server started @ <ip-address>:<port>"

    The above message tells us that the Internal Network Interface Server was successfully started together with the auto assigned IP-Address (see <ip-address>) and assigned Port number (see <port>).

  6. Start (the second instance of) BrainStim.
  7. After this (second instance of) BrainStim started you might have noticed the following message to appear inside the 'default' OutputLog tab:
     
    Debug: Configured Network Server disabled.

    Important! The above message tells us that the Internal Network Interface Server was not started for this instance of BrainStim, because only one instance of BrainStim at a time can be used for handling network messages.

    Furthermore the (first instance of) BrainStim now also shows the following message to appear inside the 'default' OutputLog tab:

    -> SocketData Received [Log("New BrainStim Instance Initializing...");]
    New BrainStim Instance Initializing...
    -> SocketData Successfully executed by the Script Engine.

    Important! Here we can see that the (first instance of) BrainStim also knows (detects) if another instance of BrainStim is starting, this feature is always enabled even if you disable the Network Server setting of the Settings Dialog because these messages (SocketData) are send over another interface.

    Now we can use the second instance of BrainStim to send messages (script code) to the first Instance, but first you'll have to make sure to install and enable the TCPNetworkServer plugin which is not by default installed. You can find it here. Alternatively you could make your own custom implementation for a client that can connect to BrainStim.

    Now we'll proceed with making use of the TCPNetworkServer plugin.

  8. Make sure to activate the second instance of BrainStim and open the TCPNetworkClient.qs file located in the Main Program Directory directory under the subfolder \Examples\BrainStim\QtScript TCPNetwork\
  9. Change the line where the Server Port Number is defined, and change the number to the assigned port number as shown in step 5.
    var sServerPort = "12345";
  10. Save the document (CTRL + s key) and examine the code.
  11. Execute (press the F5 key) the QtScript code.
  12. The 'default' OutputLog tab from (the second instance of) BrainStim now shows something like:
    ... Valid Script Syntax ...
    ... Script started Evaluating on 13:24:09...
    ServerDataRecieved() called: Welcome you're connected to BrainStim
    CleanupScript succeeded!
    ... Total Time(ms): 11
    ... Script fully Evaluated on 13:24:09 ...

    We see that when we connect through this network interface and established a connection a welcome message "Welcome you're connected to BrainStim" is then send from the server to the client.

  13. if we now activate the first instance of BrainStim and look at the 'default' OutputLog tab we'll now see something like:
    Debug: "The Network Server accepted an incoming connection"
    This is send from the client
    Debug: NetworkServerThreadClient::clientDisconnected A client disconnected an active network connection.
  14. The first line tells us that the Internal Network Interface accepted an Incoming connection. The second line is the result from the execution of the script code send:

    "Log(\"This string is send from the client\")"
    And finally the third line shows that the client disconnected.