Skip to content

Commit 62d4cd6

Browse files
committed
add source
1 parent fe44afc commit 62d4cd6

16 files changed

+3142
-0
lines changed

src/client/Base64.java

Lines changed: 1451 additions & 0 deletions
Large diffs are not rendered by default.

src/client/FileUtils.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (c) 2017 NEOS-Server
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package org.neos.client;
23+
24+
import java.io.BufferedReader;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.DataInputStream;
28+
import java.io.FileInputStream;
29+
import java.io.FileNotFoundException;
30+
import java.io.FileReader;
31+
import java.io.IOException;
32+
import java.io.InputStreamReader;
33+
import java.io.ObjectInputStream;
34+
import java.io.ObjectOutputStream;
35+
import java.io.Reader;
36+
37+
/**
38+
* Helper class for accessing file via a generic method When Java application
39+
* accesses resource files, it can directly use file reader. However, when it is
40+
* executing as an applet, it needs to use other method.
41+
*
42+
* This class provide unified interface for accessing resource file.
43+
*
44+
* @author Thawan Kooburat
45+
*
46+
*/
47+
public class FileUtils {
48+
/**
49+
* Use this mode when running Java application directly
50+
*/
51+
public static final int APPLICATION_MODE = 0;
52+
53+
/**
54+
* Use this mode when executing as an applet
55+
*/
56+
public static final int APPLET_MODE = 1;
57+
58+
private static FileUtils self = null;
59+
60+
private int mode;
61+
62+
private FileUtils() {
63+
64+
}
65+
66+
/**
67+
* Create FileUtils instance for a specified mode
68+
*
69+
* @param mode
70+
* @return
71+
*/
72+
public static FileUtils getInstance(int mode) {
73+
if (self == null) {
74+
self = new FileUtils();
75+
self.mode = mode;
76+
}
77+
78+
return self;
79+
}
80+
81+
public String readFile(String fileName) {
82+
83+
StringBuffer content = new StringBuffer();
84+
String line;
85+
BufferedReader in;
86+
try {
87+
Reader reader;
88+
if (mode == APPLET_MODE)
89+
reader = new InputStreamReader(
90+
FileUtils.class.getResourceAsStream("/" + fileName));
91+
else
92+
reader = new FileReader(fileName);
93+
94+
in = new BufferedReader(reader);
95+
96+
while ((line = in.readLine()) != null)
97+
content.append(line + "\n");
98+
99+
} catch (FileNotFoundException e) {
100+
// TODO Auto-generated catch block
101+
e.printStackTrace();
102+
} catch (IOException e) {
103+
// TODO Auto-generated catch block
104+
e.printStackTrace();
105+
}
106+
107+
return content.toString();
108+
}
109+
110+
public byte[] readBinaryFile(String fileName) {
111+
112+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
113+
114+
byte buffer[] = new byte[1024];
115+
116+
int read;
117+
118+
try {
119+
DataInputStream reader;
120+
if (mode == APPLET_MODE)
121+
reader = new DataInputStream(
122+
FileUtils.class.getResourceAsStream("/" + fileName));
123+
else
124+
reader = new DataInputStream(new FileInputStream(fileName));
125+
126+
while ((read = reader.read(buffer, 0, buffer.length)) >= 0) {
127+
byteStream.write(buffer, 0, read);
128+
}
129+
130+
} catch (FileNotFoundException e) {
131+
// TODO Auto-generated catch block
132+
e.printStackTrace();
133+
} catch (IOException e) {
134+
// TODO Auto-generated catch block
135+
e.printStackTrace();
136+
}
137+
138+
return byteStream.toByteArray();
139+
}
140+
141+
/**
142+
* Use to clone object and its children. Useful for cloning a collection.
143+
* Standard Java clone only do shallow copy, so object in a collection is
144+
* not cloned.
145+
*
146+
* @param oldObj
147+
* @return
148+
*/
149+
static public Object deepCopy(Object oldObj) {
150+
ObjectOutputStream oos = null;
151+
ObjectInputStream ois = null;
152+
Object result = null;
153+
try {
154+
ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
155+
oos = new ObjectOutputStream(bos); // B
156+
// serialize and pass the object
157+
oos.writeObject(oldObj); // C
158+
oos.flush(); // D
159+
ByteArrayInputStream bin = new ByteArrayInputStream(
160+
bos.toByteArray()); // E
161+
ois = new ObjectInputStream(bin); // F
162+
// return the new object
163+
result = ois.readObject(); // G
164+
165+
oos.close();
166+
ois.close();
167+
} catch (Exception e) {
168+
e.printStackTrace();
169+
170+
}
171+
return result;
172+
}
173+
174+
}

