Merge branch 'maximilien' into dev
This commit is contained in:
commit
8635ffcf82
@ -7,15 +7,14 @@ import java.util.regex.Pattern;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol
|
* Protocol
|
||||||
*
|
* <p>
|
||||||
* Class allowing to define new rules for the LightContainer protocol,
|
* Class allowing to define new rules for the LightContainer protocol,
|
||||||
* also provides utility functions to work with regexes and the command to compare.
|
* also provides utility functions to work with regexes and the command to compare.
|
||||||
*
|
*
|
||||||
* @version 1.0
|
|
||||||
* @since 1.0
|
|
||||||
*
|
|
||||||
* @see Pattern
|
|
||||||
* @author Jérémi NIHART <j.nihart@student.helmo.be>
|
* @author Jérémi NIHART <j.nihart@student.helmo.be>
|
||||||
|
* @version 1.0
|
||||||
|
* @see Pattern
|
||||||
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public abstract class Protocol {
|
public abstract class Protocol {
|
||||||
// Variables
|
// Variables
|
||||||
@ -25,6 +24,7 @@ public abstract class Protocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Protocol constructor
|
* Protocol constructor
|
||||||
|
*
|
||||||
* @param rule Command (e.g: LOGIN).
|
* @param rule Command (e.g: LOGIN).
|
||||||
* @param regex Regex to compile and use for this command (e.g: LOGIN ([A-Z0-9a-z]{1,20})).
|
* @param regex Regex to compile and use for this command (e.g: LOGIN ([A-Z0-9a-z]{1,20})).
|
||||||
* Do not forget to define the groups in the regex.
|
* Do not forget to define the groups in the regex.
|
||||||
@ -38,7 +38,6 @@ public abstract class Protocol {
|
|||||||
* Retrieve, the name of the Rule.
|
* Retrieve, the name of the Rule.
|
||||||
*
|
*
|
||||||
* @return Name of this rule.
|
* @return Name of this rule.
|
||||||
*
|
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public String getRule() {
|
public String getRule() {
|
||||||
@ -48,17 +47,16 @@ public abstract class Protocol {
|
|||||||
/**
|
/**
|
||||||
* Check if a command matches the rule with the rule matcher.
|
* Check if a command matches the rule with the rule matcher.
|
||||||
*
|
*
|
||||||
|
* @param cmd Command to verify with the rule matcher.
|
||||||
* @return True : if the command match with the rule.
|
* @return True : if the command match with the rule.
|
||||||
* False though.
|
* False though.
|
||||||
* @param cmd Command to verify with the rule matcher.
|
|
||||||
*
|
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
protected boolean matcherCheck(String cmd) {
|
protected boolean matcherCheck(String cmd) {
|
||||||
Matcher ruleMatcher = this.rulePattern.matcher(cmd);
|
Matcher ruleMatcher = this.rulePattern.matcher(cmd);
|
||||||
if (ruleMatcher.matches()) {
|
if (ruleMatcher.matches()) {
|
||||||
this.groups = new ArrayList<>();
|
this.groups = new ArrayList<>();
|
||||||
for (int i=1; i <= ruleMatcher.groupCount(); i++) this.groups.add(ruleMatcher.group(i));
|
for (int i = 1; i <= ruleMatcher.groupCount(); i++) this.groups.add(ruleMatcher.group(i));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -70,10 +68,8 @@ public abstract class Protocol {
|
|||||||
* <b>Requires to have run {@link #matcherCheck(String)}</b>
|
* <b>Requires to have run {@link #matcherCheck(String)}</b>
|
||||||
*
|
*
|
||||||
* @return String list containing all the groups extrapolated from the command.
|
* @return String list containing all the groups extrapolated from the command.
|
||||||
*
|
|
||||||
* @since 1.0
|
|
||||||
*
|
|
||||||
* @see Protocol#matcherCheck(String)
|
* @see Protocol#matcherCheck(String)
|
||||||
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
protected List<String> matcherGetGroups() {
|
protected List<String> matcherGetGroups() {
|
||||||
return this.groups;
|
return this.groups;
|
||||||
@ -81,17 +77,15 @@ public abstract class Protocol {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the rule on a command.
|
* Execute the rule on a command.
|
||||||
*
|
* <p>
|
||||||
* This function allows you to check a command and process those groups (parameters)
|
* This function allows you to check a command and process those groups (parameters)
|
||||||
* use the utility functions of {@link Protocol} to facilitate processing, see @see.
|
* use the utility functions of {@link Protocol} to facilitate processing, see @see.
|
||||||
*
|
*
|
||||||
* @param cmd Command on which to execute the rule.
|
* @param cmd Command on which to execute the rule.
|
||||||
*
|
|
||||||
* @since 1.0
|
|
||||||
*
|
|
||||||
* @see Protocol#execute(String)
|
* @see Protocol#execute(String)
|
||||||
* @see #matcherCheck(String)
|
* @see #matcherCheck(String)
|
||||||
* @see #matcherGetGroups()
|
* @see #matcherGetGroups()
|
||||||
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public abstract void execute(String cmd);
|
public abstract void execute(String cmd);
|
||||||
|
|
||||||
@ -99,7 +93,6 @@ public abstract class Protocol {
|
|||||||
* Retrieve, the hashcode of the rule.
|
* Retrieve, the hashcode of the rule.
|
||||||
*
|
*
|
||||||
* @return Rule hashcode.
|
* @return Rule hashcode.
|
||||||
*
|
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -112,7 +105,6 @@ public abstract class Protocol {
|
|||||||
*
|
*
|
||||||
* @return True : if the rules are equals
|
* @return True : if the rules are equals
|
||||||
* False if not.
|
* False if not.
|
||||||
*
|
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,45 @@
|
|||||||
|
package lightcontainer.protocol;
|
||||||
|
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public abstract class ProtocolWriter {
|
||||||
|
|
||||||
|
private final Pattern rulePattern;
|
||||||
|
private final String cmdName;
|
||||||
|
|
||||||
|
|
||||||
|
protected ProtocolWriter(String cmdName, String pattern) {
|
||||||
|
this.rulePattern = Pattern.compile(pattern);
|
||||||
|
this.cmdName = cmdName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de récupérer le nom du protocol
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getCmdName() {
|
||||||
|
return cmdName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de contruire une commande selon une règle établie.
|
||||||
|
* @param datas Les données à ajouter dans la commande; L'ordre défini leur position dans la commande
|
||||||
|
* @return La commande construite
|
||||||
|
*/
|
||||||
|
public String execute(String... datas) {
|
||||||
|
// Concatatène le nom de la commande avec les données (trim), avec un espace entre chaque
|
||||||
|
String command = null;
|
||||||
|
StringJoiner builder = new StringJoiner(" ", this.cmdName, "\r\n");
|
||||||
|
|
||||||
|
for (String data : datas)
|
||||||
|
builder.add(data);
|
||||||
|
|
||||||
|
command = builder.toString();
|
||||||
|
|
||||||
|
// Vérifie que tout match (cf. Matcher). Si match alors on retourne la commande build, sinon on retourne NULL
|
||||||
|
Matcher ruleMatcher = this.rulePattern.matcher(command);
|
||||||
|
return ruleMatcher.matches() ? command : null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package lightcontainer.protocol.rules;
|
package lightcontainer.protocol.rules.reader;
|
||||||
|
|
||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
|
|
@ -0,0 +1,14 @@
|
|||||||
|
package lightcontainer.protocol.rules.writer;
|
||||||
|
|
||||||
|
import lightcontainer.protocol.ProtocolWriter;
|
||||||
|
|
||||||
|
public class SignoutRule extends ProtocolWriter {
|
||||||
|
|
||||||
|
private static final String PATTERN = "^SIGNOUT\r\n$";
|
||||||
|
|
||||||
|
public static final String NAME = "SIGNOUT";
|
||||||
|
|
||||||
|
public SignoutRule() {
|
||||||
|
super(NAME, PATTERN);
|
||||||
|
}
|
||||||
|
}
|
@ -4,26 +4,40 @@ port = (6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9
|
|||||||
size = [0-9]{1,10}
|
size = [0-9]{1,10}
|
||||||
line = \r\n
|
line = \r\n
|
||||||
visiblechar = \p{Print}
|
visiblechar = \p{Print}
|
||||||
passchar = [^\s!]
|
passchar = [^ !]
|
||||||
binary = .
|
binary = .
|
||||||
password = [^\s!]{5,50}
|
password = [^ !]{5,50}
|
||||||
bl = \s
|
bl = //espace
|
||||||
letter = [A-Za-z]
|
letter = [A-Za-z]
|
||||||
digit_letter = [A-Za-z0-9]
|
digit_letter = [A-Za-z0-9]
|
||||||
filename = [^\s!]{1,20}
|
filename = [^ !]{1,20}
|
||||||
domain = [A-Za-z0-9.]{5,20}
|
domain = [A-Za-z0-9.]{5,20}
|
||||||
hash_filename = [A-Za-z0-9.]{50,200}
|
hash_filename = [A-Za-z0-9.]{50,200}
|
||||||
hash_filecontent = [A-Za-z0-9.]{50,200}
|
hash_filecontent = [A-Za-z0-9.]{50,200}
|
||||||
file_info = [A-Za-z0-9.]{50,200}\s[0-9]{1,10}\s[A-Za-z0-9.]{50,200}
|
file_info = [A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}
|
||||||
login = [A-Za-z0-9]{2,20}
|
login = [A-Za-z0-9]{2,20}
|
||||||
|
|
||||||
//StorBackEnd to FileFrontEnd
|
//StorBackEnd to FileFrontEnd
|
||||||
sbe_hello = ^(HELLO)\s([A-Za-z0-9.]{5,20})\s((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([0-5][0-9]{4})|([0-9]{1,4}))\r\n$ //TODO \r\n -> à tester pour voir si déjà dans le flux ou doit être construit
|
sbe_hello = ^HELLO ([A-Za-z0-9.]{5,20}) ([\d]{0,5})\r\n$ //TODO \r\n -> à tester pour voir si déjà dans le flux ou doit être construit
|
||||||
|
|
||||||
//FileFrontEnd to StorBackEnd
|
//FileFrontEnd to StorBackEnd
|
||||||
ffe_sendfile = ^(SENDFILE)\s([A-Za-z0-9.]{50,200}\s[0-9]{1,10}\s[A-Za-z0-9.]{50,200})\r\n(.*)$
|
ffe_sendfile = ^SENDFILE ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n$
|
||||||
sbe_sendresult = ^(SEND_OK|SEND_ERROR)\r\n$
|
sbe_sendresult = ^(SEND_OK|SEND_ERROR)\r\n$
|
||||||
ffe_erasefile = ^(ERASEFILE)\s([A-Za-z0-9.]{50,200})\r\n$
|
ffe_erasefile = ^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$
|
||||||
sbe_eraseresult = ^(ERASE_OK|ERASE_ERROR)\r\n$
|
sbe_eraseresult = ^(ERASE_OK|ERASE_ERROR)\r\n$
|
||||||
ffe_retrievefile = ^(RETRIEVEFILE)\s([A-Za-z0-9.]{50,200})\r\n$
|
ffe_retrievefile = ^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$
|
||||||
sbe_retrieveresult = ^(RETRIEVE_OK)\s([A-Za-z0-9.]{50,200}\s[0-9]{1,10}\s[A-Za-z0-9.]{50,200})\r\n(.*)|(RETRIEVE_ERROR)$
|
sbe_retrieveresult = ^(RETRIEVE_OK ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n)|(RETRIEVE_ERROR\r\n)$
|
||||||
|
|
||||||
|
//Client to FileFrontEnd
|
||||||
|
client_signin = ^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$
|
||||||
|
client_signup = ^SIGNUP ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$
|
||||||
|
ffe_signresult = ^(SIGN_OK|SIGN_ERROR)\r\n$
|
||||||
|
client_filelist = ^FILELIST\r\n$
|
||||||
|
ffe_filelistresult = ^FILES(( [^ !]{1,20})!([0-9]{1,10})){0,50}$
|
||||||
|
client_savefile = ^SAVE_FILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$
|
||||||
|
ffe_savefileresult = ^(SAVEFILE_OK|SAVEFILE_ERROR)\r\n$
|
||||||
|
client_getfile = ^GETFILE ([^ !]{1,20})\r\n$
|
||||||
|
ffe_getfileresult = ^(GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n)|(GETFILE_ERROR\r\n)$
|
||||||
|
client_removefile = ^REMOVEFILE ([^ !]{1,20})\r\n$
|
||||||
|
ffe_removefileresult = ^(REMOVEFILE_OK|REMOVEFILE_ERROR)\r\n$
|
||||||
|
client_signout = ^SIGNOUT\r\n$
|
@ -1,6 +1,7 @@
|
|||||||
package lightcontainer.protocol.rules;
|
package lightcontainer.protocol.rules.reader;
|
||||||
|
|
||||||
import lightcontainer.protocol.ProtocolReader;
|
import lightcontainer.protocol.ProtocolReader;
|
||||||
|
import lightcontainer.protocol.rules.reader.HelloRule;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
@ -0,0 +1,21 @@
|
|||||||
|
package lightcontainer.protocol.rules.writer;
|
||||||
|
|
||||||
|
import lightcontainer.protocol.ProtocolWriter;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SignoutRuleTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenRuleIsRightThenReturnCommand() {
|
||||||
|
//GIVEN
|
||||||
|
ProtocolWriter protocolWriter = new SignoutRule();
|
||||||
|
String[] datas = {};
|
||||||
|
|
||||||
|
//EXPECT
|
||||||
|
assertNotNull(protocolWriter.execute(datas));
|
||||||
|
assertEquals("SIGNOUT\r\n", protocolWriter.execute(datas));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user