Commit 66474db5 authored by Depasse Arthur's avatar Depasse Arthur

demo

parent 1847f4b8
......@@ -231,7 +231,7 @@ public class Main extends AbstractAcceleoGenerator {
String name = ((MessageImpl) me).getName();
System.out.println(name);
String[] args = {this.targetFolder.getAbsolutePath() + "/" + name + ".asn1", this.targetFolder.getAbsolutePath().concat("/src/main/java/"), "asn1"};
String[] args = {this.targetFolder.getAbsolutePath() + "/" + name + ".asn1", this.targetFolder.getAbsolutePath().concat("/src/main/java"), "asn1"};
Process process = new ProcessBuilder(file.getAbsolutePath(), "-f", args[0], "-o", args[1], "-p", args[2]).start();
InputStream is = process.getInputStream();
......
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DEMO</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.eclipse.sirius.nature.modelingproject</nature>
</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<fsm:FSM xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:fsm="http://fr.supelec.plong9.fsm" name="Client" initialState="//@ownedStates.0" finalState="//@ownedStates.2" groupId="fr.supelec">
<ownedStates name="Attente" onEnteredActions="//@ownedActions.3">
<outgoingTransitions name="connexionRecue" actions="//@ownedActions.0" guard="//@ownedGuards.0" event="//@ownedEvents.0" target="//@ownedStates.1"/>
<outgoingTransitions name="timeoutr" event="//@ownedEvents.2" target="//@ownedStates.2"/>
</ownedStates>
<ownedStates name="AttentePong" incomingTransitions="//@ownedStates.0/@outgoingTransitions.0" expectedMessage="//@ownedMessages.0">
<outgoingTransitions name="pongRecu" actions="//@ownedActions.1 //@ownedActions.2" event="//@ownedEvents.1" target="//@ownedStates.2"/>
</ownedStates>
<ownedStates name="Fin" incomingTransitions="//@ownedStates.1/@outgoingTransitions.0 //@ownedStates.0/@outgoingTransitions.1"/>
<ownedEvents name="newConnection"/>
<ownedEvents name="pongRecu"/>
<ownedEvents name="timeout"/>
<ownedActions name="envoiePing"/>
<ownedActions name="AffichagePong"/>
<ownedActions name="DisconnectFromServerAction"/>
<ownedActions name="timeoutAction"/>
<ownedGuards name="TjrsVrai"/>
<ownedMessages event="//@ownedEvents.1" name="MessagePing">
<data key="id" value="INTEGER"/>
<data key="type" value="MESSAGETYPE"/>
<enumsDef key="MESSAGETYPE" value="ping, pong"/>
</ownedMessages>
</fsm:FSM>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Client</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
MessagePing DEFINITIONS IMPLICIT TAGS::=
BEGIN
MESSAGETYPE ::= ENUMERATED {ping, pong}
MessagePing ::= SEQUENCE {
id INTEGER
, type MESSAGETYPE
}
END
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.supelec</groupId>
<artifactId>Client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.openmuc</groupId>
<artifactId>jasn1</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>fr.supelec.plong9.demo</groupId>
<artifactId>fsm</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>tests</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>Client.main</mainClass>
<systemProperties>
<systemProperty>
<key>javax.net.debug</key>
<value>null</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
package Client;
import java.io.IOException;
import org.openmuc.jasn1.ber.ReverseByteArrayOutputStream;
import fsm.Action;
import fsm.Event;
import fsm.FSM;
import fsm.FSMClient;
// Start of user code imports
// your imports
// End of user code
public class AFFICHAGEPONG extends Action {
@Override
public void execute(FSM fsm, Event e) {
// Start of user code for AffichagePong contents
fsm.LOGGER.info("Received Pong with id : " + e.getData("id"));
// End of user code
}
}
package Client;
import fsm.*;
import network.NetworkMessageParser;
import javafx.util.Pair;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.UUID;
import asn1.messageping.*;
public class ClientNetworkMessageParser extends NetworkMessageParser {
boolean alreadyParsed;
public ClientNetworkMessageParser(Pair<UUID, DataInputStream> origin, FSMClient fsm) {
super(origin, fsm);
alreadyParsed = false;
}
@Override
public void parse(String currentState) {
if(!alreadyParsed) {
switch (currentState) {
case "AttentePong":
MessagePing m = new MessagePing();
try {
m.decode(input);
Event e = new Event();
e.name = "pongRecu";
e.origin = originId;
e.setData("id", m.getId().intValue());
fsm.queueEvent(e);
alreadyParsed = true;
} catch (EOFException e) {
} catch (IOException e) {
fsm.LOGGER.warning("Cannot read from InputStream of client " + originId);
running = false;
e.printStackTrace();
}
break;
default:
break;
}
}
}
}
package Client;
import java.io.IOException;
import org.openmuc.jasn1.ber.ReverseByteArrayOutputStream;
import fsm.Action;
import fsm.Event;
import fsm.FSM;
import fsm.FSMClient;
// Start of user code imports
import java.util.concurrent.ThreadLocalRandom;
import org.openmuc.jasn1.ber.types.BerInteger;
import asn1.messageping.*;
// End of user code
public class ENVOIEPING extends Action {
@Override
public void execute(FSM fsm, Event e) {
// Start of user code for envoiePing contents
var dos = ((FSMClient)fsm).getNetworkClient().getOutputStream();
int randomId = ThreadLocalRandom.current().nextInt(0, 10000 + 1);
fsm.LOGGER.info("Sending Ping with id : " + randomId);
MessagePing r = new MessagePing();
r.setType(new MESSAGETYPE(0));
r.setId(new BerInteger(randomId));
// Strangely ASN1 require a reversing of bytes to be correctly decoded ...
ReverseByteArrayOutputStream rba = new ReverseByteArrayOutputStream(1000);
try {
r.encode(rba);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
dos.write(rba.getArray());
} catch (IOException exception) {
exception.printStackTrace();
}
// End of user code
}
}
package Client;
import java.io.IOException;
import org.openmuc.jasn1.ber.ReverseByteArrayOutputStream;
import fsm.Action;
import fsm.Event;
import fsm.FSM;
import fsm.FSMClient;
// Start of user code imports
// your imports
// End of user code
public class TIMEOUTACTION extends Action {
@Override
public void execute(FSM fsm, Event e) {
// Start of user code for timeoutAction contents
Event ev = new Event();
ev.name = "timeout";
fsm.launchTimer(3*1000, ev);
// End of user code
}
}
package Client;
import fsm.Event;
import fsm.FSM;
import fsm.Guard;
// Start of user code imports
// your imports
// End of user code
public class TJRSVRAI extends Guard{
@Override
public boolean eval(FSM fsm, Event e) {
// Start of user code for TjrsVrai contents
// TODO : Guard content
return true;
// End of user code
}
}
/**
* This class file was automatically generated by jASN1 v1.10.0 (http://www.openmuc.org)
*/
package asn1.messageping;
import java.io.IOException;
import java.io.EOFException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.io.Serializable;
import org.openmuc.jasn1.ber.*;
import org.openmuc.jasn1.ber.types.*;
import org.openmuc.jasn1.ber.types.string.*;
public class MESSAGETYPE extends BerEnum {
private static final long serialVersionUID = 1L;
public MESSAGETYPE() {
}
public MESSAGETYPE(byte[] code) {
super(code);
}
public MESSAGETYPE(BigInteger value) {
super(value);
}
public MESSAGETYPE(long value) {
super(value);
}
}
/**
* This class file was automatically generated by jASN1 v1.10.0 (http://www.openmuc.org)
*/
package asn1.messageping;
import java.io.IOException;
import java.io.EOFException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.io.Serializable;
import org.openmuc.jasn1.ber.*;
import org.openmuc.jasn1.ber.types.*;
import org.openmuc.jasn1.ber.types.string.*;
public class MessagePing implements BerType, Serializable {
private static final long serialVersionUID = 1L;
public static final BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.CONSTRUCTED, 16);
public byte[] code = null;
private BerInteger id = null;
private MESSAGETYPE type = null;
public MessagePing() {
}
public MessagePing(byte[] code) {
this.code = code;
}
public void setId(BerInteger id) {
this.id = id;
}
public BerInteger getId() {
return id;
}
public void setType(MESSAGETYPE type) {
this.type = type;
}
public MESSAGETYPE getType() {
return type;
}
public int encode(OutputStream reverseOS) throws IOException {
return encode(reverseOS, true);
}
public int encode(OutputStream reverseOS, boolean withTag) throws IOException {
if (code != null) {
for (int i = code.length - 1; i >= 0; i--) {
reverseOS.write(code[i]);
}
if (withTag) {
return tag.encode(reverseOS) + code.length;
}
return code.length;
}
int codeLength = 0;
codeLength += type.encode(reverseOS, true);
codeLength += id.encode(reverseOS, true);
codeLength += BerLength.encodeLength(reverseOS, codeLength);
if (withTag) {
codeLength += tag.encode(reverseOS);
}
return codeLength;
}
public int decode(InputStream is) throws IOException {
return decode(is, true);
}
public int decode(InputStream is, boolean withTag) throws IOException {
int codeLength = 0;
int subCodeLength = 0;
BerTag berTag = new BerTag();
if (withTag) {
codeLength += tag.decodeAndCheck(is);
}
BerLength length = new BerLength();
codeLength += length.decode(is);
int totalLength = length.val;
codeLength += totalLength;
subCodeLength += berTag.decode(is);
if (berTag.equals(BerInteger.tag)) {
id = new BerInteger();
subCodeLength += id.decode(is, false);
subCodeLength += berTag.decode(is);
}
else {
throw new IOException("Tag does not match the mandatory sequence element tag.");
}
if (berTag.equals(MESSAGETYPE.tag)) {
type = new MESSAGETYPE();
subCodeLength += type.decode(is, false);
if (subCodeLength == totalLength) {
return codeLength;
}
}
throw new IOException("Unexpected end of sequence, length tag: " + totalLength + ", actual sequence length: " + subCodeLength);
}
public void encodeAndSave(int encodingSizeGuess) throws IOException {
ReverseByteArrayOutputStream reverseOS = new ReverseByteArrayOutputStream(encodingSizeGuess);
encode(reverseOS, false);
code = reverseOS.getArray();
}
public String toString() {
StringBuilder sb = new StringBuilder();
appendAsString(sb, 0);
return sb.toString();
}
public void appendAsString(StringBuilder sb, int indentLevel) {
sb.append("{");
sb.append("\n");
for (int i = 0; i < indentLevel + 1; i++) {
sb.append("\t");
}
if (id != null) {
sb.append("id: ").append(id);
}
else {
sb.append("id: <empty-required-field>");
}
sb.append(",\n");
for (int i = 0; i < indentLevel + 1; i++) {
sb.append("\t");
}
if (type != null) {
sb.append("type: ").append(type);
}
else {
sb.append("type: <empty-required-field>");
}
sb.append("\n");
for (int i = 0; i < indentLevel; i++) {
sb.append("\t");
}
sb.append("}");
}
}
package Client;
import java.util.logging.Level;
import fsm.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import Client.ENVOIEPING;
import Client.AFFICHAGEPONG;
import fsm.DisconnectFromServerAction;
import Client.TIMEOUTACTION;
public class main {
public static void main(String[] args) {
InetAddress host = null;
try {
// TODO : Insert the address to the host here :
host = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
var fsm = new FSMClient("Client", (dis, f) -> new ClientNetworkMessageParser(dis, f), host, 50300, "clientpw");
fsm.LOGGER.setLevel(Level.INFO);
State state_attente = new State("Attente");
state_attente.onEnteredAction(new TIMEOUTACTION());
State state_attentepong = new State("AttentePong");
State state_fin = new State("Fin");
Transition trans_connexionrecue = new Transition("connexionRecue");
trans_connexionrecue.setSource(state_attente);
trans_connexionrecue.setTarget(state_attentepong);
trans_connexionrecue.registerEvent("newConnection");
trans_connexionrecue.registerAction(new ENVOIEPING());
trans_connexionrecue.registerGuard(new TJRSVRAI());
Transition trans_timeoutr = new Transition("timeoutr");
trans_timeoutr.setSource(state_attente);
trans_timeoutr.setTarget(state_fin);
trans_timeoutr.registerEvent("timeout");
Transition trans_pongrecu = new Transition("pongRecu");
trans_pongrecu.setSource(state_attentepong);
trans_pongrecu.setTarget(state_fin);
trans_pongrecu.registerEvent("pongRecu");
trans_pongrecu.registerAction(new AFFICHAGEPONG());
trans_pongrecu.registerAction(new DisconnectFromServerAction());
fsm.addState(state_attente);
fsm.addState(state_attentepong);
fsm.addState(state_fin);
fsm.setInitialState(state_attente);
fsm.setFinalState(state_fin);
fsm.start();
}
}
/AFFICHAGEPONG.class
/ClientNetworkMessageParser.class
/ENVOIEPING.class
/TIMEOUTACTION.class
/TJRSVRAI.class
/asn1/
/main.class