Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit 2931cb6

Browse files
author
Konstantina Chremmou
committed
Merge pull request #12 from kc284/master
CP-7265 (SDK work for CAR-1415) and CA-124147.
2 parents 4319e15 + 4d72999 commit 2931cb6

File tree

8 files changed

+203
-35
lines changed

8 files changed

+203
-35
lines changed

java/lib/com/xensource/xenapi/Connection.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ public class Connection
5858

5959
private APIVersion apiVersion;
6060

61-
protected int _wait = 600;
61+
/**
62+
* Reply timeout for xml-rpc calls. The default value is 10 minutes.
63+
*/
64+
protected int _replyWait = 600;
65+
66+
/**
67+
* Connection timeout for xml-rpc calls. The default value is 5 seconds.
68+
*/
69+
protected int _connWait = 5;
6270

6371
/**
6472
* Updated when Session.login_with_password() is called.
@@ -135,7 +143,7 @@ public Connection(String client, String username, String password) throws java.n
135143
}
136144

137145
/**
138-
* Creates a connection to a particular server using a given username and password. This object can then be passed
146+
* Creates a connection to a particular server using a given url. This object can then be passed
139147
* in to any other API calls.
140148
*
141149
* Note this constructor does NOT call Session.loginWithPassword; the programmer is responsible for calling it,
@@ -149,16 +157,29 @@ public Connection(URL url)
149157
deprecatedConstructorUsed = false;
150158
this.client = getClientFromURL(url);
151159
}
152-
153-
public Connection(URL url, int wait)
154-
{
155-
this(url);
156-
_wait = wait;
160+
161+
/**
162+
* Creates a connection to a particular server using a given url. This object can then be passed
163+
* in to any other API calls.
164+
*
165+
* Note this constructor does NOT call Session.loginWithPassword; the programmer is responsible for calling it,
166+
* passing the Connection as a parameter. No attempt to connect to the server is made until login is called.
167+
*
168+
* When this constructor is used, a call to dispose() will do nothing. The programmer is responsible for manually
169+
* logging out the Session.
170+
*
171+
* The parameters replyWait and connWait set timeouts for xml-rpc calls.
172+
*/
173+
public Connection(URL url, int replyWait, int connWait)
174+
{
175+
this(url);
176+
_replyWait = replyWait;
177+
_connWait = connWait;
157178
}
158-
179+
159180

160181
/**
161-
* Creates a connection to a particular server using a given username and password. This object can then be passed
182+
* Creates a connection to a particular server using a given url. This object can then be passed
162183
* in to any other API calls.
163184
*
164185
* The additional sessionReference parameter must be a reference to a logged-in Session. Any method calls on this
@@ -174,6 +195,24 @@ public Connection(URL url, String sessionReference)
174195
this.sessionReference = sessionReference;
175196
}
176197

198+
/**
199+
* Creates a connection to a particular server using a given url. This object can then be passed
200+
* in to any other API calls.
201+
*
202+
* The additional sessionReference parameter must be a reference to a logged-in Session. Any method calls on this
203+
* Connection will use it. This constructor does not call Session.loginWithPassword, and dispose() on the resulting
204+
* Connection object does not call Session.logout. The programmer is responsible for ensuring the Session is logged
205+
* in and out correctly.
206+
*
207+
* The parameters replyWait and connWait set timeouts for xml-rpc calls.
208+
*/
209+
public Connection(URL url, String sessionReference, int replyWait, int connWait)
210+
{
211+
this(url, sessionReference);
212+
_replyWait = replyWait;
213+
_connWait = connWait;
214+
}
215+
177216
protected void finalize() throws Throwable
178217
{
179218
dispose();
@@ -259,14 +298,14 @@ private static String loginWithPassword(XmlRpcClient client, String username, St
259298

260299
public XmlRpcClientConfigImpl getConfig()
261300
{
262-
return config;
301+
return config;
263302
}
264303
private XmlRpcClient getClientFromURL(URL url)
265304
{
266305
config.setTimeZone(TimeZone.getTimeZone("UTC"));
267306
config.setServerURL(url);
268-
config.setReplyTimeout(_wait * 1000);
269-
config.setConnectionTimeout(5000);
307+
config.setReplyTimeout(_replyWait * 1000);
308+
config.setConnectionTimeout(_connWait * 1000);
270309
XmlRpcClient client = new XmlRpcClient();
271310
client.setConfig(config);
272311
return client;
@@ -330,7 +369,7 @@ else if (method_call.equals("session.logout"))
330369
new Connection(new URL(client_url.getProtocol(),
331370
(String)error[1],
332371
client_url.getPort(),
333-
client_url.getFile()), _wait);
372+
client_url.getFile()), _replyWait, _connWait);
334373
tmp_conn.sessionReference = sessionReference;
335374
try
336375
{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) Citrix Systems, Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1) Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2) Redistributions in binary form must reproduce the above
13+
* copyright notice, this list of conditions and the following
14+
* disclaimer in the documentation and/or other materials
15+
* provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.xensource.xenapi;
32+
33+
import java.util.Set;
34+
35+
/**
36+
* Class used to map the output of Event.from().
37+
*
38+
* @author Citrix Systems, Inc.
39+
*/
40+
public class EventBatch
41+
{
42+
/**
43+
* The events returned by Event.from().
44+
*/
45+
public Set<Event.Record> events;
46+
47+
/**
48+
* The number of valid objects of all types in the database.
49+
*/
50+
public Object validRefCounts;
51+
52+
/**
53+
* A token that can be used in subsequent calls of Event.from().
54+
* It represents the point from which to generate database events.
55+
*/
56+
public String token;
57+
}

