Skip to content

Commit

Permalink
Merge pull request #7 from eleme/feature/queue
Browse files Browse the repository at this point in the history
Feature/queue
  • Loading branch information
Xiaofei-it committed Aug 31, 2016
2 parents 9a6b260 + 675152f commit 43dc169
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 59 deletions.
4 changes: 2 additions & 2 deletions README-zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void showText(String text) {

```
dependencies {
compile 'xiaofei.library:hermes-eventbus:0.1.1'
compile 'xiaofei.library:hermes-eventbus:0.2.0'
}
```

Expand All @@ -61,7 +61,7 @@ dependencies {
<dependency>
<groupId>xiaofei.library</groupId>
<artifactId>hermes-eventbus</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
```
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Add the following into your gradle file:

```
dependencies {
compile 'xiaofei.library:hermes-eventbus:0.1.6'
compile 'xiaofei.library:hermes-eventbus:0.2.0'
}
```

Expand All @@ -74,7 +74,7 @@ For maven, please use the following:
<dependency>
<groupId>xiaofei.library</groupId>
<artifactId>hermes-eventbus</artifactId>
<version>0.1.6</version>
<version>0.2.0</version>
<type>pom</type>
</dependency>
```
Expand Down
8 changes: 4 additions & 4 deletions hermes-eventbus/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/Xiaofei-it/HermesEventBus-OriginalRepo'
gitUrl = 'https://github.com/Xiaofei-it/HermesEventBus-OriginalRepo.git'

libraryVersion = '0.1.6'
libraryVersion = '0.2.0'

developerId = 'Xiaofei-it'
developerName = 'Eric Zhao'
Expand All @@ -31,8 +31,8 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 23
versionCode 2
versionName "0.1.6"
versionCode 3
versionName "0.2.0"
}
buildTypes {
release {
Expand All @@ -48,7 +48,7 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'xiaofei.library:hermes:0.6.1'
compile 'xiaofei.library:concurrent-utils:0.1.3'
compile 'xiaofei.library:concurrent-utils:0.1.4'
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import org.greenrobot.eventbus.EventBus;

import xiaofei.library.concurrentutils.ObjectCanary;
import xiaofei.library.concurrentutils.ObjectCanary2;
import xiaofei.library.concurrentutils.util.Action;
import xiaofei.library.concurrentutils.util.Function;
import xiaofei.library.hermes.Hermes;
Expand All @@ -40,10 +40,6 @@ public class HermesEventBus {

private static final String HERMES_SERVICE_DISCONNECTED = "Hermes service disconnected!";

private static final String UNKNOWN_ERROR = "An unknown error occurred. Please report this to the author.";

private static final int STATE_UNDEFINED = -1;

private static final int STATE_DISCONNECTED = 0;

private static final int STATE_CONNECTING = 1;
Expand All @@ -58,39 +54,33 @@ public class HermesEventBus {

private volatile boolean mMainProcess;

private volatile ObjectCanary<IMainService> mRemoteApis;
private volatile ObjectCanary2<IMainService> mRemoteApis;

private volatile MainService mMainApis;

private volatile int mState = STATE_UNDEFINED;
private volatile int mState = STATE_DISCONNECTED;

/**
* TODO
*
* 1. Consider more about the interleaving, especially when the service is being connected or disconnected.
*
* 2. I should solve the following problems in the future:
* 2. Pay attention to the following cases:
*
* (1) Before the connection succeeds, e1, e2 and e3 are put into the queue.
* Then when the connection succeeds, they are posted one by one.
* However, after e1 is posted, we post another event e4.
* How can I guarantee that e4 is posted after e3?
* I should guarantee that e4 is posted after e3.
*
* (2) Before the connection succeeds, some sticky events (e1, e2 and e3)
* are put into the queue.
* Then when the connection succeeds, they are posted one by one.
* However, after e1 is posted, we get a sticky event.
* How can I guarantee that we get e3 rather than e1?
*
* I have made some modifications in the concurrent library but has not imported it
* into HermesEventBus.
* Work remains to be done in the future.
*
* I should guarantee that we get e3 rather than e1.
*/

private HermesEventBus() {
mEventBus = EventBus.getDefault();
mRemoteApis = new ObjectCanary<IMainService>();
mRemoteApis = new ObjectCanary2<IMainService>();
}

public static HermesEventBus getDefault() {
Expand Down Expand Up @@ -122,7 +112,7 @@ private static String getCurrentProcessName(Context context) {
}

public void init(Context context) {
mContext = context;
mContext = context.getApplicationContext();
mMainProcess = isMainProcess(context.getApplicationContext());
if (mMainProcess) {
Hermes.init(context);
Expand All @@ -137,7 +127,7 @@ public void init(Context context) {
}

public void connectApp(Context context, String packageName) {
mContext = context;
mContext = context.getApplicationContext();
mMainProcess = false;
mState = STATE_CONNECTING;
Hermes.setHermesListener(new HermesListener());
Expand Down Expand Up @@ -169,17 +159,13 @@ private void actionInternal(final Action<IMainService> action) {
} else {
if (mState == STATE_DISCONNECTED) {
Log.w(TAG, HERMES_SERVICE_DISCONNECTED);
} else if (mState == STATE_CONNECTING) {
mRemoteApis.actionNonNullNonBlocking(new Action<IMainService>() {
} else {
mRemoteApis.action(new Action<IMainService>() {
@Override
public void call(IMainService o) {
action.call(o);
}
});
} else if (mState == STATE_CONNECTED) {
action.call(mRemoteApis.get());
} else {
throw new IllegalStateException(UNKNOWN_ERROR);
}
}
}
Expand All @@ -191,17 +177,13 @@ private <T> T calculateInternal(final Function<IMainService, ? extends T> functi
if (mState == STATE_DISCONNECTED) {
Log.w(TAG, HERMES_SERVICE_DISCONNECTED);
return null;
} else if (mState == STATE_CONNECTING) {
return mRemoteApis.calculateNonNull(new Function<IMainService, T>() {
} else {
return mRemoteApis.calculate(new Function<IMainService, T>() {
@Override
public T call(IMainService o) {
return function.call(o);
}
});
} else if (mState == STATE_CONNECTED) {
return function.call(mRemoteApis.get());
} else {
throw new IllegalStateException(UNKNOWN_ERROR);
}
}
}
Expand Down Expand Up @@ -311,14 +293,16 @@ public void call(IMainService o) {
@Override
public void onHermesDisconnected(Class<? extends HermesService> service) {
// Log.v(TAG, "Hermes disconnected in Process " + Process.myPid());
mState = STATE_DISCONNECTED;
mRemoteApis.action(new Action<IMainService>() {
@Override
public void call(IMainService o) {
o.unregister(Process.myPid());
}
});
mRemoteApis.set(null);
mState = STATE_DISCONNECTED;
// I deleted the statement which assigns null to mRemoteApis.
// Then, if the service is disconnected, the pending events will still be posted
// but this process will not receive them any more.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,4 @@ public interface ISubService {
@MethodId("cancelEventDelivery")
void cancelEventDelivery(Object event);

@MethodId("removeStickyEvent")
boolean removeStickyEvent(Object event);

@MethodId("removeAllStickyEvents")
void removeAllStickyEvents();

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,6 @@ public void post(Object event) {
mEventBus.post(event);
}

@MethodId("removeStickyEvent")
@Override
public boolean removeStickyEvent(Object event) {
return mEventBus.removeStickyEvent(event);
}

@MethodId("removeAllStickyEvents")
@Override
public void removeAllStickyEvents() {
mEventBus.removeAllStickyEvents();
}

@MethodId("cancelEventDelivery")
@Override
public void cancelEventDelivery(Object event) {
Expand Down

0 comments on commit 43dc169

Please sign in to comment.