INTERCLOUD

  • Home
  • About
  • Protocol
  • Gateway
    • Intercloud Operationvs. Client-cloud Operation
    • Architecture
    • Security Measures
  • Installation
    • Download
    • The Pre-Requisites and Tools
    • Configuration Procedures
    • Running the Gateway
  • Development
    • The Pre-Defined API Classes
    • Invoking an API Method UsingJSON(Object Storage)
    • Invoking an API Method UsingJSON(Virtual Machine)
    • Developing a Native Java Application
  • Mobile Intercloud
    • Blocks

Development

This section presents how to develop software programs for Intercloud data communication with the help of the Gateway efficiently. A number of examples are provided as demonstrations.

Note: Before continuing on the development of Intercloud applications, please be reminded to review the sections of Intercloud operation vs. Client-cloud operation and Security Measures.

The Pre-defined API Classes

There are pre-defined API classes in the package hk.edu.polyu.intercloud.api.

  • ObjectStorageAPI serves as an API for object storage service.
  • VirtualMachineAPI serves as an API for VM service.
  • CheckStatusAPI is used to check the status of requests.

Please refer to the JavaDocs for further details.

Invoking an API method using JSON (Object Storage)

The Gateway continuously listens to TCP port 2001 for request messages during runtime. Developers can create an application (whether in Java, PHP, Node.js, etc.) to invoke the pre-definded API methods by sending messages through Java socket. The messages are in JSON format and reflect to the API classes and methods mentioned here. The following is an example:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "put",
    "Parameters": [
        "Apple.jpg",
        "HTTP",
        true,
        "Private",
        true
    ]
}

The following is the explanation of the elements:

  • API: The name of the API class to be reflected.
  • Target (optional): The parameter “String cloud” of the API class constructor. If you are constructing a class without such parameter, do not specify this element in the message.
  • Method: The name of the method to be invoked.
  • Parameters: An array of the method parameters.

Hence essentially, this message reflects to the Object Storage API. It encrypts and stores Apple.jpg to iccp9.iccp.cf via HTTP by a signed protocol message, overwriting all previous versions.

After sending the above message to the Gateway, it returns a request ID for tracking and performs the requested operation (i.e. putting an object). You can use the ID to check the status of the operation by the getStatus(…) method in the CheckStatusAPI class.

By importing the org.json library, you can develop simple applications to instruct the Gateway to perform various operations.

Example 1: SocketUtil in Java
public class SocketUtil {
    public static String sendMessage(String listener, JSONObject json)
            throws UnknownHostException, IOException {
        int port = 2001;
        String response = null, read = null;
        Socket socket = new Socket(listener, port);
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println(json.toString());
        System.out.println("$ [-> " + listener + ":" + port + "] " + json.toString(4));
        while ((read = in.readLine()) != null) {
            response = read;
        }
        System.out.println("$ [<- " + listener + ":" + port + "] " + response);
        out.flush();
        out.close();
        in.close();
        socket.close();
        return response;
    }
}

Let’s build an utility class for the communication with the Gateway first. The above is an example, named as SocketUtil.  The sendMessage(…) method takes the parameter json, which is the request message, and send it to the listener, which is the IP address of the Gateway. It then gets the response from the Gateway and return it as a String.

Example 2: Simple put object in Java
public class PutObject {

    static String listener = "iccp1.iccp.cf";
    static String targetCloud = "iccp3.iccp.cf";
    static String object = "Banana.jpg";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "ObjectStorageAPI");
        json.put("Target", targetCloud);
        json.put("Method", "put");
        String str = "[\"" + object + "\",\"HTTPS\",true,\"Public\",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp1.iccp.cf to store Banana.jpg to iccp3.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp1.iccp.cf:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp3.iccp.cf",
    "Method": "put",
    "Parameters": [
        "Banana.jpg",
        "HTTPS",
        true,
        "Public",
        true
    ]
}

