-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmppManager.java
More file actions
375 lines (285 loc) · 8.66 KB
/
Copy pathXmppManager.java
File metadata and controls
375 lines (285 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import java.util.Date;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.ping.PingFailedListener;
import org.jivesoftware.smack.ping.packet.Ping;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.ping.PingManager;
/**
* This class will be used as manager to interact with any Xmpp server
* Some methods can be invoked on this instance
*
*/
public class XmppManager implements ConnectionListener, PacketListener, PingFailedListener, MessageListener {
private static ConnectionConfiguration config;
private static XMPPConnection connection;
private static XmppManager xmppManager;
private static String username;
private static String password;
private static ChatManager chatManager;
private String host;
private int port;
private String service;
/*
* Initialize the required static classes
*/
static {
try {
Class.forName("org.jivesoftware.smack.ReconnectionManager");
Class.forName(ServiceDiscoveryManager.class.getName());
} catch (Exception ex) {
ex.printStackTrace();
}
}
/*
* Singleton
*/
public static XmppManager getInstance() {
if (null == xmppManager) {
xmppManager = new XmppManager();
}
return xmppManager;
}
/*
* This will do all the initialization
*/
private void initalize() {
try {
host = getHost();
port = getPort();
service = getService();
System.out.println("Connecting to server host[" + host + "] post[" + port + "]");
if (null == config) {
config = new ConnectionConfiguration(host, port, service, ProxyInfo.forDefaultProxy());
config.setReconnectionAllowed(true);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(SecurityMode.disabled);
}
if (null == connection)
connection = new XMPPConnection(config);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
if (!connection.isConnected())
connection.connect();
Thread.currentThread().sleep(1000 * 2);
System.out.println("Connected with server [" + host + "]: " + connection.isConnected());
connection.addConnectionListener(this);
chatManager = connection.getChatManager();
if (!connection.isAuthenticated()) {
System.out.println("Connected but Not authenticated, so try to login with ID:" + username);
connection.login(username, password);
Thread.currentThread().sleep(1000 * 2);
System.out.println("Success logged in with ID [" + username + "] ");
}
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
System.out.println("Presence sent.");
connection.addPacketListener(this, new PacketTypeFilter(Packet.class));
System.out.println("All done connected ok...");
try {
//tweak it as per your need
PingManager.getInstanceFor(connection).ping(host, 1000 * 60 * 1);
//Thread.currentThread().sleep(1000 * 60 * 1);
} catch (Exception e) {
e.printStackTrace();
}
} catch (XMPPException xe) {
System.out.println("XMPPException - " + xe.getMessage());
} catch (Exception e) {
System.out.println("Exception - " + e.getMessage());
}
}
/*
* this will register itself to the host
*/
public void selfRegister() throws XMPPException, Exception {
if (null == connection) {
initalize();
}
if (connection != null && connection.isConnected()) {
System.out.println("Connected.");
connection.getAccountManager().createAccount(username, password);
Thread.currentThread().sleep(1000 * 2);
System.out.println("User registered with Id - " + username);
}
}
// initalization
public void init() throws XMPPException {
initalize();
}
// connection
public void closeConnection() throws XMPPException {
if (connection != null && connection.isConnected()) {
connection.disconnect();
System.out.println(String.format("User disconnected."));
}
}
// sendmessage
public void sendMessage(String loginID, String message) throws XMPPException {
if (null == connection || !connection.isConnected() || !connection.isAuthenticated()) {
System.out.println("Server not rechable or not connected, trying to reconnect.");
initalize();
}
if (null != chatManager) {
Chat chat = chatManager.createChat(loginID + "@" + service, this);
chat.sendMessage(message);
System.out.println("Mesage Sent [" + message + "]");
} else {
System.out.println("Cannot send message to user [" + loginID + "]");
}
}
// send broadcast message to all
public void sendBroadcastMessage(String message) throws XMPPException {
if (null != connection && connection.isAuthenticated()) {
Message msg = new Message();
msg.setTo(service + "/announce/all");
msg.setBody(message);
connection.sendPacket(msg);
System.out.println("Broadcast message sent - " + message);
} else {
System.out.println("Cannot broadcast message to all.");
}
}
// here we get the stanzas
@Override
public void processPacket(Packet packet) {
try {
if (null != packet) {
System.out.println("RCVD XML - " + packet.toXML());
// do a ping back to the server
if (packet instanceof Ping) {
Ping ping = new Ping();
ping.setTo(packet.getFrom());
ping.setType(Type.RESULT);
connection.sendPacket(ping);
// send the presence also
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
}
// this is the message that we are waiting for
if (packet instanceof Message) {
Message message = (Message) packet;
if (null != message) {
String body = message.getBody();
String from = message.getFrom();
System.out.println("RCVD MSG - " + body);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// In case ping failed
@Override
public void pingFailed() {
System.out.println("Ping to server failed at time : " + new Date());
}
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
@Override
public void processMessage(Chat chat, Message packet) {
try {
if (null != packet) {
if (packet instanceof Message) {
Message message = (Message) packet;
if (null != message) {
String body = message.getBody();
String from = message.getFrom();
System.out.println("MSG from messagelistener - " + body);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void connectionClosed() {
try {
System.out.println("ConnectionClosed.");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void connectionClosedOnError(Exception arg0) {
try {
System.out.println("ConnectionClosedOnError - " + arg0.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void reconnectingIn(int arg0) {
try {
System.out.println("ReconnectingIn in [" + arg0 + "] secs");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void reconnectionFailed(Exception arg0) {
try {
System.out.println("ReconnectionFailed - " + arg0.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void reconnectionSuccessful() {
try {
System.out.println("ReconnectionSuccessful.");
// reconnected, so authenticate it again
if (!connection.isAuthenticated())
connection.login(username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public void setService(String service) {
this.service = service;
}
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
XmppManager.username = username;
}
public static String getPassword() {
return password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getService() {
return service;
}
public static void setPassword(String password) {
XmppManager.password = password;
}
}