diff --git a/iot/tp3/.gitignore b/iot/tp3/.gitignore new file mode 100644 index 0000000..ab4f15c --- /dev/null +++ b/iot/tp3/.gitignore @@ -0,0 +1,3 @@ +out +/resources.zip +/sujet_CoAP.pdf diff --git a/iot/tp3/.vscode/settings.json b/iot/tp3/.vscode/settings.json new file mode 100644 index 0000000..278c9f4 --- /dev/null +++ b/iot/tp3/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "editor.formatOnType": true, + "[commonlisp]": { + "editor.wordSeparators": "`|;:'\",()" + } +} \ No newline at end of file diff --git a/iot/tp3/occitanium-client/.gitignore b/iot/tp3/occitanium-client/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/iot/tp3/occitanium-client/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/iot/tp3/occitanium-client/.idea/.gitignore b/iot/tp3/occitanium-client/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/iot/tp3/occitanium-client/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/iot/tp3/occitanium-client/.idea/misc.xml b/iot/tp3/occitanium-client/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/iot/tp3/occitanium-client/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-client/.idea/modules.xml b/iot/tp3/occitanium-client/.idea/modules.xml new file mode 100644 index 0000000..8143d35 --- /dev/null +++ b/iot/tp3/occitanium-client/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-client/.idea/vcs.xml b/iot/tp3/occitanium-client/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/iot/tp3/occitanium-client/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-client/Californium.properties b/iot/tp3/occitanium-client/Californium.properties new file mode 100644 index 0000000..d5f7721 --- /dev/null +++ b/iot/tp3/occitanium-client/Californium.properties @@ -0,0 +1,37 @@ +#Californium CoAP Properties file +#Wed Apr 10 19:25:41 CEST 2024 +ACK_RANDOM_FACTOR=1.5 +ACK_TIMEOUT=2000 +ACK_TIMEOUT_SCALE=2 +CROP_ROTATION_PERIOD=2000 +DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP +DEFAULT_BLOCK_SIZE=512 +DEFAULT_COAP_PORT=5683 +DEFAULT_ENDPOINT_THREAD_COUNT=1 +DEFAULT_LEISURE=5000 +EXCHANGE_LIFECYCLE=247000 +HTTP_CACHE_RESPONSE_MAX_AGE=86400 +HTTP_CACHE_SIZE=32 +HTTP_PORT=8080 +HTTP_SERVER_SOCKET_BUFFER_SIZE=8192 +HTTP_SERVER_SOCKET_TIMEOUT=100000 +MARK_AND_SWEEP_INTERVAL=10000 +MAX_MESSAGE_SIZE=1024 +MAX_RETRANSMIT=4 +MAX_TRANSMIT_WAIT=93000 +NOTIFICATION_CHECK_INTERVAL=86400000 +NOTIFICATION_CHECK_INTERVAL_COUNT=100 +NOTIFICATION_MAX_AGE=128000 +NOTIFICATION_REREGISTRATION_BACKOFF=2000 +NSTART=1 +PROBING_RATE=1.0 +SERVER_THRESD_NUMER=8 +UDP_CONNECTOR_DATAGRAM_SIZE=2000 +UDP_CONNECTOR_LOG_PACKETS=false +UDP_CONNECTOR_OUT_CAPACITY=2147483647 +UDP_CONNECTOR_RECEIVER_THREAD_COUNT=1 +UDP_CONNECTOR_RECEIVE_BUFFER=0 +UDP_CONNECTOR_SENDER_THREAD_COUNT=1 +UDP_CONNECTOR_SEND_BUFFER=0 +USE_RANDOM_MID_START=true +USE_RANDOM_TOKEN_START=true diff --git a/iot/tp3/occitanium-client/lib/californium-core-1.0.0-SNAPSHOT.jar b/iot/tp3/occitanium-client/lib/californium-core-1.0.0-SNAPSHOT.jar new file mode 100755 index 0000000..d96b658 Binary files /dev/null and b/iot/tp3/occitanium-client/lib/californium-core-1.0.0-SNAPSHOT.jar differ diff --git a/iot/tp3/occitanium-client/lib/element-connector-1.0-SNAPSHOT.jar b/iot/tp3/occitanium-client/lib/element-connector-1.0-SNAPSHOT.jar new file mode 100755 index 0000000..f8ef714 Binary files /dev/null and b/iot/tp3/occitanium-client/lib/element-connector-1.0-SNAPSHOT.jar differ diff --git a/iot/tp3/occitanium-client/occitanium-client.iml b/iot/tp3/occitanium-client/occitanium-client.iml new file mode 100644 index 0000000..be4e9f8 --- /dev/null +++ b/iot/tp3/occitanium-client/occitanium-client.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-client/src/Main.java b/iot/tp3/occitanium-client/src/Main.java new file mode 100644 index 0000000..abb2922 --- /dev/null +++ b/iot/tp3/occitanium-client/src/Main.java @@ -0,0 +1,39 @@ +import org.eclipse.californium.core.CoapClient; +import org.eclipse.californium.core.CoapHandler; +import org.eclipse.californium.core.CoapResponse; + +public class Main { + public static void main(String[] args) throws InterruptedException { + var light_client = new CoapClient("coap://localhost:5683/light"); + var lamp_client = new CoapClient("coap://localhost:5683/light/lamp"); + while (true) { + var relation = light_client.observe(new LampUpdateHandler(lamp_client)); + relation.wait(); + } + } +} + +class LampUpdateHandler implements CoapHandler { + static String ON = "On"; + static String OFF = "Off"; + + CoapClient lamp_client; + public LampUpdateHandler(CoapClient lamp_client_) { + lamp_client = lamp_client_; + } + + @Override + public void onLoad(CoapResponse coapResponse) { + var content = coapResponse.getResponseText(); + var level = Integer.parseInt(content); + System.out.println("Light at " + level); + var state = OFF; + if (level < 5) state = ON; + System.out.println("Sending state " + state); + } + + @Override + public void onError() { + System.err.println("Owno :s"); + } +} diff --git a/iot/tp3/occitanium-server/.gitignore b/iot/tp3/occitanium-server/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/iot/tp3/occitanium-server/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/iot/tp3/occitanium-server/.idea/.gitignore b/iot/tp3/occitanium-server/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/iot/tp3/occitanium-server/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/iot/tp3/occitanium-server/.idea/misc.xml b/iot/tp3/occitanium-server/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/iot/tp3/occitanium-server/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-server/.idea/modules.xml b/iot/tp3/occitanium-server/.idea/modules.xml new file mode 100644 index 0000000..b5cd156 --- /dev/null +++ b/iot/tp3/occitanium-server/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-server/.idea/uiDesigner.xml b/iot/tp3/occitanium-server/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/iot/tp3/occitanium-server/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-server/.idea/vcs.xml b/iot/tp3/occitanium-server/.idea/vcs.xml new file mode 100644 index 0000000..c2365ab --- /dev/null +++ b/iot/tp3/occitanium-server/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-server/Californium.properties b/iot/tp3/occitanium-server/Californium.properties new file mode 100644 index 0000000..a92234a --- /dev/null +++ b/iot/tp3/occitanium-server/Californium.properties @@ -0,0 +1,37 @@ +#Californium CoAP Properties file +#Wed Apr 10 19:09:21 CEST 2024 +ACK_RANDOM_FACTOR=1.5 +ACK_TIMEOUT=2000 +ACK_TIMEOUT_SCALE=2 +CROP_ROTATION_PERIOD=2000 +DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP +DEFAULT_BLOCK_SIZE=512 +DEFAULT_COAP_PORT=5683 +DEFAULT_ENDPOINT_THREAD_COUNT=1 +DEFAULT_LEISURE=5000 +EXCHANGE_LIFECYCLE=247000 +HTTP_CACHE_RESPONSE_MAX_AGE=86400 +HTTP_CACHE_SIZE=32 +HTTP_PORT=8080 +HTTP_SERVER_SOCKET_BUFFER_SIZE=8192 +HTTP_SERVER_SOCKET_TIMEOUT=100000 +MARK_AND_SWEEP_INTERVAL=10000 +MAX_MESSAGE_SIZE=1024 +MAX_RETRANSMIT=4 +MAX_TRANSMIT_WAIT=93000 +NOTIFICATION_CHECK_INTERVAL=86400000 +NOTIFICATION_CHECK_INTERVAL_COUNT=100 +NOTIFICATION_MAX_AGE=128000 +NOTIFICATION_REREGISTRATION_BACKOFF=2000 +NSTART=1 +PROBING_RATE=1.0 +SERVER_THRESD_NUMER=8 +UDP_CONNECTOR_DATAGRAM_SIZE=2000 +UDP_CONNECTOR_LOG_PACKETS=false +UDP_CONNECTOR_OUT_CAPACITY=2147483647 +UDP_CONNECTOR_RECEIVER_THREAD_COUNT=1 +UDP_CONNECTOR_RECEIVE_BUFFER=0 +UDP_CONNECTOR_SENDER_THREAD_COUNT=1 +UDP_CONNECTOR_SEND_BUFFER=0 +USE_RANDOM_MID_START=true +USE_RANDOM_TOKEN_START=true diff --git a/iot/tp3/occitanium-server/lib/californium-core-1.0.0-SNAPSHOT.jar b/iot/tp3/occitanium-server/lib/californium-core-1.0.0-SNAPSHOT.jar new file mode 100755 index 0000000..d96b658 Binary files /dev/null and b/iot/tp3/occitanium-server/lib/californium-core-1.0.0-SNAPSHOT.jar differ diff --git a/iot/tp3/occitanium-server/lib/element-connector-1.0-SNAPSHOT.jar b/iot/tp3/occitanium-server/lib/element-connector-1.0-SNAPSHOT.jar new file mode 100755 index 0000000..f8ef714 Binary files /dev/null and b/iot/tp3/occitanium-server/lib/element-connector-1.0-SNAPSHOT.jar differ diff --git a/iot/tp3/occitanium-server/occitanium-server.iml b/iot/tp3/occitanium-server/occitanium-server.iml new file mode 100644 index 0000000..be4e9f8 --- /dev/null +++ b/iot/tp3/occitanium-server/occitanium-server.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/iot/tp3/occitanium-server/src/Main.java b/iot/tp3/occitanium-server/src/Main.java new file mode 100644 index 0000000..4e20bea --- /dev/null +++ b/iot/tp3/occitanium-server/src/Main.java @@ -0,0 +1,157 @@ +import org.eclipse.californium.core.CoapResource; +import org.eclipse.californium.core.CoapServer; +import org.eclipse.californium.core.coap.CoAP; +import org.eclipse.californium.core.server.resources.CoapExchange; + +public class Main { + public static void main(String[] args) { + var server = new CoapServer(5683); + server.add(new HelloWorldResource()); + server.add(new TimeResource()); + server.add(new SubPathResource()); + server.add(new ModifiableResource()); + server.add(new DeletableResource()); + server.add(new LightResource()); + server.start(); + } +} + + +class HelloWorldResource extends CoapResource { + public HelloWorldResource() { + super("hello-world"); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.CONTENT, "hello world"); + } +} + + +class TimeResource extends CoapResource { + public TimeResource() { + super("time"); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.CONTENT, Long.toString(System.currentTimeMillis())); + } +} + + +class SubPathResource extends CoapResource { + public SubPathResource() { + super("subpath"); + add(new AnotherResource()); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.CONTENT, "subpath"); + } +} + + +class AnotherResource extends CoapResource { + public AnotherResource() { + super("another"); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.CONTENT, "another"); + } +} + + +class ModifiableResource extends CoapResource { + private String value; + public ModifiableResource() { + super("modifiable"); + value = "Start"; + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.CONTENT, value); + } + + @Override + public void handlePUT(CoapExchange exchange) { + value = exchange.getRequestText(); + exchange.respond(CoAP.ResponseCode.CHANGED, value); + } +} + + +class DeletableResource extends CoapResource { + public DeletableResource() { + super("deletable"); + } + + @Override + public void handleGET(CoapExchange exchange) { + exchange.respond(CoAP.ResponseCode.DELETED, "Deleted resource."); + } +} + + +class LightResource extends CoapResource { + public LightResource() { + super("Light"); + brightness = 6; + add(new LampResource()); + setObservable(true); + } + + Integer brightness; + + @Override + public void handleGET(CoapExchange exchange) { + System.out.println("access detected"); + exchange.respond(CoAP.ResponseCode.CONTENT, brightness.toString()); + } + + @Override + public void handlePUT(CoapExchange exchange) { + brightness = Integer.parseInt(exchange.getRequestText()); + exchange.respond(CoAP.ResponseCode.CHANGED, brightness.toString()); + } +} + +enum LampState { + On("On"), + Off("Off"); + + private String text; + private LampState(String text_) { + text = text_; + } + + @Override + public String toString() { + return text; + } + + public static LampState fromString(String text_) { + if (text_.equals("On")) return LampState.On; + if (text_.equals("Off")) return LampState.Off; + return null; + } +} + +class LampResource extends CoapResource { + private LampState state = LampState.On; + public LampResource() { + super("lamp"); + } + + @Override + public void handlePUT(CoapExchange exchange) { + var input = LampState.fromString(exchange.getRequestText()); + if (input == null) return; + exchange.respond(CoAP.ResponseCode.CHANGED, state.toString()); + } +} \ No newline at end of file