Commit 3c6c960b authored by Matthias Möller's avatar Matthias Möller

Test3

parent 30f3807a
package Modell;
import com.sun.jna.Pointer;
import View.EmotivGUI;
//This class describes the approach to process data before sending them
public abstract class AbstractWebSocketComponent extends AbstractEmotiv {
AbstractEmotiv nextEmotivComponent;
//Wenn eine neue Komponente dem Dekorator hinzugefgt wird, wird der Leader der Grundeinheit EmotivInit aktualisiert
public AbstractWebSocketComponent(AbstractEmotiv emo){
this.nextEmotivComponent = emo;
setWebSocketLeader(this);
}
@Override
public void initialize() {
this.nextEmotivComponent.initialize();
}
@Override
public void actualize() {
this.nextEmotivComponent.actualize();
}
public Pointer getEmotivEvent(){
return this.nextEmotivComponent.getEmotivEvent();
}
public Pointer getEmoState(){
return this.nextEmotivComponent.getEmoState();
}
//Soll den leader der Grundeinheit aktualisieren durch delegation
public void setGUILeader(AbstractEmostateUpdatedComponent leader){
}
@Override
public void setWebSocketLeader(AbstractWebSocketComponent webSocketLeader) {
}
public void destroy(){
this.nextEmotivComponent.destroy();
this.nextEmotivComponent = null;
}
public EmotivGUI getView(){
return this.nextEmotivComponent.getView();
}
public void login(EmotivGUI emotivGUI){
this.nextEmotivComponent.login(emotivGUI);
}
public void updateGUI(){
}
public String edkErrorToString(){
return this.nextEmotivComponent.edkErrorToString();
}
public Float getRunningTime(){
return this.nextEmotivComponent.getRunningTime();
}
public String getEmotivEventTypeString(){
return this.nextEmotivComponent.getEmotivEventTypeString();
}
public String getMentalCommandTypeString(){
return this.nextEmotivComponent.getMentalCommandTypeString();
}
public String getBattery(){
return this.nextEmotivComponent.getBattery();
}
}
package Modell2;
public class EmotivInit {
}
package Server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
public class WebsocketServer {
private ServerSocket server;
private Socket client;
private InputStream in;
private OutputStream out;
private Scanner scanner;
private PrintWriter os;
//creates a connection and, if it's successful, it upgrades the connection to websocket
public void establishConnection() throws IOException, NoSuchAlgorithmException{
this.server = new ServerSocket(1333);
System.out.println("Connection to establish");
this.client = server.accept();
System.out.println("Connection established");
this.in = this.client.getInputStream();
this.scanner = new Scanner(this.in, "UTF-8");
this.out = this.client.getOutputStream();
this.os = new PrintWriter(this.out);
establishWebsocketConnection();
this.os.println("lol, omg");
this.os.flush();
}
private void establishWebsocketConnection() throws NoSuchAlgorithmException, IOException {
String data = this.scanner.useDelimiter("\\r\\n\\r\\n").next();
Matcher get = Pattern.compile("^GET").matcher(data);
if (get.find()) {
Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data);
match.find();
byte[] response = ("HTTP/1.1 101 Switching Protocols\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Accept: "
+ DatatypeConverter
.printBase64Binary(
MessageDigest
.getInstance("SHA-1")
.digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
.getBytes("UTF-8")))
+ "\r\n\r\n")
.getBytes("UTF-8");
this.out.write(response, 0, response.length);
}
}
private byte[] convertStringIntoByteCode(String message) {
byte payload[] = message.getBytes();
int length= payload.length;
int formatedBytes[] = null;
formatedBytes[0] = (byte) 129;
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment