/*
* Copyrights : CNRS
* Author : Oleg Lodygensky
* Acknowledgment : XtremWeb-HEP is based on XtremWeb 1.8.0 by inria : http://www.xtremweb.net/
* Web : http://www.xtremweb-hep.org
*
* This file is part of XtremWeb-HEP.
*
* XtremWeb-HEP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* XtremWeb-HEP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XtremWeb-HEP. If not, see .
*
*/
package xtremweb.client;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Collection;
import java.util.Iterator;
import xtremweb.common.AppInterface;
import xtremweb.common.CommandLineOptions;
import xtremweb.common.CommandLineParser;
import xtremweb.common.DataInterface;
import xtremweb.common.Logger;
import xtremweb.common.MD5;
import xtremweb.common.MileStone;
import xtremweb.common.StatusEnum;
import xtremweb.common.UID;
import xtremweb.common.WorkInterface;
import xtremweb.common.XMLValue;
import xtremweb.common.XMLVector;
import xtremweb.common.XWConfigurator;
import xtremweb.common.XWReturnCode;
import xtremweb.common.XWTools;
import xtremweb.communications.CommClient;
import xtremweb.communications.URI;
import xtremweb.communications.XMLRPCCommandGetApps;
import xtremweb.communications.XMLRPCCommandRemove;
import xtremweb.communications.XMLRPCCommandSend;
/**
* Created: May 23rd, 2014
*
* This class implements API usage to retrieve applications list, as example
* Usage: java -cp xtremweb.jar xtremweb.client.HelloWorld --xwconfig
* xtremweb.client.conf
*
* @author Oleg Lodygensky
* @since 9.0.0
*/
public final class HelloWorld {
/**
* This is the logger
*
* @since 7.0.0
*/
private final Logger logger;
/**
* This stores and tests the command line parameters
*/
private final CommandLineParser args;
/**
* This stores client config such as login, password, server addr etc.
*/
private XWConfigurator config;
/**
* This is the default constructor
*/
private HelloWorld(final String[] a) throws ParseException {
final String[] argv = a.clone();
logger = new Logger(this);
config = null;
args = new CommandLineParser(argv);
try {
config = (XWConfigurator) args.getOption(CommandLineOptions.CONFIG);
} catch (final NullPointerException e) {
logger.exception(e);
if (args.getOption(CommandLineOptions.GUI) == null) {
logger.fatal("You must provide a config file, using \"--xwconfig\" !");
} else {
new MileStone(XWTools.split(new String()));
}
}
}
/**
* This retrieves and initializes the default communication client
*
* @return the default communication client
* @exception IOException
* is thrown if cache directory can not be created or if we
* can't retrieve the default client
* @throws InstantiationException
*/
public CommClient commClient() throws IOException {
try {
final CommClient client = config.defaultCommClient();
client.setLoggerLevel(logger.getLoggerLevel());
client.setAutoClose(false);
return client;
} catch (final Exception e) {
throw new IOException(e);
}
}
/**
* This prints a message to std err and exits.
*
* @param msg
* is the message to print to stderr
* @param code
* is the return code to use on exit
*/
private void exit(String msg, XWReturnCode code) {
try {
final CommClient client = commClient();
client.setAutoClose(true);
client.close();
} catch (final Exception e) {
}
}
/**
* This first retrieves registered applications; then submit a job for the
* 1st found application.
*/
private void execute() throws IOException {
try {
final CommClient client = commClient();
final URI uri = client.newURI();
//
// retrieve register applications
//
final XMLRPCCommandGetApps cmd = new XMLRPCCommandGetApps(uri,
config.getUser());
final XMLVector xmluids = (XMLVector) cmd.exec(client);
final Collection uids = xmluids.getXmlValues();
if ((uids == null) || (uids.isEmpty())) {
logger.warn("no application found");
return;
}
UID appUid = null;
String appName = null;
final Iterator theEnum = uids.iterator();
while (theEnum.hasNext()) {
final UID uid = (UID) theEnum.next().getValue();
if (appUid == null) {
appUid = uid;
}
try {
final AppInterface app = (AppInterface) client.get(uid);
if (appName == null) {
appName = app.getName();
}
logger.info(app.toXml());
} catch (final ClassCastException cce) {
cce.printStackTrace();
}
}
//
// submit new work
//
WorkInterface work = new WorkInterface();
work.setUID(new UID());
work.setApplication(appUid);
//
// if there any input file?
// here, as example, let suppose input file is name 'input.zip'
// but of course, we could programmatically create a Zip file here,
// if for example, 'input' is a directory
//
final File dataFile = new File("input.zip");
if (dataFile.exists()) {
final DataInterface data = new DataInterface(new UID());
final URI dataUri = commClient().newURI(data.getUID());
data.setStatus(StatusEnum.UNAVAILABLE);
data.setSize(dataFile.length());
data.setMD5(MD5.asHex(MD5.getHash(dataFile)));
//
// 1st, send data definition
//
logger.info("Sending data 'input.zip' : " + data.toXml());
final XMLRPCCommandSend cmdSend = new XMLRPCCommandSend(
dataUri, data);
cmdSend.exec(client);
//
// then send data content
//
commClient().uploadData(dataUri, dataFile);
work.setDirin(dataUri);
} else {
logger.info("File not found 'input.zip'");
}
logger.info("Submitting a new work for application '" + appName
+ "' : " + work.toXml());
final XMLRPCCommandSend cmdSend = new XMLRPCCommandSend(uri, work);
cmdSend.exec(client);
logger.info("Submitted " + work.getUID());
//
// wait for job completion
//
work = commClient().waitForCompletedWork(work.getUID());
if (work.getResult() != null) {
logger.info("Downloading results");
final File fileResult = new File("myresult");
client.downloadData(work.getResult(), fileResult);
}
logger.info("Removing work from server");
final URI workURI = client.newURI(work.getUID());
final XMLRPCCommandRemove cmdRemove = new XMLRPCCommandRemove(
workURI);
cmdRemove.exec(client);
logger.info("Disconnecting");
client.disconnect();
} catch (final Exception e) {
e.printStackTrace();
exit(e.getMessage(), XWReturnCode.CONNECTION);
}
}
/**
* This is the standard main method
*/
public static void main(String[] argv) {
try {
new HelloWorld(argv).execute();
} catch (final IOException e) {
e.printStackTrace();
} catch (final ParseException e) {
e.printStackTrace();
}
}
}