When iccp1.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the put method) to store the object Banana.jpg to iccp3.iccp.cf via HTTPS, overwriting all previous versions. The application gets the request ID from the response from iccp1.iccp.cf and store it as a String.

Example 3: Simple get object in Java
public class GetObject {

    static String listener = "iccp1.iccp.cf";
    static String targetCloud = "iccp3.iccp.cf";
    static String object = "Banana.jpg";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "ObjectStorageAPI");
        json.put("Target", targetCloud);
        json.put("Method", "get");
        String str = "[\"" + object + "\",\"HTTPS\",true,\"Public\"]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp1.iccp.cf to retrieve the latest version of Banana.jpg from iccp3.iccp.cf, which is very similar to Example 2. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp1.iccp.cf:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp3.iccp.cf",
    "Method": "get",
    "Parameters": [
        "Banana.jpg",
        "HTTPS",
        true,
        "Public"
    ]
}

When iccp1.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the get method) to retrieve the object Banana.jpg from iccp3.iccp.cf via HTTPS. The application gets the request ID from the response from iccp1.iccp.cf and store it as a String.

Example 4: List objects in Java
public class ListObject {

    static String listener = "iccp1.iccp.cf";
    static String targetCloud = "iccp3.iccp.cf";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "ObjectStorageAPI");
        json.put("Target", targetCloud);
        json.put("Method", "list");
        String str = "[true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp1.iccp.cf to query for a list of object stored in iccp3.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp1.iccp.cf:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp3.iccp.cf",
    "Method": "list",
    "Parameters": [
        true
    ]
}

When iccp1.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the list method) to query for a list of object stored in iccp3.iccp.cf. The application gets the request ID from the response from iccp1.iccp.cf and store it as a String.

Example 5: Check the status of a request in Java
public class CheckStatus {

    static String listener = "iccp1.iccp.cf";
    static long id = 4166973035500L;

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "CheckStatusAPI");
        json.put("Method", "checkStatus");
        json.put("ID", id);
        String response = SocketUtil.sendMessage(listener, json);
        System.out.println(response);
    }
}

The above is an example application to check the status of a request with the Gateway at iccp1.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp1.iccp.cf:

{
    "API": "CheckStatusAPI",
    "Target": "iccp3.iccp.cf",
    "Method": "checkStatus",
    "ID": 4166973035500
}

When iccp1.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the checkStatus method) to retrieve the status of the request with ID 4166973035500. If the operation of the request has been successfully performed, iccp1.iccp.cf responses the application with a JSON-formatted ICCP response message; otherwise it responses with an error message. The application gets the response message from iccp1.iccp.cf and print it out.

Apart from Java, you may develop applications in other languages, such that they are able to communicate with the Gateway. Here are some examples written in PHP.

Example 6: Simple put object in PHP
<?php

$address = $_GET["from"]; // The working Gateway
$port = 2001; // The default client socket port
// Build the JSON request message
$params_array = array($_GET["obj"], "HTTPS", true, "Public", true);
$msg_array = array(
    "API" => "ObjectStorageAPI",
    "Target" => $_GET["to"], // The target Gateway (destination of put)
    "Method" => "put",
    "Parameters" => $params_array
);
$msg = json_encode($msg_array);
var_dump($msg);

try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $connect = socket_connect($socket, $address, $port);
    $write = socket_write($socket, $msg, strlen($msg)); // Send message
    if (!$socket || !$connect || !$write) {
        echo "Failed to send the request. Reason: " . socket_strerror(socket_last_error($socket));
    } else {
        echo "Request sent.";
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

The above is an example script to request a Gateway to store an object to another cloud, written in PHP. The script gets 3 input attributes (from, obj and to), generates a request message in JSON and sends to the working Gateway.

For example, if we run put.php?from=iccp1.iccp.cf&obj=Orange.jpg&to=iccp7.iccp.cf, the script will generate the following message:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp7.iccp.cf",
    "Method": "put",
    "Parameters": [
        "Orange.jpg",
        "HTTPS",
        true,
        "Public",
        true
    ]
}

