Exchanging data between connected Devices¶
CLAID offers two options for exchanging data between connected devices, for example a Smartphone and a Server:
- Synchronous communication via Channels: Modules running across connected devices are logically integrated into one System by CLAID. This allows Modules running on one device to access all Channels on a remote device and vice versa. This allows (near) realtime streaming of Data, as long as a connection persists. We will cover this in Tutorial 2.6
- Asynchronous transfer of files: Files on the filesystem, for example recorded by means covered in the previous Tutorial, can be synchronized asynchronously using our DataSyncModule, which we will cover in this Tutorial. Typically, this is the option we use when collecting data on mobile devices like smartwatches or smartphones. Since a network connection can not be guaranteed continuously, we typically store data on the device and synchronize it with the server when a connection becomes available.
The following figure shows, how a typical asynchronous pipeline for the collection of data from mobile devices to a connected Server looks like:
You see, that two instances of CLAID are connected via a remote connection, which is based on TCP/IPV4 (note, that CLAID does not yet support IPV6). Files from the Smartphone or a Smartwatch will be synchronized with a connected PC or Server via the DataSyncModule and DataReceiverModule respectively, based on configurable synchronization intervalls or conditions, as we will discuss in the following subsections.
1. Creating a new CLAID application on your PC or Server to receive data¶
During data collection, we typically want to transfer data from a mobile device (Smartphone or Smartwatch) to a Server or PC. Follow the instructions of this section on the device you want to receive data. If you want to send data between two mobile devices, you can skip to the next section. Otherwise, the current section will guide you through the setup of a new CLAID application on a Linux, macOS, or Windows device to receive incoming data. Make sure that you have installed the necessary prerequisites.
The first step to do is to create a new application on your target device (i.e., on the server). For this, we have to create a new application project. You can use the CLAID manager to do so, assuming you have set it up during tutorial series 1. To create a new project, see the instructions below. This will create a new CLAID application folder called "DataReceiverApplication". Alternatively, you can choose any name you like.
Creating a new Android Project using CLAID manager
On Linux and macOS:
On Windows (do not use PowerShell!):Instead of "DataReceiverApplication", you can also specify a path like /path/to/your/folder/DataReceiverApplication etc.
Note, that here we are creating a regular project, which will be based on C++. This Tutorial involves no programming, so the choice of the main language for the CLAID project does not really matter. However, if you want to use this as a starting point for your own application, and you prefer to use another main language such as Java or Python, you can finde more information in Tutorial Series 03.
After having created the project, navigate with your terminal into the project folder
Now, you can use the CLAID manager to build and run the application:The output should look as follows:
tutorial$ claid create "DataReceiverApplication"
Application "DataReceiverApplication" was created successfully
tutorial$ claid build && claid run
[... build output]
Hello world from CLAID!
Configuring the application to receive incoming data¶
The NetworkServerModule¶
For remote communication, CLAID follows principles of transparent computing. In a nutshell, this means that you can connect devices in any desired topology, and they are integrated into one logical system, where Modules can communicate and interact with each other as if they were running on the same device, transparently sending data across the network border between them. However, network communication is still based on TCP/IP. Inherently, we still need to have some devices acting as a Server, and other devices connecting to this Server as Clients.
To realize this, with CLAID we provide the NetworkServerModule and the NetworkClientModule respectively. You can load and include them in your configuration, just like any other Module. You can also load multiple instances of them, letting your application act as one or multiple Servers and/or Clients at the same time. To receive incoming data, we will let the application that we created in the previous step act as Server. Open the CLAID.xml inside the project directory that you have created before (i.e., the DataReceiverApplication folder), and insert the following content:
CLAID.xml with NetworkServerModule
With this, our application is now ready to accept incoming connections on Port 1337. You can, of course, choose any port you like.
The DataReceiverModule¶
Using the NetworkServerModule allows other devices to connect as Clients. In order to exchange data, we have to run the DataReceiverModule on the Server, which will receive data from a DataSyncModule running on the Client (see next section). Hence, we have to integrate it into our configuration aswell. The DataReceiverModule is part of the DataCollection package. Make sure you have installed the package as shown in the previous tutorial. Afterward, adapt your configuration as follows:
CLAID.xml for data receiver application
The DataReceiverModule has a property called "storagePath". Here, you can specify a folder in which to store incoming data from the connected device. We advise to store data from each connected device in an own directory. You can use the "%user_id" identifier, which will be resolved by the DataReceiverModule to the user id specified by the DataSyncModule on the device, as you will see in the next section.
2. Configuring the (mobile device) Client to send data¶
Having set up the Server in the previous section, we can now configure the device that we would like to send data from. We assume that you continued from the previous Tutorial. Hence, you can do the following on the device that you used to collect data in the previous tutorial.
The NetworkClientModule¶
As pendant to the NetworkServerModule, we can use the NetworkClientModule to connect to a Server, using an IP and port:
CLAID.xml for data receiver application
In this configuration, you have to substitute "your_server_ip" with the IP address of the device on which you have set up the NetworkServerModule and DataReceiverModules above. We refer to this device as "Server" in the following. For the port, we use the number specified for the NetworkServerModule before.
If the Server is in the same network: Use the local IP of the device the server is running on. In the easiest case, you can find that using the CLAID manager on your PC or Server:
If that does not work, you have to manually use ifconfig on Linux and macOS or ipconfig on Windows.If the Server is running externally: You have to use the public IP of your device. If you only know the domain name, you can use a DNS lookup to find out the IP address.
The DataSyncModule¶
While the NetworkClientModule is the pendant to the NetworkServerModule and allows a Client to connect to a Server, the DataSyncModule is the pendant to the DataReceiverModule. It allows you to send files available in a certain directory to the DataReceiverModule. Just as the DataReceiverModule, the DataSyncModule is available via the DataCollection package. When the package is installed, you can adapt the configuration to include the Module as follows:
CLAID.xml for device sending data
<Module class="claid::Network::NetworkClientModule">
<ConnectTo>your_server_ip:1337</ConnectTo>
</Module>
<Module class="claid::DataSyncModule">
<!-- Path to a folder containing the data you want to upload -->
<filePath>/sdcard/CLAIDTutorial/</filePath>
<!-- Upload data every 5 seconds -->
<syncingPeriodInMs>5000</syncingPeriodInMs>
<!-- Do NOT delete the local files after synchronization -->
<deleteFileAfterSync>false</deleteFileAfterSync>
</Module>
The DataSyncModule has 4 important properties that you can specify via the configuration:
- filePath: Path to the folder containing the files that you want to synchronize with the Server.
- syncingPeriodInMs: Time period for synchronization of files. Files will be synchronized after the amount of milliseconds specified by this property (for example, every 5 seconds).
- deleteFileAfterSync: If set to true, every file successfully synchronized with the Server will be deleted locally.
Exceuting the configuration¶
Having now set up the configurations on both the Server and Client, we can start the CLAID application on both.
On the Server: Start your application using the CLAID manager:
On the mobile device: Execute the application as done in Tutorial 2.3, for example via Android Studio or Xcode.
After starting both applications, they should establish a connection and synchronize data (after the time intervall specified for the DataSyncModule). On the Server, you should see the following in the terminal:
tutorial$ claid run
tutorial$ Waiting for incoming connections
tutorial$ Device connected via IP ....
Cannot establish a connection?
If the devices you want to connect with each other are not in the same physical network (e.g., same Wifi) as the device from which you want to connect, you might have to open ports and add firewall rules for the device running the CLAID application acting as Server. You might have to set up port forwarding, if you connect externally.
Also, we found that some Android or iOS Emulators sometimes also have problems with the firewall rules, making it challenging to connect to CLAID running on the Emulator from outside.
Special note for macOS: If you are running on macOS, sometimes it doesn't work due to some firewall limitations. When you start CLAID on your device, macOS should show a dialog and ask you whether you want to allow your CLAID application to accept incoming connections. Sometimes, however, macOS does not show this dialog. We found that if we put a CLAID application folder on the Desktop under macOS, the OS will not show the firewall dialog. Do not put a CLAID application/project folder on the Desktop under macOS.