jvermillard / leshan

OMA Lightweight M2M java implementation - LWM2M
40 stars 25 forks source link

Runtime exception on Android #64

Closed corneliudascalu closed 9 years ago

corneliudascalu commented 9 years ago

I'm getting a RuntimeException about a timeout when running the LeshanClientExample on Android:

java.lang.RuntimeException: Request Timed Out: coap://localhost/rd?ep=4c7458b4-8a77-493f-ac6c-ef3d301d50cb (timeout)
            at leshan.client.californium.impl.CaliforniumLwM2mClientRequestSender$SyncRequestObserver.waitForResponse(CaliforniumLwM2mClientRequestSender.java:236)
            at leshan.client.californium.impl.CaliforniumLwM2mClientRequestSender.send(CaliforniumLwM2mClientRequestSender.java:96)
            at leshan.client.californium.LeshanClient.send(LeshanClient.java:116)
            at com.fortech.lwm2mproto.LeshanClientExample.<init>(LeshanClientExample.java:56)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:27)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:16)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

This is what I'm doing:

new LeshanClientExample("coap://localhost", 9000, "coap://leshan.eclipse.org", 5683);

To clarify, this method is thrown when the client sends the RegisterRequest.

In case it helps, there are some warnings being logged before this exception is thrown:

02-06 17:08:06.999    5442-5460/com.fortech.lwm2mproto I/NetworkConfig﹕ Storing standard properties in file Californium.properties
02-06 17:08:07.002    5442-5460/com.fortech.lwm2mproto W/NetworkConfig﹕ Error while storing properties to /Californium.properties
    java.io.FileNotFoundException: Californium.properties: open failed: EROFS (Read-only file system)
            at libcore.io.IoBridge.open(IoBridge.java:456)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
            at java.io.FileWriter.<init>(FileWriter.java:42)
            at org.eclipse.californium.core.network.config.NetworkConfig.store(NetworkConfig.java:218)
            at org.eclipse.californium.core.network.config.NetworkConfig.store(NetworkConfig.java:205)
            at org.eclipse.californium.core.network.config.NetworkConfig.createStandardWithFile(NetworkConfig.java:170)
            at org.eclipse.californium.core.network.config.NetworkConfig.getStandard(NetworkConfig.java:125)
            at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:115)
            at leshan.client.californium.LeshanClient.<init>(LeshanClient.java:66)
            at com.fortech.lwm2mproto.LeshanClientExample.<init>(LeshanClientExample.java:48)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:27)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:16)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
            at libcore.io.Posix.open(Native Method)
            at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
            at libcore.io.IoBridge.open(IoBridge.java:442)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
            at java.io.FileWriter.<init>(FileWriter.java:42)
            at org.eclipse.californium.core.network.config.NetworkConfig.store(NetworkConfig.java:218)
            at org.eclipse.californium.core.network.config.NetworkConfig.store(NetworkConfig.java:205)
            at org.eclipse.californium.core.network.config.NetworkConfig.createStandardWithFile(NetworkConfig.java:170)
            at org.eclipse.californium.core.network.config.NetworkConfig.getStandard(NetworkConfig.java:125)
            at org.eclipse.californium.core.CoapServer.<init>(CoapServer.java:115)
            at leshan.client.californium.LeshanClient.<init>(LeshanClient.java:66)
            at com.fortech.lwm2mproto.LeshanClientExample.<init>(LeshanClientExample.java:48)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:27)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:16)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
02-06 17:08:07.030    5442-5460/com.fortech.lwm2mproto I/CoapServer﹕ Starting server
02-06 17:08:07.030    5442-5460/com.fortech.lwm2mproto I/CoAPEndpoint﹕ Starting endpoint at coap://localhost:9000
02-06 17:08:07.075    5442-5466/com.fortech.lwm2mproto W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.net.InetAddress.getAddress()' on a null object reference
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.Matcher.sendRequest(Matcher.java:130)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.CoAPEndpoint$OutboxImpl.sendRequest(CoAPEndpoint.java:470)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.CoapStack$StackBottomAdapter.sendRequest(CoapStack.java:207)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.AbstractLayer.sendRequest(AbstractLayer.java:58)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.ReliabilityLayer.sendRequest(ReliabilityLayer.java:104)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.AbstractLayer.sendRequest(AbstractLayer.java:58)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.TokenLayer.sendRequest(TokenLayer.java:50)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.AbstractLayer.sendRequest(AbstractLayer.java:58)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.BlockwiseLayer.sendRequest(BlockwiseLayer.java:139)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.AbstractLayer.sendRequest(AbstractLayer.java:58)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.ObserveLayer.sendRequest(ObserveLayer.java:43)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.AbstractLayer.sendRequest(AbstractLayer.java:58)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.CoapStack$StackTopAdapter.sendRequest(CoapStack.java:165)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.CoapStack$StackTopAdapter.sendRequest(CoapStack.java:159)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.stack.CoapStack.sendRequest(CoapStack.java:118)
02-06 17:08:07.078    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at org.eclipse.californium.core.network.CoAPEndpoint$3.run(CoAPEndpoint.java:393)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-06 17:08:07.079    5442-5466/com.fortech.lwm2mproto W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

This part in particular seems important:

java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.net.InetAddress.getAddress()' on a null object reference

It looks like there is no destination for a request.

Am I doing anything wrong, should I do something differently? Thank you.