When iccp1.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the put method) to store the object Orange.jpg to iccp7.iccp.cf via HTTPS, overwriting all previous versions.

Example 7: Simple get object in PHP
<?php

$address = $_GET["to"]; // The working Gateway
$port = 2001; // The default client socket port
// Build the JSON request message
$params_array = array($_GET["obj"], "HTTPS", true, "Public");
$msg_array = array(
    "API" => "ObjectStorageAPI",
    "Target" => $_GET["from"], // The target Gateway (origin of get)
    "Method" => "get",
    "Parameters" => $params_array
);
$msg = json_encode($msg_array);
var_dump($msg);

try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $connect = socket_connect($socket, $address, $port);
    $write = socket_write($socket, $msg, strlen($msg)); // Send message
    if (!$socket || !$connect || !$write) {
        echo "Failed to send the request. Reason: " . socket_strerror(socket_last_error($socket));
    } else {
        echo "Request sent.";
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

The above is an example script to request a Gateway to retrieve the latest version of an object from another cloud, which is very similar to Example 6. The script gets 3 input attributes (from, obj and to), generates a request message in JSON and sends to the working Gateway.

For example, if we run get.php?from=iccp7.iccp.cf&obj=Banana.jpg&to=iccp1.iccp.cf, the script will generate the following message:

{
    "API": "ObjectStorageAPI",
    "Target": "iccp7.iccp.cf",
    "Method": "get",
    "Parameters": [
        "Banana.jpg",
        "HTTPS",
        true,
        "Public"
    ]
}

When iccp1.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the get method) to retrieve the latest version of the object Banana.jpg from iccp7.iccp.cf via HTTPS.

Download

download_2

All the above examples, along with others, can be downloaded here.

A test web-page, based on the PHP examples, is available here.

Invoking an API Method using JSON (Virtual Machine)

Similar to the case of object storage, we can also invoke the pre-definded Virtual Machine API methods by sending JSON messages. The messages reflect to the API classes and methods mentioned here. The following is an example:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "createVM",
    "Parameters": [
        "TestWin",
        2,
        10,
        1,
        true
    ]
}

The following is the explanation of the elements:

  • API: The name of the API class to be reflected.
  • Target (optional): The parameter “String cloud” of the API class constructor. If you are constructing a class without such parameter, do not specify this element in the message.
  • Method: The name of the method to be invoked.
  • Parameters: An array of the method parameters.

Hence essentially, this message reflects to the Virtual Machine API. It requests iccp9.iccp.cf to create a VM named TestWin, with 2GB memory, 10GB hard disk and 1 CPU, in iccp9.iccp.cf by a signed protocol message.

After sending the above message to the Gateway, it returns a request ID for tracking and performs the requested operation (i.e. putting an object). You can use the ID to check the status of the operation by the getStatus(…) method in the CheckStatusAPI class.

By importing the org.json library, you can develop simple applications to instruct the Gateway to perform various operations.

Note: The following examples use the SocketUtil (mentioned in the object storage section) to communicate with the Gateway.

