started task 7

This commit is contained in:
2025-12-28 03:18:13 +01:00
parent 01bd4ba693
commit ec4234ee13
66 changed files with 2431 additions and 121 deletions

View File

@@ -24,115 +24,130 @@
java -cp "out:lib/*" client.Client
```
### Server commands:
```java
// list of valid commands
private String[] commands = {
"add",
"subtract",
"multiply",
"divide",
"mod",
"power",
"root",
"exit"
};
### inspecting packets using wireshark.
***
#### Normal functions
***
```
I installed wireshark-cli and run
```bash
sudo tshark -i lo -f "tcp port 8080" -z follow,tcp,ascii,0
```
In Client program the interaction looked like this
```
Enter an integer: 4
Enter an integer: 6
The sum is: 10
```
And the output of wireshark was as follows
### inspecting packets using wireshark.
***
#### Normal functions
***
```xml
POST /RPC2 HTTP/1.1
Content-Type: text/xml
User-Agent: Apache XML RPC 3.0 (Sun HTTP Transport)
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8080
Accept: */*
Connection: keep-alive
Content-Length: 195
I installed wireshark-cli and run
```bash
sudo tshark -i lo -f "tcp port 8080" -z follow,tcp,ascii,0
```
In Client program the interaction looked like this
```
Enter an integer: 4
Enter an integer: 6
The sum is: 10
```
And the output of wireshark was as follows
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.sum</methodName>
<params>
<param>
<value><i4>4</i4></value>
</param>
<param>
<value><i4>6</i4></value>
</param>
</params>
</methodCall>
```xml
POST /RPC2 HTTP/1.1
Content-Type: text/xml
User-Agent: Apache XML RPC 3.0 (Sun HTTP Transport)
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8080
Accept: */*
Connection: keep-alive
Content-Length: 195
HTTP/1.1 200 OK
Server: Apache XML-RPC 1.0
Connection: close
Content-Type: text/xml
Content-Length: 129
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.sum</methodName>
<params>
<param>
<value><i4>4</i4></value>
</param>
<param>
<value><i4>6</i4></value>
</param>
</params>
</methodCall>
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
HTTP/1.1 200 OK
Server: Apache XML-RPC 1.0
Connection: close
Content-Type: text/xml
Content-Length: 129
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value><i4>10</i4></value>
</param>
</params>
</methodResponse>
```
I believe this is the expected result.
***
#### Intentional error
I changed the messge call string to `sample.add` instead of the correct `sample.sum` and run the program. obviously it threw an exception. the http response from wireguard was as follows
```xml
POST /RPC2 HTTP/1.1
Content-Type: text/xml
User-Agent: Apache XML RPC 3.0 (Sun HTTP Transport)
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8080
Accept: */*
Connection: keep-alive
Content-Length: 196
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.add</methodName>
<params>
<param>
<value><i4>10</i4></value>
<value><i4>23</i4></value>
</param>
<param>
<value><i4>4</i4></value>
</param>
</params>
</methodResponse>
```
I believe this is the expected result.
</methodCall>
***
#### Intentional error
I changed the messge call string to `sample.add` instead of the correct `sample.sum` and run the program. obviously it threw an exception. the http response from wireguard was as follows
```xml
POST /RPC2 HTTP/1.1
Content-Type: text/xml
User-Agent: Apache XML RPC 3.0 (Sun HTTP Transport)
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8080
Accept: */*
Connection: keep-alive
Content-Length: 196
HTTP/1.1 200 OK
Server: Apache XML-RPC 1.0
Connection: close
Content-Type: text/xml
Content-Length: 265
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>sample.add</methodName>
<params>
<param>
<value><i4>23</i4></value>
</param>
<param>
<value><i4>4</i4></value>
</param>
</params>
</methodCall>
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><i4>0</i4></value>
</member>
<member>
<name>faultString</name>
<value>No such handler: sample.add</value>
</member>
</struct>
</value>
</fault>
</methodResponse>
```
Again the expected results. I only tested it in one machine. so time will be done may be another time.
HTTP/1.1 200 OK
Server: Apache XML-RPC 1.0
Connection: close
Content-Type: text/xml
Content-Length: 265
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><i4>0</i4></value>
</member>
<member>
<name>faultString</name>
<value>No such handler: sample.add</value>
</member>
</struct>
</value>
</fault>
</methodResponse>
```
Again the expected results. I only tested it in one machine. so time will be done may be another time.

Binary file not shown.

View File

