task 2 ongoing

This commit is contained in:
2025-12-21 20:45:20 +01:00
parent dbe0192866
commit 5141562548
10 changed files with 119 additions and 0 deletions

View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View 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;
}
}

View 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);
}
}