Skip to content

Commit 1ee9af7

Browse files
committed
Origin Manager refactoring
1 parent c6a32ff commit 1ee9af7

9 files changed

Lines changed: 457 additions & 64 deletions

File tree

‎java/pom.xml‎

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
11
<?xml version="1.0"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
45

5-
<groupId>com.pubnub</groupId>
6-
<artifactId>pubnub</artifactId>
7-
<version>3.7.2</version>
6+
<groupId>com.pubnub</groupId>
7+
<artifactId>pubnub</artifactId>
8+
<version>3.7.2</version>
89

9-
<packaging>jar</packaging>
10-
<name>pubnub</name>
11-
<description>PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!</description>
12-
<url>https://github.com/pubnub/java</url>
13-
<licenses>
14-
<license>
15-
<url>https://github.com/pubnub/pubnub-api/blob/master/LICENSE</url>
16-
<distribution>repo</distribution>
17-
</license>
18-
</licenses>
19-
<scm>
10+
<packaging>jar</packaging>
11+
<name>pubnub</name>
12+
<description>PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
13+
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
14+
second!
15+
</description>
2016
<url>https://github.com/pubnub/java</url>
21-
<connection>scm:git:git://github.com/pubnub/java.git</connection>
22-
</scm>
23-
<developers>
24-
<developer>
25-
<id>pubnub</id>
26-
<name>Pubnub Inc.</name>
27-
<email>support@pubnub.com</email>
28-
<url>http://www.pubnub.com</url>
29-
</developer>
30-
</developers>
31-
17+
<licenses>
18+
<license>
19+
<url>https://github.com/pubnub/pubnub-api/blob/master/LICENSE</url>
20+
<distribution>repo</distribution>
21+
</license>
22+
</licenses>
23+
<scm>
24+
<url>https://github.com/pubnub/java</url>
25+
<connection>scm:git:git://github.com/pubnub/java.git</connection>
26+
</scm>
27+
<developers>
28+
<developer>
29+
<id>pubnub</id>
30+
<name>Pubnub Inc.</name>
31+
<email>support@pubnub.com</email>
32+
<url>http://www.pubnub.com</url>
33+
</developer>
34+
</developers>
35+
<properties>
36+
<powermock.version>1.6.2</powermock.version>
37+
</properties>
3238
<dependencies>
3339
<dependency>
3440
<groupId>org.json</groupId>
@@ -45,5 +51,65 @@
4551
<artifactId>bcprov-jdk16</artifactId>
4652
<version>1.46</version>
4753
</dependency>
54+
<dependency>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
<version>4.12</version>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.hamcrest</groupId>
62+
<artifactId>hamcrest-all</artifactId>
63+
<version>1.3</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.mockito</groupId>
68+
<artifactId>mockito-all</artifactId>
69+
<version>1.10.19</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.powermock</groupId>
74+
<artifactId>powermock-api-mockito</artifactId>
75+
<version>${powermock.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.powermock</groupId>
80+
<artifactId>powermock-core</artifactId>
81+
<version>${powermock.version}</version>
82+
<scope>test</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>org.powermock</groupId>
86+
<artifactId>powermock-module-junit4</artifactId>
87+
<version>${powermock.version}</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.powermock</groupId>
92+
<artifactId>powermock-module-junit4-common</artifactId>
93+
<version>${powermock.version}</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.powermock</groupId>
98+
<artifactId>powermock-module-test-mockito-junit4-agent</artifactId>
99+
<version>${powermock.version}</version>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>org.powermock</groupId>
104+
<artifactId>powermock-module-junit4-rule</artifactId>
105+
<version>${powermock.version}</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.powermock</groupId>
110+
<artifactId>powermock-classloading-xstream</artifactId>
111+
<version>${powermock.version}</version>
112+
<scope>test</scope>
113+
</dependency>
48114
</dependencies>
49115
</project>

‎java/src1/com/pubnub/api/OriginManager.java‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OriginManager {
1212
private int failbackOriginManager;
1313
private PubnubCoreShared app;
1414
private String http;
15+
private Callback callback;
1516

1617
public OriginManager(PubnubCoreShared app) {
1718
this.app = app;
@@ -22,37 +23,33 @@ public OriginManager(PubnubCoreShared app) {
2223
this.maxRetriesAfterFailure = app.getOriginManagerIntervalAfterFailure();
2324
}
2425

25-
public void start() {
26-
try {
27-
startCurrentOriginManager();
28-
startFailbackOriginManager();
29-
} catch (PubnubException e) {
30-
stop();
31-
restartSubscription();
32-
}
26+
public synchronized void start() {
27+
startCurrentOriginManager();
28+
startFailbackOriginManager();
3329
}
3430

35-
public void stop() {
31+
public synchronized void stop() {
3632
stopCurrentOriginManager();
3733
stopFailbackOriginManager();
3834
}
3935

40-
private void startCurrentOriginManager() throws PubnubException {
36+
private void startCurrentOriginManager() {
4137
OriginsPool originsPool = app.getOriginsPool();
4238

43-
if (originsPool != null && app.getOriginsPool().size() > 2) {
39+
if (originsPool != null && app.getOriginsPool().size() >= 2) {
4440
currentOriginManager =
4541
app.getTimedTaskManager().addTask("Current Origin Manager", new CurrentOriginManager(interval));
4642
log.verbose("Current Origin Manager started");
4743
} else {
48-
throw new PubnubException("Origins Pool set should be assigned before #enableOriginManager() method invocation");
44+
invokeErrorCallback(PubnubError.PNERROBJ_OM_NOT_ENOUGH_ORIGINS);
4945
}
5046
}
5147

5248
private void startFailbackOriginManager() {
5349
if (deadOrigins.size() != 0) {
5450
failbackOriginManager =
5551
app.getTimedTaskManager().addTask("Failback Manager", new FailbackOriginManager(interval));
52+
restartSubscription();
5653
log.verbose("Failback Origin Manager started");
5754
}
5855
}
@@ -247,6 +244,16 @@ public void handleError(HttpRequest hreq, PubnubError error) {
247244
app._request(hreq, app.nonSubscribeManager);
248245
}
249246

247+
public synchronized void setCallback(Callback callback) {
248+
this.callback = callback;
249+
}
250+
251+
private synchronized void invokeErrorCallback(PubnubError error) {
252+
if (this.callback != null) {
253+
this.callback.errorCallback("", error);
254+
}
255+
}
256+
250257
public void restartSubscription() {
251258
app.disconnectAndResubscribe();
252259
}

‎java/src1/com/pubnub/api/PubnubCoreShared.java‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class PubnubCoreShared extends PubnubCore {
2727
private OriginsPool originsPool;
2828
private OriginManager originManager;
2929

30+
private static Logger log = new Logger(PubnubCoreShared.class);
31+
3032
/**
3133
* Pubnub Constructor
3234
*
@@ -211,6 +213,14 @@ public OriginsPool getOriginsPool() {
211213
return originsPool;
212214
}
213215

216+
public void setOriginsPool(String[] originsPoolArray) throws PubnubException {
217+
setOriginsPool(new LinkedHashSet<String>(Arrays.asList(originsPoolArray)));
218+
}
219+
220+
public void setOriginsPool(String[] originsPoolArray, boolean explicitlyEnableOriginManager) throws PubnubException {
221+
setOriginsPool(new LinkedHashSet<String>(Arrays.asList(originsPoolArray)), explicitlyEnableOriginManager);
222+
}
223+
214224
public void setOriginsPool(LinkedHashSet<String> originsPool) throws PubnubException {
215225
setOriginsPool(originsPool, false);
216226
}
@@ -219,8 +229,6 @@ public void setOriginsPool(LinkedHashSet<String> originsPool, boolean explicitly
219229
throws PubnubException {
220230

221231
this.originsPool.set(originsPool);
222-
stopOriginManager();
223-
startOriginManager();
224232

225233
if (explicitlyEnableOriginManager) {
226234
this.originManagerExplicitlyEnabled = true;
@@ -233,7 +241,6 @@ public void setOriginsPool(LinkedHashSet<String> originsPool, boolean explicitly
233241
*/
234242
protected void startOriginManager() {
235243
getOriginManager().start();
236-
disconnectAndResubscribe();
237244
}
238245

239246
/**
@@ -253,22 +260,33 @@ public synchronized void triggerOriginManager() {
253260

254261
if (originManagerExplicitlyEnabled && !isOriginManagerRunning && isCurrentlySubscribed) {
255262
startOriginManager();
263+
this.ORIGIN_STR = null;
256264
} else if ((!originManagerExplicitlyEnabled && isOriginManagerRunning)
257265
|| (isOriginManagerRunning && !isCurrentlySubscribed)) {
258266
stopOriginManager();
267+
this.ORIGIN_STR = null;
259268
}
260269
}
261270

262271
public boolean isOriginManagerRunning() {
263272
return getOriginManager().isCurrentOriginManagerRunning();
264273
}
265274

275+
public void enableOriginManager() throws PubnubException {
276+
enableOriginManager(new Callback() {
277+
@Override
278+
public void errorCallback(String channel, PubnubError error) {
279+
log.verbose(error.getErrorString());
280+
}
281+
});
282+
}
283+
266284
/**
267285
* Explicitly enables Origin Manager.
268286
*
269-
* @throws PubnubException if origins pool is not assigned yet
270287
*/
271-
public void enableOriginManager() throws PubnubException {
288+
public void enableOriginManager(Callback callback) {
289+
getOriginManager().setCallback(callback);
272290
this.originManagerExplicitlyEnabled = true;
273291
triggerOriginManager();
274292
}
@@ -320,6 +338,16 @@ protected void resetSubscribeHttpManager() {
320338
triggerOriginManager();
321339
}
322340

341+
/**
342+
* Force disabled state if Origin Manager is running.
343+
*
344+
* @return cache busting state
345+
*/
346+
@Override
347+
public boolean getCacheBusting() {
348+
return super.getCacheBusting() && !isOriginManagerRunning();
349+
}
350+
323351
public void setLogging(boolean state) {
324352
Logger.setLOGGING(state);
325353
}

‎java/srcPubnubApi/com/pubnub/api/OriginsPool.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.Set;
77

88
public class OriginsPool {
9-
private LinkedHashSet<String> originsList;
9+
private LinkedHashSet<String> originsList = new LinkedHashSet<String>();
1010

1111
public synchronized void set(Set<String> originsList) throws PubnubException {
1212
if (originsList.size() < 2) {

‎java/srcPubnubApi/com/pubnub/api/PubnubCore.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class PubnubCore {
2222
private String HOSTNAME = "pubsub";
2323
private int HOSTNAME_SUFFIX = 1;
2424
private String DOMAIN = "pubnub.com";
25-
private String ORIGIN_STR = null;
25+
protected String ORIGIN_STR = null;
2626
protected String PUBLISH_KEY = "";
2727
protected String SUBSCRIBE_KEY = "";
2828
protected String SECRET_KEY = "";
@@ -291,7 +291,7 @@ protected String getPubnubUrl() {
291291
if (ORIGIN_STR == null) {
292292
ORIGIN_STR = http();
293293
ORIGIN_STR += getOrigin();
294-
ORIGIN_STR += ((!this.CACHE_BUSTING)?"":"-" + String.valueOf(HOSTNAME_SUFFIX));
294+
ORIGIN_STR += this.getCacheBusting() ? ("-" + String.valueOf(HOSTNAME_SUFFIX)) : "";
295295
ORIGIN_STR += "." + DOMAIN;
296296
}
297297

‎java/srcPubnubApi/com/pubnub/api/PubnubError.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ public class PubnubError {
217217
* Crypto Error
218218
*/
219219
public static final int PNERR_CRYPTO_ERROR = 135;
220+
221+
/**
222+
* OriginsPool should contain at least 2 origins
223+
*/
224+
public static final int PNERR_OM_NOT_ENOUGH_ORIGINS = 137;
220225
// Error Objects
221226

222227
static final PubnubError PNERROBJ_TIMEOUT =
@@ -455,6 +460,12 @@ public class PubnubError {
455460
"Please contact support with error details."
456461
);
457462

463+
static final PubnubError PNERROBJ_OM_NOT_ENOUGH_ORIGINS =
464+
new PubnubError (
465+
PNERR_OM_NOT_ENOUGH_ORIGINS,
466+
"Origins Pool should contain at least 2 origins"
467+
);
468+
458469
public final int errorCode;
459470
public final int errorCodeExtended;
460471
public final JSONObject errorObject;

0 commit comments

Comments
 (0)