Example 8: Create a VM in Java
public class CreateVM {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "kate9.iccp.cf";
    static final String vmname = "CentOS";
    static final int memory = 2;
    static final int disksize = 10;
    static final int cpu = 1;

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "createVM");
        String str = "[\"" + vmname + "\"," + memory + "," + disksize + ","
                + cpu + ",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to issue a request to iccp9.iccp.cf to create a VM named CentOS with 2GB memory, 10GB hard disk and 1 CPU for it. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "createVM",
    "Parameters": [
        "TestWin",
        2,
        10,
        1,
        true
    ]
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the createVM method) to issue a request to iccp9.iccp.cf to create a VM named CentOS with 2GB memory, 10GB hard disk and 1 CPU for it. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 9: Simple put VM in Java
public class PutVM {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "iccp9.iccp.cf";
    static final String vmname = "Win2012";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "putVM");
        String str = "[\"" + vmname + "\",\"HTTPS\",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to store a VM Win2012 to iccp9.iccp.cf to host it. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "putVM",
    "Parameters": [
        "Win2012",
        "HTTPS",
        true
    ],
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the putVM method) to store the VM Win2012 to iccp9.iccp.cf via HTTPS to host it. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 10: Simple get VM in Java
public class GetVM {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "iccp9.iccp.cf";
    static final String vmname = "Win2012";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "getVM");
        String str = "[\"" + vmname + "\",\"HTTPS\",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to retrieve the latest version of the VM Win2012 from iccp9.iccp.cf, which is very similar to Example 9. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "getVM",
    "Parameters": [
        "Win2012",
        "HTTPS",
        true
    ],
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the getVM method) to retrieve the VM Win2012 from iccp9.iccp.cf via HTTPS and host it. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 11: List VM in Java
public class ListVM {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "iccp9.iccp.cf";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "listVM");
        String str = "[true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to retrieve a list of hosted VMs from iccp9.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "listVM",
    "Parameters": [true],
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the listVM method) to retrieve a list of hosted VMs from iccp9.iccp.cf. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 12: Get VM details in Java
public class GetVMDetails {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "iccp9.iccp.cf";
    static final String vmname = "Win2012";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "getVMDetails");
        String str = "[\"" + vmname + "\",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to get the details of the VM Win2012 from iccp9.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "getVMDetails",
    "Parameters": [
        "Win2012",
        true
    ],
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the getVMDetails method) to retrieve the details of the VM Win2012 from iccp9.iccp.cf. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 13: Control the power of a VM in Java
public class PowerControl {

    static final String listener = "iccp8.iccp.cf";
    static final String targetCloud = "iccp9.iccp.cf";
    static final String vmname = "Win2012";
    static final String power = "on";

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "VirtualMachineAPI");
        json.put("Target", targetCloud);
        json.put("Method", "powerControl");
        String str = "[\"" + vmname + "\",\"" + power + "\",true]";
        JSONArray ja = new JSONArray(str);
        json.put("Parameters", ja);
        String response = SocketUtil.sendMessage(listener, json);
    }
}

The above is an example application to request the Gateway at iccp8.iccp.cf to turn on Win2012 at iccp9.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp8.iccp.cf:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "powerControl",
    "Parameters": [
        "Win2012",
        "on",
        true
    ],
}

When iccp8.iccp.cf receives the above message, it responses the application with a request ID and invokes the corresponding API method (i.e. the powerControl method) to control the power of Win2012 at iccp9.iccp.cf. The application gets the request ID from the response from iccp8.iccp.cf and store it as a String.

Example 14: Check the status of a request in Java

Note: This example is the same as Example 5 in the object storage section. Skip this example if you have ready read Example 5.

public class CheckStatus {

    static String listener = "iccp1.iccp.cf";
    static long id = 4166973035500L;

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        JSONObject json = new JSONObject();
        json.put("API", "CheckStatusAPI");
        json.put("Method", "checkStatus");
        json.put("ID", id);
        String response = SocketUtil.sendMessage(listener, json);
        System.out.println(response);
    }
}

The above is an example application to check the status of a request with the Gateway at iccp1.iccp.cf. Essentially, the application invokes the sendMessage(…) method in SocketUtil to sends the following message to iccp1.iccp.cf:

{
    "API": "CheckStatusAPI",
    "Target": "iccp3.iccp.cf",
    "Method": "checkStatus",
    "ID": 4166973035500
}

When iccp1.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the checkStatus method) to retrieve the status of the request with ID 4166973035500. If the operation of the request has been successfully performed, iccp1.iccp.cf responses the application with a JSON-formatted ICCP response message; otherwise it responses with an error message. The application gets the response message from iccp1.iccp.cf and print it out.