src/client/NeosClient.java

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright (c) 2017 NEOS-Server
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
package org.neos.client;
23+
24+
import java.util.Vector;
25+
26+
import org.apache.xmlrpc.XmlRpcException;
27+
28+
public class NeosClient {
29+
private String host;
30+
private String port;
31+
32+
NeosXmlRpcClient server = null;
33+
34+
public NeosClient(String host, String port) {
35+
this.host = host;
36+
this.port = port;
37+
38+
server = new NeosXmlRpcClient(host, port);
39+
40+
if (server != null) {
41+
try {
42+
server.connect();
43+
} catch (XmlRpcException e) {
44+
// TODO Auto-generated catch block
45+
System.err.printf("Unable to connect to server %s:%s\n%s",
46+
host, port, e.getMessage());
47+
server = null;
48+
49+
}
50+
51+
}
52+
}
53+
54+
/**
55+
* Simple job submission interface.
56+
*
57+
* @param jobXML
58+
* String containing NEOS's job xml
59+
* @return null if there is an error during job submission
60+
*/
61+
public NeosJob submitJob(String jobXML) {
62+
if (server == null)
63+
return null;
64+
65+
Vector params = new Vector();
66+
params.add(jobXML);
67+
68+
Integer jobNo = 0;
69+
String jobPass = "";
70+
71+
String result = "";
72+
73+
try {
74+
75+
Object[] results = (Object[]) server.execute("submitJob", params,
76+
5000);
77+
78+
jobNo = (Integer) results[0];
79+
jobPass = (String) results[1];
80+
81+
params = new Vector();
82+
params.add(jobNo);
83+
params.add(jobPass);
84+
85+
Object retval = server.execute("getFinalResults", params);
86+
87+
if (retval instanceof String) {
88+
result = (String) retval;
89+
} else if (retval instanceof byte[]) {
90+
result = (new String((byte[]) retval));
91+
92+
}
93+
94+
} catch (XmlRpcException e) {
95+
System.err.println("Error submitting job\n" + e.getMessage());
96+
return null;
97+
}
98+
99+
NeosJob job = new NeosJob();
100+
job.setJobNo(jobNo);
101+
job.setJobPass(jobPass);
102+
job.setResult(result);
103+
104+
return job;
105+
}
106+
107+
/**
108+
* Non-blocking version of job submission method
109+
*
110+
* @param jobXML
111+
* @param callback
112+
* Object that implement result callback interface
113+
* @return NeosJob which contains job number and job password but not the
114+
* result.
115+
*/
116+
public NeosJob submitJobNonBlocking(String jobXML, ResultCallback callback) {
117+
if (server == null)
118+
return null;
119+
120+
ResultReceiver receiver;
121+
122+
Vector params = new Vector();
123+
params.add(jobXML);
124+
125+
Integer jobNo = 0;
126+
String jobPass = "";
127+
128+
String result = "";
129+
130+
try {
131+
132+
Object[] results = (Object[]) server.execute("submitJob", params,
133+
5000);
134+
135+
jobNo = (Integer) results[0];
136+
jobPass = (String) results[1];
137+
138+
} catch (XmlRpcException e) {
139+
System.err.println("Error submitting job\n" + e.getMessage());
140+
return null;
141+
}
142+
143+
receiver = new ResultReceiver(server, callback, jobNo, jobPass);
144+
receiver.start();
145+
146+
NeosJob job = new NeosJob();
147+
job.setJobNo(jobNo);
148+
job.setJobPass(jobPass);
149+
150+
return job;
151+
}
152+
153+
/**
154+
* Get content of results.txt file of a given job
155+
*
156+
* @param jobNo
157+
* job number
158+
* @param jobPass
159+
* job password
160+
* @return content of "results.txt" or empty string if file is not found
161+
*/
162+
public String getResultsFile(int jobNo, String jobPass) {
163+
if (server == null)
164+
return "";
165+
String result = "";
166+
167+
Vector params = new Vector();
168+
params.add(jobNo);
169+
params.add(jobPass);
170+
171+
// Wait until job is done
172+
try {
173+
Object ret;
174+
175+
String neosStatus = "";
176+
while (!neosStatus.equals("Done")) {
177+
neosStatus = (String) server.execute("getJobStatus", params);
178+
Thread.sleep(10);
179+
}
180+
181+
// Add file name that we want to get
182+
params.add("results.txt");
183+
184+
ret = server.execute("getOutputFile", params);
185+
186+
if (ret instanceof String) {
187+
result = (String) ret;
188+
} else if (ret instanceof byte[]) {
189+
result = (new String((byte[]) ret));
190+
191+
}
192+
193+
} catch (XmlRpcException e) {
194+
System.err.println("Error submitting job\n" + e.getMessage());
195+
196+
} catch (InterruptedException e) {
197+
System.err.println("User interrupted \n" + e.getMessage());
198+
199+
}
200+
201+
if (result.equals("Output file not exists"))
202+
result = "";
203+
204+
return result;
205+
206+
}
207+
}

0 commit comments

Comments
 (0)