package org.hpsearch.demo.CrisisGridServices;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util.Properties;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import org.hpsearch.demo.CrisisGridServices.RunOffModel.Computation;
import org.hpsearch.demo.CrisisGridServices.RunOffModel.MapsGenerator4Visual;
import org.hpsearch.demo.CrisisGridServices.RunOffModel.RainFallComm;
import org.hpsearch.proxy.ProxyWebService;
import org.hpsearch.proxy.RunnableProxyWebService;
import org.hpsearch.proxy.WrapperProxyWebService;
import cgl.narada.jms.NBJmsInitializer;
/**
* This is the equivalent of the ComputeService in CrisisGrid Demo. Purpose of
* this service is to initially receive the Terrain maps from the mapServer.
* Then
* Created: May 13, 2004
* Modified from RunoffModelMain.java written by Sunghoon Ko(suko@indiana.edu)
*
* @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
*/
public class ComputeService extends WrapperProxyWebService {
/**
*
*/
public ComputeService() {
}
public void process() throws Exception {
}
public void initService(InputStream[] in, OutputStream[] out,
String[] params) {
try {
Properties props = new Properties();
props.put("hostname", InetAddress.getLocalHost().getHostName());
props.put("portnum", "3045");
initJMSConnection(props, "niotcp", 2004, "abcdefg");
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
public String[] serviceExceptions() {
return new String[0];
}
// Stuff private to the service
public RainFallComm rainfallComm;
public MapsGenerator4Visual runoffMaps4VisualComm;
public Computation runoffComputation;
private TopicConnection connection;
public void initJMSConnection(Properties bridgeProperties,
String transportType, int entityId, String userName) {
try {
NBJmsInitializer ini = new NBJmsInitializer(bridgeProperties,
transportType, entityId);
// Lookup a JMS connection factory
TopicConnectionFactory conFactory = (TopicConnectionFactory) ini
.lookup();
// Create a JMS connection
connection = conFactory.createTopicConnection("guest", "password");
System.out.println("++++ Init RainFallComm");
rainfallComm = new RainFallComm(this);
rainfallComm.initializeSession(connection, userName);
System.out.println("++++ Init MapsGenerator4Visual");
runoffMaps4VisualComm = new MapsGenerator4Visual(this);
runoffMaps4VisualComm.initializeSession(connection, userName);
System.out.println("++++ Init Computation");
runoffComputation = new Computation("");
} catch (Exception e) {
setStatus(RunnableProxyWebService.INIT_FAILURE);
}
setStatus(RunnableProxyWebService.SERVICE_READY);
}
/////////////////////////////////////////////////////////////////////
public void startService() throws Exception {
// Start the JMS connection; allows messages to be delivered
setStatus(ProxyWebService.SERVICE_RUNNING);
connection.start();
System.out.println("$$$$$$$ CONNECTION STARTED...");
runoffMaps4VisualComm.sendMe();
}
public void stopService() throws Exception {
// Stop the JMS connection;
connection.stop();
setStatus(ProxyWebService.SERVICE_STOPPED);
}
public void resumeService() throws Exception {
// Start the JMS connection; allows messages to be delivered
connection.start();
setStatus(ProxyWebService.SERVICE_RUNNING);
}
public void suspendService() throws Exception {
// Stop the JMS connection
connection.stop();
setStatus(ProxyWebService.SERVICE_SUSPENDED);
}
////////////////////////////////////////////////////////////////
public void runoffModel() {
runoffComputation.compute();
try {
runoffMaps4VisualComm.writeTextMessage("RunoffModelMain",
"DoneCompute", " ");
} catch (Exception e) {
}
// runoff maps are written on file after runoffCalculation() done.
// file names are fout*0.out
try {
String[] fileName = { "fout00.out", "fout30.out", "fout50.out",
"fout70.out", "fout99.out"};
for (int i = 0; i < 5; i++) {
// create two file references
File inputFile = new File(fileName[i]);
FileInputStream fis = new FileInputStream(inputFile);
byte[] buffer = new byte[(int) inputFile.length()];
runoffMaps4VisualComm.writeTextMessage("FileStatus",
"FileName", fileName[i]);
System.out.println("Sending: " + fileName[i]);
// chunk to send large file
int size = 1024 * 2;
byte[] tempByte = new byte[size];
int length = 0;
while ((length = fis.read(tempByte)) > -1)
runoffMaps4VisualComm.writeBytesMessage(tempByte, length);
if (length < 0)
runoffMaps4VisualComm.writeTextMessage("FileStatus",
"FileEnd", " ");
fis.close();
}
runoffMaps4VisualComm.writeTextMessage("FileStatus", "AllFileEnd",
" ");
} catch (Exception e) {
}
}
}