-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added zeromq listeners and async tasks
- Loading branch information
ZDmitry
committed
Dec 26, 2016
1 parent
e9d34bd
commit 82ad46a
Showing
7 changed files
with
154 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.zeromq.rnzeromq"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
android/src/main/java/org/zeromq/rnzeromq/MessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.zeromq.rnzeromq; | ||
|
||
import android.os.Handler; | ||
import android.os.Message; | ||
|
||
class MessageHandler extends Handler { | ||
|
||
private final EventEmitter emitter = EventEmitter.getInstance(); | ||
|
||
@Override | ||
public void handleMessage(Message msg) { | ||
emitter.emit("zeromq.recvd", (String)msg.obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
android/src/main/java/org/zeromq/rnzeromq/ZeroMQServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.zeromq.rnzeromq; | ||
|
||
import android.os.Handler; | ||
|
||
import org.zeromq.ZMQ; | ||
|
||
|
||
class ZeroMQServer implements Runnable { | ||
|
||
private final String _serverAddress; | ||
private final Handler _threadHandler = new MessageHandler(); | ||
|
||
ZeroMQServer(final String serverAddr) { | ||
_serverAddress = serverAddr; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
ZMQ.Context context = ZMQ.context(1); | ||
ZMQ.Socket socket = context.socket(ZMQ.REP); | ||
|
||
socket.bind(_serverAddress); | ||
while(!Thread.currentThread().isInterrupted()) { | ||
byte[] msg = socket.recv(0); | ||
if (msg.length > 0) { | ||
_threadHandler.sendMessage(_threadHandler.obtainMessage(0, new String(msg))); | ||
socket.send("", 0); | ||
} | ||
} | ||
|
||
socket.close(); | ||
context.term(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.zeromq.rnzeromq; | ||
|
||
import android.os.AsyncTask; | ||
import android.os.Handler; | ||
|
||
import org.zeromq.ZMQ; | ||
|
||
|
||
class ZeroMQTask extends AsyncTask<String, Void, String> { | ||
|
||
private final String _serverAddress; | ||
private Handler _threadHandler; | ||
|
||
ZeroMQTask(final String serverAddress, final Handler handler) { | ||
_serverAddress = serverAddress; | ||
_threadHandler = handler; | ||
|
||
if (_threadHandler == null) { | ||
_threadHandler = new MessageHandler(); | ||
} | ||
} | ||
|
||
@Override | ||
protected String doInBackground(String... params) { | ||
ZMQ.Context context = ZMQ.context(1); | ||
ZMQ.Socket socket = context.socket(ZMQ.REQ); | ||
socket.connect(_serverAddress); | ||
|
||
socket.send(params[0].getBytes(), 0); | ||
String result = new String(socket.recv(0)); | ||
|
||
socket.close(); | ||
context.term(); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(String result) { | ||
_threadHandler.sendMessage(_threadHandler.obtainMessage(0, result)); | ||
} | ||
} |