Skip to content

Commit 28cba81

Browse files
author
Devendra
committed
changing token request to async pattern
1 parent 82bd743 commit 28cba81

2 files changed

Lines changed: 114 additions & 39 deletions

File tree

java/examples/src/com/pubnub/examples/PubnubTokenRequest.java

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,32 @@ class TokenRequestProcessor {
4949
"In high-maturity sectors, always insure revenue-neutral currencies.",
5050
"Pro rata debts: in the commodities marketplace, be sure not to securitize them." };
5151

52-
void provisionReadOnlyPAMPermissions(String channel, String authKey) {
53-
pubnub.pamGrant(channel, authKey, true, false, new PrintCallback());
54-
pubnub.pamGrant(channel + "-pnpres", authKey, true, true, new PrintCallback());
52+
void provisionReadOnlyPAMPermissions(String channel, final String authKey, final Callback callback) {
53+
pubnub.pamGrant(channel, authKey, true, false, new Callback(){
54+
@Override
55+
public void errorCallback(String channel, PubnubError error) {
56+
System.out.println(error);
57+
}
58+
@Override
59+
public void successCallback(String channel, Object response) {
60+
pubnub.pamGrant(channel + "-pnpres", authKey, true, true, callback);
61+
}
62+
});
63+
5564
}
5665

57-
void provisionReadWritePAMPermissions(String channel, String authKey) {
58-
pubnub.pamGrant(channel, authKey, true, true, new PrintCallback());
59-
pubnub.pamGrant(channel + "-pnpres", authKey, true, true, new PrintCallback());
66+
void provisionReadWritePAMPermissions(String channel, final String authKey, final Callback callback) {
67+
pubnub.pamGrant(channel, authKey, true, true, new Callback(){
68+
@Override
69+
public void errorCallback(String channel, PubnubError error) {
70+
System.out.println(error);
71+
}
72+
@Override
73+
public void successCallback(String channel, Object response) {
74+
System.out.println(response);
75+
pubnub.pamGrant(channel + "-pnpres", authKey, true, true, callback);
76+
}
77+
});
6078
}
6179

6280
private void notifyUser(Object message) {
@@ -87,34 +105,79 @@ private void displayMenuOptions() {
87105
System.out.println("publish a (r)andom message on public channel");
88106
}
89107

90-
public void start() {
91-
108+
public void setupPermissions() {
92109
pubnub = new Pubnub("pam", "pam", "pam", true);
93110
pubnub.setAuthKey(serverAuthToken);
94111

95112
// Provision PAM Permissions on Startup
96113
// First, for devices
114+
System.out.println("Setting up channel permissions .... ");
115+
provisionReadOnlyPAMPermissions(androidChannel, androidAuthToken, new Callback(){
116+
@Override
117+
public void errorCallback(String channel, PubnubError error) {
118+
System.out.println(error);
119+
}
120+
@Override
121+
public void successCallback(String channel, Object response) {
122+
System.out.println(response);
123+
provisionReadOnlyPAMPermissions(iosChannel, iosAuthToken, new Callback(){
124+
@Override
125+
public void errorCallback(String channel, PubnubError error) {
126+
System.out.println(error);
127+
}
128+
@Override
129+
public void successCallback(String channel, Object response) {
130+
System.out.println(response);
131+
provisionReadWritePAMPermissions(androidChannel, serverAuthToken, new Callback() {
132+
@Override
133+
public void errorCallback(String channel, PubnubError error) {
134+
System.out.println(error);
135+
}
136+
@Override
137+
public void successCallback(String channel, Object response) {
138+
System.out.println(response);
139+
provisionReadWritePAMPermissions(iosChannel, serverAuthToken, new Callback(){
140+
@Override
141+
public void errorCallback(String channel, PubnubError error) {
142+
System.out.println(error);
143+
}
144+
@Override
145+
public void successCallback(String channel, Object response) {
146+
System.out.println(response);
147+
try {
148+
pubnub.presence(new String[] { publicChannel, iosChannel,
149+
androidChannel }, new Callback() {
150+
@Override
151+
public void errorCallback(String channel, PubnubError error) {
152+
System.out.println(error);
153+
}
154+
public void successCallback(String channel, Object response) {
155+
}
156+
});
157+
} catch (PubnubException e) {
158+
e.printStackTrace();
159+
}
160+
System.out.println("Done");
161+
new Thread(new Runnable(){
162+
public void run() {
163+
start();
164+
}
165+
}).start();
166+
}
167+
});
168+
169+
}
170+
171+
});
172+
}
173+
});
174+
}
175+
});
176+
177+
}
178+
179+
public void start() {
97180

98-
provisionReadOnlyPAMPermissions(androidChannel, androidAuthToken);
99-
provisionReadOnlyPAMPermissions(iosChannel, iosAuthToken);
100-
101-
// Then for the server
102-
103-
provisionReadWritePAMPermissions(androidChannel, serverAuthToken);
104-
provisionReadWritePAMPermissions(iosChannel, serverAuthToken);
105-
106-
try {
107-
pubnub.presence(new String[] { publicChannel, iosChannel,
108-
androidChannel }, new PrintCallback());
109-
} catch (PubnubException e) {
110-
e.printStackTrace();
111-
}
112-
try {
113-
Thread.sleep(10000);
114-
} catch (InterruptedException e1) {
115-
// TODO Auto-generated catch block
116-
e1.printStackTrace();
117-
}
118181
String command = "";
119182
displayMenuOptions();
120183
while ((command = reader.next()) != "q") {
@@ -177,8 +240,17 @@ public void start() {
177240
//e.printStackTrace();
178241
}
179242
}
180-
181-
pubnub.publish(channel, message, new PrintCallback());
243+
244+
pubnub.publish(channel, message, new Callback(){
245+
public void successCallback(String channel, Object response) {
246+
System.out.println(response);
247+
displayMenuOptions();
248+
}
249+
public void errorCallback(String channel, PubnubError error) {
250+
System.out.println(error);
251+
displayMenuOptions();
252+
}
253+
});
182254
}
183255
if (command.equalsIgnoreCase("r")) {
184256
JSONObject message = new JSONObject();
@@ -192,15 +264,18 @@ public void start() {
192264
} catch (JSONException e) {
193265
e.printStackTrace();
194266
}
195-
pubnub.publish(publicChannel, message, new PrintCallback());
196-
}
197-
try {
198-
Thread.sleep(3000);
199-
} catch (InterruptedException e) {
200-
// TODO Auto-generated catch block
201-
e.printStackTrace();
267+
pubnub.publish(publicChannel, message, new Callback(){
268+
public void successCallback(String channel, Object response) {
269+
System.out.println(response);
270+
displayMenuOptions();
271+
}
272+
public void errorCallback(String channel, PubnubError error) {
273+
System.out.println(error);
274+
displayMenuOptions();
275+
}
276+
});
202277
}
203-
displayMenuOptions();
278+
204279
}
205280
}
206281
}
@@ -211,7 +286,7 @@ public class PubnubTokenRequest {
211286
* @param args
212287
*/
213288
public static void main(String[] args) {
214-
new TokenRequestProcessor().start();
289+
new TokenRequestProcessor().setupPermissions();
215290
}
216291

217292
}

java/jars/PubnubTokenRequest.jar

7.12 KB
Binary file not shown.

0 commit comments

Comments
 (0)