java/main.ml

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,26 @@ let get_method_params_for_xml message =
247247

248248

249249
let gen_method_return_cast message = match message.msg_result with
250-
| None ->
251-
sprintf ""
252-
| Some (ty, _) ->
253-
sprintf " Types.%s(result)" (get_marshall_function ty);;
250+
| None -> sprintf ""
251+
| Some (ty, _) -> sprintf " Types.%s(result)" (get_marshall_function ty);;
254252

255-
let gen_method_return file message =
256-
fprintf file " return%s;\n" (gen_method_return_cast message);;
253+
let gen_method_return file cls message =
254+
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then
255+
fprintf file " return Types.toEventBatch(result);\n"
256+
else
257+
fprintf file " return%s;\n" (gen_method_return_cast message)
257258

258259
let rec range = function
259260
| 0 -> []
260261
| i -> (range (i-1)) @ [i]
261262

262263

263264
(* Here is the main method generating function.*)
264-
let gen_method file message async_version =
265+
let gen_method file cls message async_version =
265266
let deprecated_string = get_method_deprecated_string message in
266-
let return_type = get_java_type_or_void message.msg_result in
267+
let return_type =
268+
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then "EventBatch"
269+
else get_java_type_or_void message.msg_result in
267270
let method_static = get_method_static_string message in
268271
let method_name = camel_case message.msg_name in
269272
let method_params = get_method_params_for_signature message in
@@ -295,7 +298,7 @@ let gen_method file message async_version =
295298
fprintf file " */\n";
296299

297300
if async_version then
298-
fprintf file " %s public %s%s %s(%s) throws\n" deprecated_string method_static "Task" (method_name^"Async") method_params
301+
fprintf file " %s public %sTask %s(%s) throws\n" deprecated_string method_static (method_name^"Async") method_params
299302

300303
else
301304
fprintf file " %s public %s%s %s(%s) throws\n" deprecated_string method_static return_type method_name method_params;
@@ -341,19 +344,19 @@ let gen_method file message async_version =
341344
fprintf file " return Types.toTask(result);\n" )
342345
else (
343346
match message.msg_result with
344-
| None ->
345-
fprintf file " return;\n"
346-
| Some _ ->
347-
fprintf file " Object result = response.get(\"Value\");\n";
348-
gen_method_return file message
347+
| None -> fprintf file " return;\n"
348+
| Some _ -> fprintf file " Object result = response.get(\"Value\");\n";
349+
gen_method_return file cls message
349350
);
350351

351352
fprintf file " }\n\n"
352353

353354
(* Here is the main method generating function.*)
354-
let gen_versioned_method file message versioned_message async_version =
355+
let gen_versioned_method file cls message versioned_message async_version =
355356
let deprecated_string = get_method_deprecated_string message in
356-
let return_type = get_java_type_or_void message.msg_result in
357+
let return_type =
358+
if (String.lowercase cls.name) = "event" && (String.lowercase message.msg_name) = "from" then "EventBatch"
359+
else get_java_type_or_void message.msg_result in
357360
let method_static = get_method_static_string message in
358361
let versioned_name = camel_case versioned_message.msg_name in
359362
let method_params = get_method_params_for_signature versioned_message in
@@ -409,7 +412,7 @@ let gen_versioned_method file message versioned_message async_version =
409412
if async_version then
410413
fprintf file " return Types.toTask(result);\n"
411414
else
412-
gen_method_return file message;
415+
gen_method_return file cls message;
413416

414417
fprintf file " }\n\n"
415418
;;
@@ -418,8 +421,8 @@ let gen_versioned_method file message versioned_message async_version =
418421
(*Some methods have an almost identical asynchronous counterpart, which returns*)
419422
(* a Task reference rather than its usual return value*)
420423
let gen_method_and_asynchronous_counterpart file cls message =
421-
if message.msg_async then gen_method file message true;
422-
gen_method file message false;;
424+
if message.msg_async then gen_method file cls message true;
425+
gen_method file cls message false;;
423426

424427
(* Generate the record *)
425428

