task 2 ongoing
This commit is contained in:
0
week3_TinsaeGhilay/Task 2/README.md
Normal file
0
week3_TinsaeGhilay/Task 2/README.md
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/commons-logging-1.1.jar
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/commons-logging-1.1.jar
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/lib/ws-commons-util-1.0.2.jar
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/ws-commons-util-1.0.2.jar
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-client-3.1.2.jar
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-client-3.1.2.jar
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-common-3.1.2.jar
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-common-3.1.2.jar
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-server-3.1.2.jar
Normal file
BIN
week3_TinsaeGhilay/Task 2/lib/xmlrpc-server-3.1.2.jar
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/out/client/Client.class
Normal file
BIN
week3_TinsaeGhilay/Task 2/out/client/Client.class
Normal file
Binary file not shown.
BIN
week3_TinsaeGhilay/Task 2/out/server/Server.class
Normal file
BIN
week3_TinsaeGhilay/Task 2/out/server/Server.class
Normal file
Binary file not shown.
77
week3_TinsaeGhilay/Task 2/src/client/Client.java
Normal file
77
week3_TinsaeGhilay/Task 2/src/client/Client.java
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
package client;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.util.Scanner;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Client class for XML-RPC communication
|
||||
*
|
||||
* @author Tinsae Ghilay
|
||||
* from the example on
|
||||
* https://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm
|
||||
*/
|
||||
// import the org.apache.xmlrpc package
|
||||
// it contains classes for XML-RPC client (and server) implementation
|
||||
import org.apache.xmlrpc.*;
|
||||
import org.apache.xmlrpc.client.XmlRpcClient;
|
||||
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
|
||||
|
||||
public class Client {
|
||||
|
||||
private static String TAG = "Client";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
// create an XML-RPC client object with address to server @ localhost
|
||||
// URL can be any valid URL. the default port for XML-RPC is 80
|
||||
// but here we use 8080 since 80 may need root privileges
|
||||
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
|
||||
config.setServerURL(new URL("http://localhost:8080/RPC2"));
|
||||
|
||||
XmlRpcClient client = new XmlRpcClient();
|
||||
client.setConfig(config);
|
||||
|
||||
while (true) {
|
||||
// create parameters to be sent to server method in a vector
|
||||
// and populate it
|
||||
Vector params = new Vector();
|
||||
|
||||
int a = validateInput(scanner);
|
||||
int b = validateInput(scanner);
|
||||
|
||||
params.addElement(Integer.valueOf(a));
|
||||
params.addElement(Integer.valueOf(b));
|
||||
// make the remote method call
|
||||
// the result of the remot call must be an Object
|
||||
// and be casted to the appropriate type
|
||||
Object result = client.execute("sample.sum", params);
|
||||
// process the result
|
||||
int sum = ((Integer) result).intValue();
|
||||
System.out.println("The sum is: " + sum);
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception e) { // catch exceptions if problems occur
|
||||
System.err.println(TAG + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static int validateInput(Scanner scanner) {
|
||||
int number;
|
||||
while (true) {
|
||||
System.out.print("Enter an integer: ");
|
||||
if (scanner.hasNextInt()) {
|
||||
number = scanner.nextInt();
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Invalid input. Please enter a valid integer.");
|
||||
scanner.next();
|
||||
}
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
}
|
||||
42
week3_TinsaeGhilay/Task 2/src/server/Server.java
Normal file
42
week3_TinsaeGhilay/Task 2/src/server/Server.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package server;
|
||||
|
||||
/**
|
||||
* Server class for XML-RPC communication
|
||||
* @author Tinsae Ghilay
|
||||
* from the example on
|
||||
* https://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm
|
||||
*/
|
||||
|
||||
// import the org.apache.xmlrpc package
|
||||
// it contains classes for XML-RPC client (and server) implementation
|
||||
import org.apache.xmlrpc.webserver.WebServer;
|
||||
import java.net.URL;
|
||||
import org.apache.xmlrpc.server.PropertyHandlerMapping;
|
||||
import org.apache.xmlrpc.server.XmlRpcServer;
|
||||
|
||||
public class Server {
|
||||
|
||||
private static String TAG = "Server";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
// start XML-RPC server at port 8080
|
||||
WebServer server = new WebServer(8080);
|
||||
XmlRpcServer xmlRpcServer = server.getXmlRpcServer();
|
||||
PropertyHandlerMapping phm = new PropertyHandlerMapping();
|
||||
phm.addHandler("sample", Server.class);
|
||||
xmlRpcServer.setHandlerMapping(phm);
|
||||
server.start();
|
||||
System.out.println(TAG + ": Server started successfully.");
|
||||
System.out.println("Accepting requests. (Halt program to stop.)");
|
||||
} catch (Exception e) { // catch exceptions if problems occur
|
||||
System.err.println(TAG + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public Integer sum(Integer x, Integer y) {
|
||||
return Integer.valueOf(x + y);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user