Skip to content

Commit

Permalink
Repeat UDP datagrams to improve reliability, and add some logging beh…
Browse files Browse the repository at this point in the history
…ind a debug flag
  • Loading branch information
arashpayan committed Jun 16, 2016
1 parent c7ded2c commit 742603a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ dependencies {
```

## Usage
#### Listening for services:
Listening for services:
```
String serviceName = "com.example.service"; // use "*" to listen for all services
ChirpBrowser browser = Chirp.browseFor(serviceName).
listener(this).
start(getApplication());
```

#### Publishing a service:
Publishing a service:
```
ChirpPublisher publisher = Chirp.publish("com.example.service").
start(getApplication());
Expand Down
1 change: 1 addition & 0 deletions chirp/src/main/java/com/arashpayan/chirp/Chirp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Chirp {
protected static final Gson sGson = new GsonBuilder().
setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).
create();
public static boolean Debug = false;

private Chirp() {
}
Expand Down
18 changes: 17 additions & 1 deletion chirp/src/main/java/com/arashpayan/chirp/ChirpBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ private void listen(ChirpSocket socket) throws UnsupportedEncodingException {
if (msg == null) {
continue;
}
if (msg.senderId.equals(mId)) {
continue;
}
mIncomingMessages.offer(msg);
}

socket.close();
logi("finishing listen");
}

private void notifyServiceDiscovered(@NonNull final Service service) {
Expand Down Expand Up @@ -318,7 +320,11 @@ protected void setHandler(Handler h) {
* to avoid memory leaks from an <code>Activity</code> being passed in
*/
public void start(@NonNull Application app) {
if (Chirp.Debug) {
logi("ChirpBrowser.start");
}
if (mIsStarted) {
logi("ChirpBrowser.start: already started");
return;
}

Expand All @@ -337,6 +343,7 @@ public void start(@NonNull Application app) {
mExecutor.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("V4Conn");
try {
mSocket4 = new ChirpSocket(false);
listen(mSocket4);
Expand All @@ -348,6 +355,7 @@ public void run() {
mExecutor.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("V6Conn");
try {
mSocket6 = new ChirpSocket(true);
listen(mSocket6);
Expand All @@ -359,6 +367,7 @@ public void run() {
mExecutor.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("MessageHandler");
try {
handleMessages();
} catch (Throwable t) {
Expand All @@ -369,6 +378,7 @@ public void run() {
mExecutor.execute(new Runnable() {
@Override
public void run() {
Thread.currentThread().setName("ExpirationTicker");
try {
Message expCheck = new Message();
expCheck.type = Message.QUEUE_EXPIRATION_CHECK;
Expand All @@ -390,7 +400,13 @@ public void run() {
* not be reused.
*/
public void stop() {
if (Chirp.Debug) {
logi("ChirpBrowser.stop");
}
if (!mIsStarted) {
if (Chirp.Debug) {
logi("ChirpBrowser.stop already stopped");
}
return;
}

Expand Down
22 changes: 16 additions & 6 deletions chirp/src/main/java/com/arashpayan/chirp/ChirpSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ protected Message read() {
return null;
}
if (mReadPacket.getLength() == 0) {
if (Chirp.Debug) {
logi("read: received 0 length packet");
}
return null;
}
// String str = null;
// try {
// str = new String(mReadBuf, 0, mReadPacket.getLength(), "utf-8");
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
if (Chirp.Debug) {
String str = null;
try {
str = new String(mReadBuf, 0, mReadPacket.getLength(), "utf-8");
logi(Thread.currentThread().getName() + " read: " + str);
} catch (UnsupportedEncodingException e) {
logi("failed to convert message to string: " + e.getMessage());
}
}
ByteArrayInputStream bais = new ByteArrayInputStream(mReadBuf, 0, mReadPacket.getLength());
Message msg;
try {
Expand All @@ -113,10 +119,14 @@ protected void send(Message msg) throws IOException {
byte[] bytes = json.getBytes();
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, mGroupAddress, CHIRP_PORT);
mSocket.send(packet);
try {Thread.sleep(20); } catch (InterruptedException ie) {logi("interrupted sleep");};
mSocket.send(packet);
}

protected void send(byte[] bytes) throws IOException {
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, mGroupAddress, CHIRP_PORT);
mSocket.send(packet);
try {Thread.sleep(20); } catch (InterruptedException ie) {logi("interrupted sleep");};
mSocket.send(packet);
}
}
24 changes: 18 additions & 6 deletions demo/src/main/java/com/arashpayan/chirpdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,30 @@ protected void onCreate(Bundle savedInstanceState) {
LinearLayoutManager llm = new LinearLayoutManager(this);
recyclerView.setLayoutManager(llm);
}
Chirp.Debug = false;
}

@Override
protected void onStart() {
super.onStart();

mAdapter.clear();
mChirpBrowser = Chirp.browseFor("*").
listener(mAdapter).
start(getApplication());
listener(mAdapter).
start(getApplication());

mChirpPublisher = Chirp.publish("com.arashpayan.chirp.demo").
start(getApplication());
start(getApplication());
}

@Override
protected void onDestroy() {
super.onDestroy();
protected void onStop() {
super.onStop();

if (mChirpBrowser != null) {
mChirpBrowser.stop();
}
if (mChirpPublisher != null) {
Log.i(TAG, "onDestroy: calling publisher stop");
mChirpPublisher.stop();
}
}
Expand All @@ -79,6 +85,12 @@ class ServicesAdapter extends RecyclerView.Adapter<ServiceBindingHolder> impleme

private ArrayList<Service> mDiscoveredServices = new ArrayList<>();

public void clear() {
int numItems = mDiscoveredServices.size();
mDiscoveredServices.clear();
notifyItemRangeRemoved(0, numItems);
}

@Override
public ServiceBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service, parent, false);
Expand Down

0 comments on commit 742603a

Please sign in to comment.