@@ -1038,6 +1041,19 @@ public class Types
10381041
TypeSet.iter (gen_marshall_func file) !types;
10391042
fprintf file "\n";
10401043
TypeSet.iter (gen_task_result_func file) !types;
1044+
fprintf file "
1045+
public static EventBatch toEventBatch(Object object) {
1046+
if (object == null) {
1047+
return null;
1048+
}
1049+
1050+
Map map = (Map) object;
1051+
EventBatch batch = new EventBatch();
1052+
batch.token = toString(map.get(\"token\"));
1053+
batch.validRefCounts = map.get(\"valid_ref_counts\");
1054+
batch.events = toSetOfEventRecord(map.get(\"events\"));
1055+
return batch;
1056+
}";
10411057
fprintf file "}\n"
10421058

10431059
(* Now run it *)

java/samples/EventMonitor.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,60 @@
3232
import java.util.Set;
3333

3434
import com.xensource.xenapi.Event;
35+
import com.xensource.xenapi.EventBatch;
3536

3637
/**
3738
* Listens for events on a connection and prints each event out as it is received.
3839
*/
3940
public class EventMonitor extends TestBase
4041
{
4142
private static final int MAX_EVENTS = 100;
43+
private static final double TIMEOUT_SEC = 30;
4244
private static final int TIMEOUT = 30 * 1000;
4345

46+
public static void RunNewTest(ILog logger, TargetServer server) throws Exception
47+
{
48+
TestBase.logger = logger;
49+
try
50+
{
51+
connect(server);
52+
53+
Set<String> everything = new HashSet<String>();
54+
everything.add("*");
55+
Event.register(connection, everything);
56+
57+
int eventsReceived = 0;
58+
long started = System.currentTimeMillis();
59+
String token = "";
60+
61+
while (eventsReceived < MAX_EVENTS && System.currentTimeMillis() - started < TIMEOUT)
62+
{
63+
EventBatch eventBatch = Event.from(connection, everything, token, TIMEOUT_SEC);
64+
announce(eventBatch.events.size() + " event(s) received");
65+
66+
// print the events out in a nice format
67+
String format = "%10s %5s %3s %10s %50s";
68+
logf(format + " date time%n", "class", "id", "uuid", "operation", "reference");
69+
for (Event.Record e : eventBatch.events)
70+
{
71+
logf(format, e.clazz, e.id, e.objUuid, e.operation, e.ref);
72+
logf(" %te/%<tm/%<tY %<tH.%<tM.%<tS %n", e.timestamp);
73+
logln("associated snapshot: " + e.snapshot);
74+
}
75+
eventsReceived += eventBatch.events.size();
76+
}
77+
}
78+
catch (Exception e)
79+
{
80+
logln("exception!");
81+
}
82+
finally
83+
{
84+
disconnect();
85+
}
86+
}
87+
88+
@Deprecated
4489
public static void RunTest(ILog logger, TargetServer server) throws Exception
4590
{
4691
TestBase.logger = logger;

java/samples/Https.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean verify(String hostname, SSLSession session)
7777
connection = new Connection(url);
7878

7979
// Log in
80-
Session.loginWithPassword(connection, server.Username, server.Password, "1.3");
80+
Session.loginWithPassword(connection, server.Username, server.Password, "1.3", "testRunner");
8181

8282
// Print a record
8383
for (Host host : Host.getAllRecords(connection).keySet())

java/samples/RunTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ public static void main(String[] args)
121121

122122
logln("RunTests.java: test run started at " + new Date().toString());
123123

124+
testName = "EventMonitor";
125+
try
126+
{
127+
testStart();
128+
EventMonitor.RunNewTest(textLogger, server);
129+
testSuccess();
130+
} catch (Exception e)
131+
{
132+
testFailure(e);
133+
}
134+
124135
testName = "AddNetwork";
125136
try
126137
{

java/samples/SessionReuse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected static void RunTest(ILog logger, TargetServer server) throws Exception
5757
try
5858
{
5959
// Create a new Session, whose reference is stored in the Connection.
60-
Session.loginWithPassword(connection1, server.Username, server.Password, "1.3");
60+
Session.loginWithPassword(connection1, server.Username, server.Password, "1.3", "testRunner");
6161

6262
// Re-use the Session in a second Connection object
6363
Connection connection2 = new Connection(url, connection1.getSessionReference());

java/samples/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected static void connect(TargetServer target) throws Exception
6363
logln(String.format("logging in to '%s' as '%s' with password '%s'...", target.Hostname, target.Username,
6464
target.Password));
6565
logln("Success");
66-
Session.loginWithPassword(connection, target.Username, target.Password, APIVersion.latest().toString());
66+
Session.loginWithPassword(connection, target.Username, target.Password, APIVersion.latest().toString(), "testRunner");
6767
logln(String.format("Session API version is %s", connection.getAPIVersion().toString()));
6868

6969
connectionName = target.Hostname;

0 commit comments

Comments
 (0)