Skip to content
Snippets Groups Projects
Commit 7a9efab7 authored by Martin Hustoles's avatar Martin Hustoles
Browse files

last commit on this trash

parent 312866fe
Branches main
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
......
File deleted
......@@ -40,6 +40,17 @@
<artifactId>byte-buddy</artifactId>
<version>1.14.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util-ajax</artifactId>
<version>9.4.46.v20220331</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -10,6 +10,10 @@ import java.io.IOException;
public class CPServer {
protected static final int SERVER_PORT = 3027;
public static void printMessage(String cookie, int messageId, String message) {
//printing recieved message on screen
System.out.println("Received message: " + message + " id: " + messageId + " with cookie: " + cookie);
}
public static void main(String[] args) {
// Set up the virtual link protocol
PhyProtocol phy = new PhyProtocol(SERVER_PORT);
......
......@@ -7,11 +7,20 @@ import java.util.zip.Checksum;
public class CPCommandMessage extends CPMsg{
private int currentMessageId;
private String command;
private String cookie;
private String message;
protected static final String HEADER = "command";
public CPCommandMessage() {
}
public boolean isCommandMessage() {
// Check if the message is a command message based on the specified format
// You may need to adjust this based on the actual structure of your message
return (this.data != null && this.data.startsWith("cp") && this.data.contains("command"));
}
public CPCommandMessage create(String cookie, String command, String message, int messageIdCount){
......@@ -31,12 +40,32 @@ public class CPCommandMessage extends CPMsg{
str.append(calcCRC(this.data)); //calculates the crc and adds it to the Frame
this.data = str.toString(); //builds the Frame again, but with the attached checksum
this.command = command;
this.cookie = cookie;
this.message = message;
return this;
}
public String getCommand() {
return command;
}
public String getCookie() {
return cookie;
}
public String getMessage() {
return message;
}
//returns the unique id of the message
public int getMessageId(){
return currentMessageId;
}
public long getTTL() {
return 0;
}
}
......@@ -20,6 +20,9 @@ public class CPCommandResponseMessage extends CPMsg{
return currentMessageId;
}
@Override
protected Msg parse(String sentence) throws IWProtocolException {
......
package cp;
import apps.CPServer;
import core.*;
import exceptions.*;
import phy.*;
......@@ -83,7 +84,7 @@ public class CPProtocol extends Protocol {
while(receving){
//1.2: 2 b
CPMsg cpmsg = new CPCommandResponseMessage();
CPCommandMessage cpmsg = new CPCommandMessage();
try {
in = this.PhyProto.receive(CP_TIMEOUT);
......@@ -91,6 +92,8 @@ public class CPProtocol extends Protocol {
//1.2: 2 b
in = cpmsg.parse(in.getData());
new CommandMessageProcessor().processCommandMessage(cpmsg);
} catch (Exception e) {
System.out.println(e.getMessage());
continue;
......@@ -113,6 +116,40 @@ public class CPProtocol extends Protocol {
return in;
}
private class CommandMessageProcessor {
public void processCommandMessage(CPCommandMessage cmdMsg) throws Exception {
String command = cmdMsg.getCommand();
String cookie = cmdMsg.getCookie();
int messageId = cmdMsg.getMessageId();
String message = cmdMsg.getMessage();
long TTL = cmdMsg.getTTL();
switch (command) {
case "status":
processStatusCommand(cookie, messageId, Integer.parseInt(command), TTL);
break;
case "print":
processPrintCommand(cookie, messageId, message);
break;
default:
System.out.println("Unknown command: " + command);
break;
}
}
private void processStatusCommand(String cookie, int messageId, int processedCommands, long sessionTTL) throws Exception {
CPStatusResponseMessage statusResponse = new CPStatusResponseMessage();
statusResponse.create(cookie, messageId, processedCommands, sessionTTL);
PhyProto.send(statusResponse.getData(), CPProtocol.this.PhyConfig);
}
private void processPrintCommand(String cookie, int messageId, String message) {
//print on server console
CPServer.printMessage(cookie, messageId, message);
}
}
public void cookieProcesing(PhyConfiguration config, CPCookie cookie) throws IWProtocolException, IOException {
......@@ -168,4 +205,8 @@ public class CPProtocol extends Protocol {
public static int getMaxId(){
return MAX_ID;
}
public PhyProtocol getPhyProtocol(){
return this.PhyProto;
}
}
package cp;
public class CPStatusResponseMessage extends CPMsg {
private int processedCommands;
private long sessionTTL;
public CPStatusResponseMessage() {
}
public void create(String cookie, int messageId, int processedCommands, long sessionTTL) {
this.processedCommands = processedCommands;
this.sessionTTL = sessionTTL;
StringBuilder str = new StringBuilder();
str.append("status").append(' ');
str.append(messageId).append(' ');
str.append(cookie).append(' ');
str.append(processedCommands).append(' ');
str.append(sessionTTL).append(' ');
super.create(str.toString());
this.data = str.toString();
str.append(calcCRC(this.data));
this.data = str.toString();
}
public int getProcessedCommands() {
return processedCommands;
}
public long getSessionTTL() {
return sessionTTL;
}
}
\ No newline at end of file
package CPPrintCommand;
import apps.CPServer;
import cp.CPCommandMessage;
import cp.CPProtocol;
import exceptions.IWProtocolException;
import org.junit.jupiter.api.Test;
import phy.PhyConfiguration;
import phy.PhyProtocol;
import java.io.IOException;
import static org.mockito.Mockito.*;
public class CPServerTest {
@Test
void testProcessWellFormedPrintCommand() throws IOException, IWProtocolException {
PhyProtocol mockPhyProtocol = mock(PhyProtocol.class);
PhyConfiguration mockPhyConfig = mock(PhyConfiguration.class);
CPProtocol cpProtocol = mock(CPProtocol.class);
// Set up print command message
CPCommandMessage printCommand = new CPCommandMessage();
printCommand.create("validCookie", "print", "Hello, Server!", 1);
// Mock the behavior getPhyProtocol() to return the mockPhyProtocol
when(cpProtocol.getPhyProtocol()).thenReturn(mockPhyProtocol);
CPServer.printMessage("validCookie", 1, "Hello, Server!");
verify(mockPhyProtocol).send(eq(printCommand.getData()), eq(mockPhyConfig));
}
@Test
void testProcessMalformedPrintCommand() throws IOException, IWProtocolException {
PhyProtocol mockPhyProtocol = mock(PhyProtocol.class);
PhyConfiguration mockPhyConfig = mock(PhyConfiguration.class);
CPProtocol cpProtocol = mock(CPProtocol.class);
CPCommandMessage malformedPrintCommand = new CPCommandMessage();
malformedPrintCommand.create("invalidOrExpiredCookie", "print", "Hello, Server!", 2);
// Mock the behavior getPhyProtocol() to return the mockPhyProtocol
when(cpProtocol.getPhyProtocol()).thenReturn(mockPhyProtocol);
CPServer.printMessage("invalidOrExpiredCookie", 2, "Hello, Server!");
verify(mockPhyProtocol, never()).send(eq(malformedPrintCommand.getData()), eq(mockPhyConfig));
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment