11package com .tns ;
22
33import android .content .Context ;
4+ import android .os .Handler ;
45import android .util .Log ;
56
67import java .io .IOException ;
8+ import java .util .concurrent .ConcurrentLinkedQueue ;
9+ import java .util .concurrent .LinkedBlockingQueue ;
10+ import java .util .concurrent .atomic .AtomicBoolean ;
711
812import fi .iki .elonen .NanoHTTPD ;
913import fi .iki .elonen .NanoWSD ;
1014
1115public class AndroidJsV8Inspector
1216{
17+ public static final String DISCONNECT_MESSAGE = "nativescript-inspector-disconnect" ;
1318 private JsV8InspectorServer server ;
1419 private Logger logger ;
15- private Context context ;
20+ private Context context ;
1621
1722 protected native final void init ();
23+
1824 protected native final void connect (Object connection );
19- protected native final void disconnect ();
20- protected native final void dispatchMessage (String message );
2125
26+ protected static native final void disconnect ();
2227
23- public AndroidJsV8Inspector (Context context , Logger logger )
24- {
25- this .context = context ;
26- this .logger = logger ;
27- }
28+ protected native final void dispatchMessage (String message );
29+ protected Handler mainHandler ;
30+ private LinkedBlockingQueue <String > inspectorMessages = new LinkedBlockingQueue <String >();
31+
32+ public AndroidJsV8Inspector (Context context , Logger logger )
33+ {
34+ this .context = context ;
35+ this .logger = logger ;
36+ }
2837
2938 public void start () throws IOException
3039 {
3140 if (this .server == null )
3241 {
42+ Runtime currentRuntime = Runtime .getCurrentRuntime ();
43+
44+ mainHandler = currentRuntime .getHandler ();
45+
3346 this .server = new JsV8InspectorServer (context .getPackageName () + "-inspectorServer" );
3447 this .server .start (-1 );
3548
49+ Log .d ("V8Inspector" , "init ThreadId:" + Thread .currentThread ().getId ());
50+
3651 init ();
3752 }
3853 }
3954
4055 private static void send (Object connection , String payload ) throws IOException
4156 {
42- ((JsV8InspectorWebSocket )connection ).send (payload );
57+ ((JsV8InspectorWebSocket ) connection ).send (payload );
58+ }
59+
60+ private static String getInspectorMessage (Object connection )
61+ {
62+ return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
4363 }
4464
4565 class JsV8InspectorServer extends NanoWSD
@@ -74,23 +94,89 @@ public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
7494 @ Override
7595 protected void onOpen ()
7696 {
77- Log .d ("V8Inspector" , "onOpen" );
78- connect (this );
97+ Log .d ("V8Inspector" , "onOpen: ThreadID: " + Thread .currentThread ().getId ());
98+
99+ final Object waitObject = new Object ();
100+
101+ mainHandler .post (new Runnable ()
102+ {
103+ @ Override
104+ public void run ()
105+ {
106+ try
107+ {
108+ Log .d ("V8Inspector" , "onOpen: runnable ThreadID : " + Thread .currentThread ().getId ());
109+ connect (JsV8InspectorWebSocket .this );
110+ }
111+ finally
112+ {
113+ synchronized (waitObject )
114+ {
115+ waitObject .notify ();
116+ }
117+ }
118+ }
119+ });
120+
121+ try
122+ {
123+ synchronized (waitObject )
124+ {
125+ waitObject .wait ();
126+ }
127+ }
128+ catch (InterruptedException e )
129+ {
130+ e .printStackTrace ();
131+ }
79132 }
80133
81134 @ Override
82135 protected void onClose (NanoWSD .WebSocketFrame .CloseCode code , String reason , boolean initiatedByRemote )
83136 {
84137 Log .d ("V8Inspector" , "onClose" );
85- disconnect ();
138+ mainHandler .post (new Runnable ()
139+ {
140+ @ Override
141+ public void run ()
142+ {
143+ disconnect ();
144+ }
145+ });
146+
147+ inspectorMessages .offer (DISCONNECT_MESSAGE );
86148 }
87149
88150 @ Override
89- protected void onMessage (NanoWSD .WebSocketFrame message )
151+ protected void onMessage (final NanoWSD .WebSocketFrame message )
90152 {
91153 Log .d ("V8Inspector" , "onMessage" );
92154 Log .d ("V8Inspector" , "onMessage TextPayload" + message .getTextPayload () + " ThreadId:" + Thread .currentThread ().getId ());
93- dispatchMessage (message .getTextPayload ());
155+ inspectorMessages .offer (message .getTextPayload ());
156+
157+ mainHandler .post (new Runnable ()
158+ {
159+ @ Override
160+ public void run ()
161+ {
162+ String nextMessage = inspectorMessages .poll ();
163+ while (nextMessage != null )
164+ {
165+ dispatchMessage (nextMessage );
166+ nextMessage = inspectorMessages .poll ();
167+ }
168+ }
169+ });
170+
171+ // mainHandler.post(new Runnable()
172+ // {
173+ // @Override
174+ // public void run()
175+ // {
176+ //
177+ // dispatchMessage(message.getTextPayload());
178+ // }
179+ // });
94180 }
95181
96182 @ Override
@@ -100,6 +186,29 @@ public void send(String payload) throws IOException
100186 super .send (payload );
101187 }
102188
189+ public String getInspectorMessage ()
190+ {
191+ try
192+ {
193+ String message = inspectorMessages .take ();
194+
195+ if (message != null && message .equalsIgnoreCase (DISCONNECT_MESSAGE ))
196+ {
197+ disconnect ();
198+ return null ;
199+ }
200+
201+ return message ;
202+ }
203+ catch (InterruptedException e )
204+ {
205+ e .printStackTrace ();
206+ }
207+
208+ return null ;
209+ }
210+
211+
103212 @ Override
104213 protected void onPong (NanoWSD .WebSocketFrame pong )
105214 {
0 commit comments