Apart from Java, you may develop applications in other languages, such that they are able to communicate with the Gateway. Here are some examples written in PHP.

Example 15: Simple put VM in PHP
<?php

$address = $_GET["from"]; // The working Gateway
$port = 2001; // The default client socket port
// Build the JSON request message
$params_array = array($_GET["vm"], "HTTPS", true);
$msg_array = array(
    "API" => "VirtualMachineAPI",
    "Target" => $_GET["to"], // The target Gateway (destination of put)
    "Method" => "putVM",
    "Parameters" => $params_array
);
$msg = json_encode($msg_array);
var_dump($msg);

try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $connect = socket_connect($socket, $address, $port);
    $write = socket_write($socket, $msg, strlen($msg)); // Send message
    if (!$socket || !$connect || !$write) {
        echo "Failed to send the request. Reason: " . socket_strerror(socket_last_error($socket));
    } else {
        echo "Request sent.";
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

The above is an example script to request a Gateway to store an object to another cloud, written in PHP. The script gets 3 input attributes (from, vm and to), generates a request message in JSON and sends to the working Gateway.

For example, if we run put_vm.php?from=iccp8.iccp.cf&vm=Win2012&to=iccp9.iccp.cf, the script will generate the following message:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "putVM",
    "Parameters": [
        "Win2012",
        "HTTPS",
        true
    ]
}

When iccp8.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the putVM method) to store the VM Win2012 to iccp9.iccp.cf via HTTPS.

Example 16: Simple get VM in PHP
<?php

$address = $_GET["to"]; // The working Gateway
$port = 2001; // The default client socket port
// Build the JSON request message
$params_array = array($_GET["vm"], "HTTPS", true, "Public");
$msg_array = array(
    "API" => "VirtualMachineAPI",
    "Target" => $_GET["from"], // The target Gateway (origin of get)
    "Method" => "getVM",
    "Parameters" => $params_array
);
$msg = json_encode($msg_array);
var_dump($msg);

