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

All the above examples, along with others, can be downloaded
here.
A test web-page, based on the PHP examples, is available
here.