ViewTerm 1.1

This commit is contained in:
SeekDaSky 2017-05-04 12:11:42 +02:00
parent 9a6c480290
commit 6541012e04
17 changed files with 392 additions and 1683 deletions

View File

@ -1,73 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="CommandTerminal" default="default" basedir=".">
<description>Builds, tests, and runs the project CommandTerminal.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="CommandTerminal-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

View File

@ -1,3 +0,0 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +0,0 @@
build.xml.data.CRC32=fdad8e2e
build.xml.script.CRC32=1cd81838
build.xml.stylesheet.CRC32=8064a381@1.74.1.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=fdad8e2e
nbproject/build-impl.xml.script.CRC32=95faf5a3
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.74.1.48

View File

@ -1,73 +0,0 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processor.options=
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.test.classpath=\
${run.test.classpath}
# Files in build.classes.dir which should be excluded from distribution jar
dist.archive.excludes=
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/CommandTerminal.jar
dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
${javac.classpath}
javac.source=1.8
javac.target=1.8
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=commandterminal.CommandTerminal
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>CommandTerminal</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

View File

@ -1,69 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandterminal;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
/**
*
* @author lmascaro
*/
public class CommandTerminal {
private final static int SGCA_MULTICAST_PORT = 4446;
private final static String SCGA_MULTICAST_ADDRESS = "224.0.0.3";
/**
* @param args the command line arguments
*/
public static void main(String[] args){
/*
* Handshake
*/
try {
DatagramSocket socket = new DatagramSocket();
ByteBuffer buf = ByteBuffer.allocate(21);
buf.clear();
buf.put((byte)(0x01|0x02));
DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
socket.send(payload);
socket.receive(payload);
buf = ByteBuffer.wrap(payload.getData());
if(buf.get() == 3){
System.out.println("--Connection request successful");
byte address[] = new byte[15];
buf = buf.get(address,0,15);
String addressString = new String(address);
InetAddress sgcaAddress = InetAddress.getByName(addressString);
//emulate an unsigned short
char cast = buf.getChar();
int port = (int) cast;
System.out.println("----Address : "+sgcaAddress.getHostAddress());
System.out.println("----Port : "+port);
}
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -1,20 +0,0 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package commandterminal;
/**
*
* @author lmascaro
*/
public enum OpCode {
ISALIVE,
CAP,
ALT,
SPEED,
SUCCESS,
ERROR
}

Binary file not shown.

View File

@ -13,16 +13,20 @@ import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ViewTerminal.InvalidFlagException;
import ViewTerminal.PlaneContainer;
/**
*
* @author lmascaro
*/
public class AsynchronousDatagramSocket implements Runnable, AutoCloseable{
private final static int MAX_MESSAGE_SIZE = 300;
public final static int MAX_MESSAGE_SIZE = 300;
private SynchronizedBuffer<DatagramPacket> buf;
private DatagramSocket socket;
private PlaneContainer container;
public AsynchronousDatagramSocket() throws SocketException{
this.buf = new SynchronizedBuffer<>();
@ -36,6 +40,10 @@ public class AsynchronousDatagramSocket implements Runnable, AutoCloseable{
new Thread(this).start();
}
public void bindContainer(PlaneContainer c){
this.container = c;
}
public void send(DatagramPacket dp) throws IOException{
this.socket.send(dp);
}
@ -49,8 +57,8 @@ public class AsynchronousDatagramSocket implements Runnable, AutoCloseable{
}
}
public void synchronousReceive(DatagramPacket dp){
dp = this.buf.removeElement(true);
public DatagramPacket synchronousReceive(){
return this.buf.removeElement(true);
}
public boolean available(){
@ -64,12 +72,18 @@ public class AsynchronousDatagramSocket implements Runnable, AutoCloseable{
@Override
public void run() {
DatagramPacket packet = new DatagramPacket(new byte[MAX_MESSAGE_SIZE],MAX_MESSAGE_SIZE);
DatagramPacket packet;
while(true){
packet = new DatagramPacket(new byte[MAX_MESSAGE_SIZE],MAX_MESSAGE_SIZE);
try {
this.socket.receive(packet);
this.buf.addElement(packet);
this.container.notifyReceive();
} catch (IOException ex) {
Logger.getLogger(AsynchronousDatagramSocket.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidFlagException e) {
System.out.println("\033[01;31m Unexpected flag received \033[0m");
}
}
}
}

View File

@ -0,0 +1,18 @@
package ViewTerminal;
public class InvalidFlagException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public InvalidFlagException(){
super();
}
public InvalidFlagException(String message){
super(message);
}
}

View File

@ -0,0 +1,108 @@
package ViewTerminal;
public class Plane {
private String code;
private int x;
private int y;
private int z;
private int cap;
private int speed;
private boolean isDead;
public Plane(String code,int x, int y, int z, int cap, int speed){
this.code = code;
this.x = x;
this.y = y;
this.z = z;
this.cap = cap;
this.speed = speed;
this.isDead = false;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public int getCap() {
return cap;
}
public boolean isDead(){
return this.isDead;
}
public void setCap(int cap) {
this.cap = cap;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//Die Motherfucker
public void die(){
this.isDead = true;
}
public String getLine(int i){
switch(i){
case 0:
return "--------------------------------";
case 1:
if(this.isDead){
return "\033[4;37;41m Code: "+this.code+"\t\t\t|";
}else{
return "\033[4;37;42m Code: "+this.code+"\t\t\t|";
}
case 2:
return "\033[0m --Coords: {"+this.x+";"+this.y+";"+this.z+"} \t|";
case 3:
return "\033[0m --Cap: "+this.cap+"\t\t\t|";
case 4:
if(this.isDead){
return "\033[4;37;41m ATTENTION: avion hors ligne\t|\033[0m";
}else if(this.speed<50){
return "\033[5;37;41m --Speed: "+this.speed+"\t\t\t|\033[0m";
}else{
return "\033[0m --Speed: "+this.speed+"\t\t\t|";
}
default:
return "";
}
}
}

View File

@ -0,0 +1,88 @@
package ViewTerminal;
import java.net.DatagramPacket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import DatagramSocket.AsynchronousDatagramSocket;
public class PlaneContainer {
private HashMap<String,Plane> map;
private AsynchronousDatagramSocket socket;
private Printer printer;
public PlaneContainer(){
this.map = new HashMap<String,Plane>();
}
public void bindSocket(AsynchronousDatagramSocket s){
this.socket = s;
}
public void bindPrinter(Printer p){
this.printer = p;
}
public void notifyReceive() throws InvalidFlagException{
DatagramPacket packet = this.socket.synchronousReceive();
ByteBuffer buf = ByteBuffer.wrap(packet.getData());
byte flag;
byte nbrPlane;
ArrayList<String> codes;
flag = buf.get();
nbrPlane = buf.get();
codes = new ArrayList<String>(nbrPlane);
//System.out.println("Processing "+nbrPlane+" planes");
if(flag != 1){
throw new InvalidFlagException("Flag is not a feedback flag :"+flag);
}else{
String code;
byte rawCode[] = new byte[5];
Plane plane;
for(int i = 0;i<nbrPlane;i++){
//get plane code
buf = buf.get(rawCode,0,5);
code = new String(rawCode);
plane = this.map.get(code);
codes.add(code);
//remove EOL char
buf.get();
if(plane != null){
//fill the plane
plane.setX(buf.getInt());
plane.setY(buf.getInt());
plane.setZ(buf.getInt());
plane.setCap(buf.getInt());
plane.setSpeed(buf.getInt());
}else{
plane = new Plane(code,buf.getInt(),buf.getInt(),buf.getInt(),buf.getInt(),buf.getInt());
this.map.put(code, plane);
}
}
}
//check for dead planes
for(String code : this.map.keySet()){
if(!codes.contains(code)){
this.map.get(code).die();
}
}
this.printer.notifyReceive();
}
public HashMap<String,Plane> getMap(){
return this.map;
}
}

View File

@ -0,0 +1,53 @@
package ViewTerminal;
import java.util.HashMap;
public class Printer {
private PlaneContainer container;
public Printer(){
}
public void bindContainer(PlaneContainer c){
this.container = c;
}
public void notifyReceive(){
System.out.println("\033[2J \033[H");
HashMap<String,Plane> map = this.container.getMap();
int i = 0;
String lines[] = new String[5];
for(int k = 0;k<5;k++){
lines[k] = "";
}
if(map.keySet().size() == 0){
System.out.println("\033[37;43m Aucun avion connecté au système \033[0m");
}else{
for(String code : map.keySet()){
if(i <= 1){
for(int k = 0;k<5;k++){
lines[k] += map.get(code).getLine(k);
}
i++;
}else{
for(int k = 0;k<5;k++){
System.out.println(lines[k]);
lines[k] = "";
}
for(int k = 0;k<5;k++){
lines[k] += map.get(code).getLine(k);
}
i=1;
}
}
for(int k = 0;k<5;k++){
System.out.println(lines[k]);
}
}
}
}

View File

@ -0,0 +1,102 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ViewTerminal;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import DatagramSocket.AsynchronousDatagramSocket;
/**
*
* @author lmascaro
*/
public class ViewTerminal {
private final static int SGCA_MULTICAST_PORT = 4445;
private final static String SCGA_MULTICAST_ADDRESS = "224.0.0.2";
/**
* @param args the command line arguments
*/
public static void main(String[] args){
System.out.println("\033[2J");
/*
* Handshake
*/
int port = 0;
String addressString = "0.0.0.0";
try {
DatagramSocket socket = new DatagramSocket();
ByteBuffer buf = ByteBuffer.allocate(21);
buf.clear();
buf.put((byte)(0x01));
DatagramPacket payload = new DatagramPacket(buf.array(),buf.array().length,InetAddress.getByName(SCGA_MULTICAST_ADDRESS),SGCA_MULTICAST_PORT);
socket.send(payload);
socket.receive(payload);
buf = ByteBuffer.wrap(payload.getData());
if(buf.get() == 1){
System.out.println("--Connection request successful");
byte address[] = new byte[4];
buf = buf.get(address,0,4);
InetAddress addressObj = InetAddress.getByAddress(address);
addressString = addressObj.getHostAddress();
//emulate an unsigned short
char cast = buf.getChar();
port = (int) cast;
System.out.println("----Address : "+addressString);
System.out.println("----Port : "+port);
}
} catch ( IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
/*
* Opening final socket
*/
try {
//create all objects
AsynchronousDatagramSocket socket = new AsynchronousDatagramSocket();
PlaneContainer container = new PlaneContainer();
Printer printer = new Printer();
//bind them
socket.bindContainer(container);
container.bindPrinter(printer);
container.bindSocket(socket);
printer.bindContainer(container);
//send first packet
DatagramPacket p = new DatagramPacket(new byte[13],13,InetAddress.getByName(addressString),port);
socket.send(p);
//now we let the objects do the job
while(true){
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Binary file not shown.