try {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $connect = socket_connect($socket, $address, $port);
    $write = socket_write($socket, $msg, strlen($msg)); // Send message
    if (!$socket || !$connect || !$write) {
        echo "Failed to send the request. Reason: " . socket_strerror(socket_last_error($socket));
    } else {
        echo "Request sent.";
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

The above is an example script to request a Gateway to retrieve a VM from another cloud, which is very similar to Example 15. The script gets 3 input attributes (from, vm and to), generates a request message in JSON and sends to the working Gateway.

For example, if we run get_vm.php?from=iccp9.iccp.cf&vm=Win2012&to=iccp8.iccp.cf, the script will generate the following message:

{
    "API": "VirtualMachineAPI",
    "Target": "iccp9.iccp.cf",
    "Method": "getVM",
    "Parameters": [
        "Win2012",
        "HTTPS",
        true
    ]
}

When iccp8.iccp.cf receives the above message, it invokes the corresponding API method (i.e. the get method) to retrieve the VM Win2012 from iccp9.iccp.cf via HTTPS.

Download

download_2

All the above examples, along with others, can be downloaded here.

A test web-page, based on the PHP examples, is available here.

Developing a Native Java Application

In addition, developers may develop an application on their own and invoke API methods directly. Note that it is essential to import the Gateway codes (hk.edu.polyu.intercloud.main.Main) and start the Gateway by calling Main.main(null). (IMPORTANT!)

Example 17: Simple list object in Java
package hk.edu.polyu.intercloud.examples;

import hk.edu.polyu.intercloud.api.ObjectStorageAPI;
import hk.edu.polyu.intercloud.main.Main;
import java.util.concurrent.TimeUnit;

public class SimpleList {
    static final String TARGET = "iccp2.iccp.cf";

    public static void main(String[] args) {
        try {
            Main.main(null); // IMPORTANT! Run the Gateway!
            ObjectStorageAPI o = new ObjectStorageAPI(TARGET);
            while (true) {
                o.list(false); // Ask TARGET for a list of objects I stored
                TimeUnit.MINUTES.sleep(1); // Sleep for 1 min.
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This is an example of continuous monitoring of the objects stored in the storage cloud via native Java calls. The class queries the storage cloud, iccp2.iccp.cf, every 1 minute.

In the program, the while-loop runs every minute. It invokes the list method in the ObjectStorageAPI to query for a list of the object stored in the cloud at iccp2.iccp.cf.

Example 19: Simple sync in Java
package hk.edu.polyu.intercloud.examples;

import hk.edu.polyu.intercloud.api.ObjectStorageAPI;
import hk.edu.polyu.intercloud.api.ObjectStorageAPI.DATA_SECURITY;
import hk.edu.polyu.intercloud.main.Main;
import hk.edu.polyu.minio.main.MinioClient;
import hk.edu.polyu.minio.main.Result;
import hk.edu.polyu.minio.messages.Item;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class SimpleSync {
    static final String END_POINT = "http://127.0.0.1:9000";
    static final String ACCESS_KEY = "RS0ROQ32X5X0GMIGA1B0";
    static final String SECRET_KEY = "k3ghnKy7oKTD0rC8PJljxJQjKZZsEjtfz5LAiQJR";
    static final String BUCKET_NAME = "intercloud";
    static final String OBJECT_NAME = "Cherry.jpg";
    static final String TARGET = "iccp2.iccp.cf";
    static MinioClient minio;
    static ObjectStorageAPI o;

    public static void main(String[] args) {
        try {
            Main.main(null); // IMPORTANT! Run the Gateway!
            Date lastCheck = new Date();
            minio = new MinioClient(END_POINT, ACCESS_KEY, SECRET_KEY);
            o = new ObjectStorageAPI(TARGET);
            String pid = o.put(OBJECT_NAME, "HTTP", false, DATA_SECURITY.PUBLIC, true);
            while (true) {
                Date lastModified = getLastModified()
                Date now = new Date();
                System.out.println("File last modified at " + lastModified + " [" + now + "]");
                // If the last modification is after the last check time
                if (lastModified.after(lastCheck)) {
                    // Put the object to the target cloud
                    pid = o.put(OBJECT_NAME, "HTTP", false, DATA_SECURITY.PUBLIC, true);
                    System.out.println("Request sent for sync. Protocol ID: " + pid);
                }
                lastCheck = now;
                TimeUnit.MINUTES.sleep(1); // 1 min. of sleep
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Check the last modification time using Minio's API
    static Date getLastModified() throws Exception {
        Iterable<Result<Item>> theObjects = minio.listObjects(BUCKET_NAME);
        for (Result<Item> result : theObjects) {
            Item item = result.get();
            if (item.objectName().equals(OBJECT_NAME))
                return item.lastModified();
        }
        throw new Exception("No such object");
    }
}

This is an example of using the Gateway to sync an object, named Cherry.jpg, in two clouds’ object storage, via native Java calls. The class checks whether the object on the cloud’s Minio object storage server is modified every 1 minute. If it is modified, it will transfer and store the object to the storage cloud at iccp2.iccp.cf.

In the program, the while-loop runs every minute. It gets the last modification time of the object compared to the time when the object is last checked. If the last modification time is later than the last check time, it will invoke the put method in the ObjectStorageAPI to store the object to the cloud at iccp2.iccp.cf via HTTP.

More examples can be found in the package hk.edu.polyu.intercloud.examples.

Intercloud

  • Room QT418, Department of Computing, The Hong Kong Polytechnic University, Hong Kong

  • +852 27667869

  • intercloud@comp.polyu.edu.hk

© 2019 Provision. All rights reserved | Design by W3layouts