@@ -39,23 +39,43 @@ public class Client {
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
InputHandler handler = new InputHandler();
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);
// get two integers from user
// promptForInt returns null if user wants to exit
Integer a = handler.promptForInt(scanner);
if (a == null) { // user wants to exit
notifyExit();
break;
}
Integer b = handler.promptForInt(scanner);
if (b == null) { // user wants to exit
notifyExit();
break;
}
// get command from user
String command = handler.promptForCommand(scanner);
if (command.equals("exit")) { // user wants to exit
notifyExit();
break;
}
// add parameters to vector
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.add", params);
Object result = client.execute("calculator." + command, params);
// process the result
int sum = ((Integer) result).intValue();
System.out.println("The sum is: " + sum);
System.out.println("The " + handler.mapCommand(command) + " of " + a + " and " + b + " is: " + sum);
}
@@ -64,19 +84,8 @@ public class Client {
}
}
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;
private static void notifyExit() {
System.out.println("Exit command received. Exiting...");
}
}

View File

@@ -0,0 +1,127 @@
package client;
/**
* InputHandler class
*
* @author Tinsae Ghilay
* for handling user inputs
*/
import java.util.Scanner;
import java.util.Arrays;
public class InputHandler {
// list of valid commands
private String[] commands = {
"add",
"subtract",
"multiply",
"divide",
"mod",
"power",
"root",
"exit"
};
/**
* method to prompt user for a valid integer
*
* @param scanner Scanner object for user input
* @return valid integer input from user
*/
public Integer promptForInt(Scanner scanner) {
Integer number;
while (true) {
System.out.print("Enter an integer: ");
if (scanner.hasNextInt()) {
number = scanner.nextInt();
scanner.nextLine(); // consume the newline
break;
} else {
String command = scanner.nextLine();
// check if user wants to exit and return null
if (command.equalsIgnoreCase("exit")) {
return null;
}
System.out.println("Invalid input. Please enter a valid integer.");
}
}
return number;
}
/**
* method to prompt user for a valid command
*
* @param scanner Scanner object for user input
* @return valid command input from user
*/
public String promptForCommand(Scanner scanner) {
System.out.print("Enter command (add, subtract, multiply, divide, mod, power, root, exit): ");
while (true) {
String command = scanner.nextLine().trim().toLowerCase();
if (isValidCommand(command)) {
return command;
} else {
System.out.print("Invalid command. Please enter a valid command: ");
}
}
}
/**
* helper method to check if command is valid
*
* @param command
* @return
*/
private boolean isValidCommand(String command) {
return Arrays.asList(commands).contains(command.toLowerCase());
}
/**
* getter for commands
*
* @return
*/
public String[] getCommands() {
return commands;
}
/**
* getter for command at index
*
* @param index
* @return
*/
public String getCommand(int index) {
return commands[index];
}
/**
* method to map user command to result term
*
* @param command
* @return
*/
public String mapCommand(String command) {
switch (command.toLowerCase()) {
case "add":
return "sum";
case "subtract":
return "difference";
case "multiply":
return "product";
case "divide":
return "quotient";
case "mod":
return "mod";
case "power":
return "power";
case "root":
return "root";
default:
return null;
}
}
}

View File

@@ -0,0 +1,91 @@
package server;
/**
* Executor class for XML-RPC communication
*
* @author Tinsae Ghilay
* based on tutorial from
* https://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm
*/
public class Executor {
// define methods that can be called remotely
/**
* sum of two integers
*
* @param x
* @param y
* @return
*/
public Integer add(Integer x, Integer y) {
return Integer.valueOf(x + y);
}
/**
* difference of two integers x - y
*
* @param x
* @param y
* @return
*/
public Integer subtract(Integer x, Integer y) {
return Integer.valueOf(x - y);
}
/**
* product of two integers
*
* @param x
* @param y
* @return
*/
public Integer multiply(Integer x, Integer y) {
return Integer.valueOf(x * y);
}
/**
* division of two integers x/y
*
* @param x
* @param y
* @return
*/
public Double divide(Integer x, Integer y) {
return Double.valueOf((double) x / y);
}
/**
* modulus of two integers
*
* @param x
* @param y
* @return
*/
public Integer mod(Integer x, Integer y) {
return Integer.valueOf(x % y);
}
/**
* power of two integers x^y
*
* @param x
* @param y
* @return
*/
public Integer power(Integer x, Integer y) {
return Integer.valueOf((int) Math.pow(x, y));
}
/**
* root of two integers X√Y
*
* @param x
* @param y
* @return
*/
public Integer root(Integer x, Integer y) {
return Integer.valueOf((int) Math.pow(x, 1.0 / y));
}
}

View File

@@ -24,20 +24,24 @@ public class Server {
// 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);
phm.addHandler("calculator", Executor.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);
}
}