jschloman commented 9 years ago

Corneliu,

I apologize for the delayed response but I think I know what the issue is: when creating the LeshanClientExample you pass in URLs of the format "coap://localhost". Looking back at LeshanClientExample (lines 73-74) we create InetSocketAddress(hostname, port) using that information and (as shown in the documentation) only the hostname, localhost, should be passed there instead. Hopefully that works.

-JF

corneliudascalu commented 9 years ago

Thank you very much for the response. I figured that out in the meantime.

Right now, I'm stuck on another error. It seems like the client is not able to send data through the UDP socket. This happens when I run the code on Android or on my local machine. It only works on the local machine if the server is also running locally. I suspect this may be a problem with a firewall or something, even if I tried using the phone's data connection instead of WiFi and still didn't work.

This is the code:

new LeshanClientExample("127.0.0.1", 5686, "leshan.eclipse.org", 5683);

And this is the error log. If you are able to give me any hint, thank you.

02-09 15:37:40.678  15725-15755/com.fortech.lwm2mproto W/UDPConnector﹕ Exception "java.net.SocketException: sendto failed: EINVAL (Invalid argument)" in thread UDP-Sender-/127.0.0.1:5686[0]: running=true
    java.net.SocketException: sendto failed: EINVAL (Invalid argument)
            at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
            at libcore.io.IoBridge.sendto(IoBridge.java:515)
            at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
            at java.net.DatagramSocket.send(DatagramSocket.java:305)
            at org.eclipse.californium.elements.UDPConnector$Sender.work(UDPConnector.java:246)
            at org.eclipse.californium.elements.UDPConnector$Worker.run(UDPConnector.java:187)
     Caused by: android.system.ErrnoException: sendto failed: EINVAL (Invalid argument)
            at libcore.io.Posix.sendtoBytes(Native Method)
            at libcore.io.Posix.sendto(Posix.java:176)
            at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
            at libcore.io.IoBridge.sendto(IoBridge.java:513)
            at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
            at java.net.DatagramSocket.send(DatagramSocket.java:305)
            at org.eclipse.californium.elements.UDPConnector$Sender.work(UDPConnector.java:246)
            at org.eclipse.californium.elements.UDPConnector$Worker.run(UDPConnector.java:187)
02-09 15:37:41.214  15725-15749/com.fortech.lwm2mproto E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.fortech.lwm2mproto, PID: 15725
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: Request Timed Out: coap://localhost/rd?ep=e0d7e954-f44d-47a8-9b49-a9a3a3780d59 (timeout)
            at leshan.client.californium.impl.CaliforniumLwM2mClientRequestSender$SyncRequestObserver.waitForResponse(CaliforniumLwM2mClientRequestSender.java:236)
            at leshan.client.californium.impl.CaliforniumLwM2mClientRequestSender.send(CaliforniumLwM2mClientRequestSender.java:96)
            at leshan.client.californium.LeshanClient.send(LeshanClient.java:116)
            at leshan.client.example.LeshanClientExample.<init>(LeshanClientExample.java:103)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:28)
            at com.fortech.lwm2mproto.MainActivity$1.doInBackground(MainActivity.java:17)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
jschloman commented 9 years ago

Corneliu,

I think the issue if you are going to leshan.eclipse.org is that you need set your client-side hostname (parameter 1) to the externally available host the server should connect back to you on instead of 127.0.0.1. Right now it would get the request but then try to send it to it's own localhost which isn't where your device is (I assume :smile: ).

corneliudascalu commented 9 years ago

Thanks. Does that mean that any device that is behind a router and doesn't have a static IP address cannot run a client?

mrinalsharma commented 9 years ago

By looking at the error it seems like you are not able to access Californium.properties. 5442-5460/com.fortech.lwm2mproto W/NetworkConfig﹕ Error while storing properties to /Californium.properties To resolve this error you would have to create a new CoapServer(NetworkConfig.createStandardWithoutFile()). The important thing to note is that you are not using file to set Californium properties. In next step, pass this newly created CoapServer as parameter to LeshanClient() constructor. I hope this should help. Thanks,Mrinal Sharma

 On Monday, February 9, 2015 9:33 AM, Corneliu Dascalu <notifications@github.com> wrote:

Thanks. Does that mean that any device that is behind a router and doesn't have a static IP address cannot run a client?— Reply to this email directly or view it on GitHub.

mrinalsharma commented 9 years ago

Hello Cornelie, I have tested this on Android and it works even if device is behind firewall. You would have to replace 127.0.0.1 with your client side IP. Thanks,Mrinal Sharma

 On Monday, February 9, 2015 9:11 AM, John <notifications@github.com> wrote:

Corneliu,I think the issue if you are going to leshan.eclipse.org is that you need set your client-side hostname (parameter 1) to the externally available host the server should connect back to you on instead of 127.0.0.1. Right now it would get the request but then try to send it to it's own localhost which isn't where your device is (I assume ).— Reply to this email directly or view it on GitHub.

jschloman commented 9 years ago

@corneliudascalu : my layer 4 knowledge is wanting but the fact that we are using UDP can cause routers to be big problems since it isn't like we are creating a nice TCP socket (of course once TCP is added to the protocol... which is something that is currently being worked on by the IETF to add to CoAP as we speak :smiley: ).

corneliudascalu commented 9 years ago

@mrinalsharma Thanks a million! It worked, the problem was the local IP of the phone.