Skip to content
Snippets Groups Projects
Commit 7b29f12c authored by Philip's avatar Philip
Browse files

CBOR now valid

parent 81731422
Branches
No related tags found
No related merge requests found
...@@ -4,10 +4,14 @@ import java.io.ByteArrayOutputStream; ...@@ -4,10 +4,14 @@ import java.io.ByteArrayOutputStream;
import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.CoapClient;
import co.nstant.in.cbor.CborBuilder; import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborEncoder; import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException; import co.nstant.in.cbor.CborException;
import boarderGateway.CoAPRessources.*; import boarderGateway.CoAPRessources.*;
import org.apache.commons.codec.binary.Hex;
import com.sun.xml.internal.fastinfoset.algorithm.HexadecimalEncodingAlgorithm;
public class CoAPClient { public class CoAPClient {
...@@ -17,28 +21,34 @@ public class CoAPClient { ...@@ -17,28 +21,34 @@ public class CoAPClient {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
new CborEncoder(baos).encode(new CborBuilder() new CborEncoder(baos).encode(new CborBuilder()
// .add("MyClient Name") .addArray()
// .addArray() // add array /*.add("Neuer Test")
// .add("Para11") .addArray() // add array
// .add("string") .add("Parameter 1")
// .end() .add("float")
// .addArray() .end()
// .add("Para22") .addArray()
// .add("integer") .add("String Parmeter 2")
// .end() .add("string")
.end()
.add("My String value") */
.add(1234)
.add("1")
.add(2300)
.end()
.build()); .build());
} catch (CborException e) { } catch (CborException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
//System.out.println("String: "+baos.toString());
System.out.println("Byte: "+Hex.encodeHexString(baos.toByteArray()));
byte[] encodedBytes = baos.toByteArray(); byte[] encodedBytes = baos.toByteArray();
CoapClient client = new CoapClient("coap://localhost/data"); CoapClient client = new CoapClient("coap://localhost/data");
client.post(encodedBytes, 0); client.put(encodedBytes, 0);
} }
......
...@@ -57,7 +57,10 @@ public class CoAPRessources { ...@@ -57,7 +57,10 @@ public class CoAPRessources {
List<String> paras = new ArrayList<String>(); List<String> paras = new ArrayList<String>();
List<String> types = new ArrayList<String>(); List<String> types = new ArrayList<String>();
// Gehe zum ersten Token. Überspringe erstes "["
myCborParser.nextToken(); myCborParser.nextToken();
myCborParser.nextToken();
String name = myCborParser.getText(); String name = myCborParser.getText();
System.out.println("Name: "+name); System.out.println("Name: "+name);
...@@ -85,6 +88,8 @@ public class CoAPRessources { ...@@ -85,6 +88,8 @@ public class CoAPRessources {
if (!myCborParser.getText().equals("]")){ if (!myCborParser.getText().equals("]")){
errorOccurred = true; errorOccurred = true;
} }
}else if (myCborParser.getText().equals("]")) {
// CBOR Ende
}else{ }else{
// Kein (korrektes) Array in CBOR gefunden // Kein (korrektes) Array in CBOR gefunden
errorOccurred = true; errorOccurred = true;
...@@ -113,6 +118,8 @@ public class CoAPRessources { ...@@ -113,6 +118,8 @@ public class CoAPRessources {
} }
}else{ }else{
// Client nicht erlaubt - Speichere in tbl_unregistered
database.storeUnregisteredClient(source_address);
exchange.respond(ResponseCode.UNAUTHORIZED); exchange.respond(ResponseCode.UNAUTHORIZED);
} }
} }
...@@ -145,6 +152,9 @@ public class CoAPRessources { ...@@ -145,6 +152,9 @@ public class CoAPRessources {
byte payload[] = exchange.getRequestPayload(); byte payload[] = exchange.getRequestPayload();
CBORParser myCborParser = f.createParser(payload); CBORParser myCborParser = f.createParser(payload);
//Springe zum ersten Token
myCborParser.nextToken();
// Erhalte Reihenfolge von String & Integer // Erhalte Reihenfolge von String & Integer
List<String> types = database.getClientParamsTypeList(source_address); List<String> types = database.getClientParamsTypeList(source_address);
for (int i = 0; i < types.size();i++){ for (int i = 0; i < types.size();i++){
......
...@@ -3,6 +3,8 @@ package boarderGateway; ...@@ -3,6 +3,8 @@ package boarderGateway;
import org.eclipse.californium.core.CoapServer; import org.eclipse.californium.core.CoapServer;
import boarderGateway.CoAPRessources.*; import boarderGateway.CoAPRessources.*;
import boarderGateway.CoAPTestRessources.*;
public class CoAPServer { public class CoAPServer {
...@@ -10,9 +12,20 @@ public class CoAPServer { ...@@ -10,9 +12,20 @@ public class CoAPServer {
// TODO Auto-generated method stub // TODO Auto-generated method stub
CoapServer server = new CoapServer(); CoapServer server = new CoapServer();
server.add(new RegisterRessource()); if (args.length > 0){
server.add(new DataRessource()); if (args[0].equals("test")){
server.add(new RegisterRessourceTest());
server.add(new DataRessourceTest());
}else{
server.add(new RegisterRessource());
server.add(new DataRessource());
}
}else{
server.add(new RegisterRessource());
server.add(new DataRessource());
}
server.start(); server.start();
} }
......
package boarderGateway;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.coap.CoAP.ResponseCode;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORConstants;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import com.fasterxml.jackson.dataformat.cbor.CBORParser;
import co.nstant.in.cbor.*;
import co.nstant.in.cbor.decoder.UnicodeStringDecoder;
import co.nstant.in.cbor.model.*;
public class CoAPTestRessources {
public static class RegisterRessourceTest extends CoapResource{
public RegisterRessourceTest(){
super("register");
getAttributes().setTitle("Client Registrierung");
}
@Override
public void handlePOST(CoapExchange exchange) {
List<String> allowedTypes = Arrays.asList("string", "integer", "float");
boolean errorOccurred = false;
// TODO Auto-generated method stub
CBORFactory f = new CBORFactory();
String source_address = exchange.getSourceAddress().getHostAddress();
System.out.println("Received POST on /register from "+source_address);
try {
// Lade erhaltenen Bytecode in CBOR Parser
byte payload[] = exchange.getRequestPayload();
CBORParser myCborParser = f.createParser(payload);
List<String> paras = new ArrayList<String>();
List<String> types = new ArrayList<String>();
myCborParser.nextToken();
myCborParser.nextToken();
String name = myCborParser.getText();
System.out.println("Name: "+name);
// Lese Arrays aus: [ "Parameter Name" , "Datentyp"]
String para = "";
String type = "";
// While Schleife, bis zum Ende der CBOR Nachricht
while (myCborParser.nextToken() != null){
// Array Beginn
if (myCborParser.getText().equals("[")){
para = myCborParser.nextTextValue();
type = myCborParser.nextTextValue();
// Prüfe ob Typ supported wird.
if (allowedTypes.contains(type)){
paras.add(para);
types.add(type);
System.out.println("Added Para "+para+" of Type "+type);
}else{
errorOccurred = true;
}
// Nächstes Token muss Array schließen, ansonsten Fehler.
myCborParser.nextToken();
if (!myCborParser.getText().equals("]")){
errorOccurred = true;
}
}else if (myCborParser.getText().equals("]")) {
// CBOR Ende
}else{
// Kein (korrektes) Array in CBOR gefunden
errorOccurred = true;
}
}
if (!errorOccurred){
exchange.respond(ResponseCode.CREATED);
}else{
exchange.respond(ResponseCode.BAD_REQUEST);
}
} catch (JsonParseException e) {
// TODO Auto-generated catch block
exchange.respond(ResponseCode.BAD_REQUEST);
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
exchange.respond(ResponseCode.BAD_REQUEST);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
exchange.respond(ResponseCode.BAD_REQUEST);
e.printStackTrace();
}
}
}
public static class DataRessourceTest extends CoapResource{
public DataRessourceTest(){
super("data");
getAttributes().setTitle("Datenuebertragung");
}
@Override
public void handlePUT(CoapExchange exchange) {
// TODO Auto-generated method stub
// Ermittle letzte registrierte ID zur Source Address
CBORFactory f = new CBORFactory();
String source_address = exchange.getSourceAddress().getHostAddress();
System.out.println("Received PUT on /data from "+source_address);
boolean errorOccurred = false;
List<String> values = new ArrayList<String>();
try {
byte payload[] = exchange.getRequestPayload();
CBORParser myCborParser = f.createParser(payload);
// Erhalte Reihenfolge von String & Integer
while (myCborParser.nextToken() != null){
System.out.println(myCborParser.getValueAsString(""));
}
exchange.respond(ResponseCode.CHANGED);
} catch (IOException e) {
// TODO Auto-generated catch block
exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR);
}
}
}
}
...@@ -20,6 +20,7 @@ public class MySQL { ...@@ -20,6 +20,7 @@ public class MySQL {
public static final String TBL_PARAMS = "tbl_parameter"; public static final String TBL_PARAMS = "tbl_parameter";
public static final String TBL_DATA = "tbl_data"; public static final String TBL_DATA = "tbl_data";
public static final String TBL_ADDRESSES = "tbl_addresses"; public static final String TBL_ADDRESSES = "tbl_addresses";
public static final String TBL_UNREGISTERED = "tbl_unregistered";
private Connection conn = null; private Connection conn = null;
private Statement statement = null; private Statement statement = null;
...@@ -96,6 +97,7 @@ public class MySQL { ...@@ -96,6 +97,7 @@ public class MySQL {
System.out.println(query); System.out.println(query);
this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS); this.statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys()); int lastID = this.getLastInsertedID(this.statement.getGeneratedKeys());
System.out.println("Last ID: "+lastID);
if (lastID > 0){ if (lastID > 0){
for ( int i = 0; i<params.size(); i++){ for ( int i = 0; i<params.size(); i++){
query = "INSERT INTO "+MySQL.TBL_PARAMS+"(fid_register, param, type) VALUES("+lastID+", '"+params.get(i)+"', '"+types.get(i)+"')"; query = "INSERT INTO "+MySQL.TBL_PARAMS+"(fid_register, param, type) VALUES("+lastID+", '"+params.get(i)+"', '"+types.get(i)+"')";
...@@ -194,6 +196,22 @@ public class MySQL { ...@@ -194,6 +196,22 @@ public class MySQL {
return parameters; return parameters;
} }
public void storeUnregisteredClient(String clientAddress){
String query = "SELECT * FROM "+MySQL.TBL_UNREGISTERED+" WHERE clientAddress = '"+clientAddress+"'";
try {
ResultSet rs = this.statement.executeQuery(query);
// True if not empty
if (!rs.next()){
query = "INSERT INTO "+MySQL.TBL_UNREGISTERED+"(clientAddress) VALUES('"+clientAddress+"')";
this.statement.executeUpdate(query);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
// Do Nothing
}
}
private int getLastInsertedID(ResultSet keys){ private int getLastInsertedID(ResultSet keys){
try { try {
keys.next(); keys.next();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment