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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.jabber;
import java.util.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.keepalive.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.version.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.Message;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.event.MessageListener;
import net.java.sip.communicator.service.protocol.jabberconstants.*;
import net.java.sip.communicator.util.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smack.util.*;
import org.jivesoftware.smackx.*;
/**
* A straightforward implementation of the basic instant messaging operation
* set.
*
* @author Damian Minkov
*/
public class OperationSetBasicInstantMessagingJabberImpl
implements OperationSetBasicInstantMessaging
{
private static final Logger logger =
Logger.getLogger(OperationSetBasicInstantMessagingJabberImpl.class);
/**
* KeepAlive interval for sending packets
*/
private static final long KEEPALIVE_INTERVAL = 180000l; // 3 minutes
/**
* The interval after which a packet is considered to be lost
*/
private static final long KEEPALIVE_WAIT = 20000l;
private boolean keepAliveEnabled = false;
/**
* The task sending packets
*/
private KeepAliveSendTask keepAliveSendTask = null;
/**
* The timer executing tasks on specified intervals
*/
private Timer keepAliveTimer = new Timer();
/**
* The queue holding the received packets
*/
private LinkedList receivedKeepAlivePackets = new LinkedList();
private int failedKeepalivePackets = 0;
/**
* A list of listeneres registered for message events.
*/
private Vector messageListeners = new Vector();
/**
* The provider that created us.
*/
private ProtocolProviderServiceJabberImpl jabberProvider = null;
/**
* A reference to the persistent presence operation set that we use
* to match incoming messages to <tt>Contact</tt>s and vice versa.
*/
private OperationSetPersistentPresenceJabberImpl opSetPersPresence = null;
/**
* Creates an instance of this operation set.
* @param provider a ref to the <tt>ProtocolProviderServiceImpl</tt>
* that created us and that we'll use for retrieving the underlying aim
* connection.
*/
OperationSetBasicInstantMessagingJabberImpl(
ProtocolProviderServiceJabberImpl provider)
{
this.jabberProvider = provider;
provider.addRegistrationStateChangeListener(new RegistrationStateListener());
// register the KeepAlive Extension in the smack library
ProviderManager.getInstance().addIQProvider(KeepAliveEventProvider.ELEMENT_NAME,
KeepAliveEventProvider.NAMESPACE,
new KeepAliveEventProvider());
}
/**
* Registeres a MessageListener with this operation set so that it gets
* notifications of successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to register.
*/
public void addMessageListener(MessageListener listener)
{
synchronized(messageListeners)
{
if(!messageListeners.contains(listener))
{
this.messageListeners.add(listener);
}
}
}
/**
* Unregisteres <tt>listener</tt> so that it won't receive any further
* notifications upon successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to unregister.
*/
public void removeMessageListener(MessageListener listener)
{
synchronized(messageListeners)
{
this.messageListeners.remove(listener);
}
}
/**
* Create a Message instance for sending arbitrary MIME-encoding content.
*
* @param content content value
* @param contentType the MIME-type for <tt>content</tt>
* @param contentEncoding encoding used for <tt>content</tt>
* @param subject a <tt>String</tt> subject or <tt>null</tt> for now subject.
* @return the newly created message.
*/
public Message createMessage(byte[] content, String contentType,
String contentEncoding, String subject)
{
return new MessageJabberImpl(new String(content), contentType
, contentEncoding, subject);
}
/**
* Create a Message instance for sending a simple text messages with
* default (text/plain) content type and encoding.
*
* @param messageText the string content of the message.
* @return Message the newly created message
*/
public Message createMessage(String messageText)
{
return new MessageJabberImpl(messageText, DEFAULT_MIME_TYPE
, DEFAULT_MIME_ENCODING, null);
}
/**
* Determines wheter the protocol provider (or the protocol itself) support
* sending and receiving offline messages. Most often this method would
* return true for protocols that support offline messages and false for
* those that don't. It is however possible for a protocol to support these
* messages and yet have a particular account that does not (i.e. feature
* not enabled on the protocol server). In cases like this it is possible
* for this method to return true even when offline messaging is not
* supported, and then have the sendMessage method throw an
* OperationFailedException with code - OFFLINE_MESSAGES_NOT_SUPPORTED.
*
* @return <tt>true</tt> if the protocol supports offline messages and
* <tt>false</tt> otherwise.
*/
public boolean isOfflineMessagingSupported()
{
return true;
}
/**
* Determines wheter the protocol supports the supplied content type
*
* @param contentType the type we want to check
* @return <tt>true</tt> if the protocol supports it and
* <tt>false</tt> otherwise.
*/
public boolean isContentTypeSupported(String contentType)
{
if(contentType.equals(DEFAULT_MIME_TYPE))
return true;
else
return false;
}
/**
* Sends the <tt>message</tt> to the destination indicated by the
* <tt>to</tt> contact.
*
* @param to the <tt>Contact</tt> to send <tt>message</tt> to
* @param message the <tt>Message</tt> to send.
* @throws java.lang.IllegalStateException if the underlying stack is
* not registered and initialized.
* @throws java.lang.IllegalArgumentException if <tt>to</tt> is not an
* instance of ContactImpl.
*/
public void sendInstantMessage(Contact to, Message message)
throws IllegalStateException, IllegalArgumentException
{
if( !(to instanceof ContactJabberImpl) )
throw new IllegalArgumentException(
"The specified contact is not a Jabber contact."
+ to);
try
{
assertConnected();
org.jivesoftware.smack.MessageListener msgListener =
new org.jivesoftware.smack.MessageListener() {
public void processMessage(Chat arg0, org.jivesoftware.smack.packet.Message arg1) {
}
};
Chat chat = jabberProvider.getConnection().getChatManager().createChat(
to.getAddress(), msgListener
);
org.jivesoftware.smack.packet.Message msg =
new org.jivesoftware.smack.packet.Message();
msg.setBody(message.getContent());
msg.addExtension(new Version());
MessageEventManager.
addNotificationsRequests(msg, true, false, false, true);
chat.sendMessage(msg);
MessageDeliveredEvent msgDeliveredEvt
= new MessageDeliveredEvent(
message, to, new Date());
fireMessageEvent(msgDeliveredEvt);
}
catch (XMPPException ex)
{
logger.error("message not send", ex);
}
}
/**
* Utility method throwing an exception if the stack is not properly
* initialized.
* @throws java.lang.IllegalStateException if the underlying stack is
* not registered and initialized.
*/
private void assertConnected() throws IllegalStateException
{
if (jabberProvider == null)
throw new IllegalStateException(
"The provider must be non-null and signed on the "
+"service before being able to communicate.");
if (!jabberProvider.isRegistered())
throw new IllegalStateException(
"The provider must be signed on the service before "
+"being able to communicate.");
}
/**
* Our listener that will tell us when we're registered to
*/
private class RegistrationStateListener
implements RegistrationStateChangeListener
{
/**
* The method is called by a ProtocolProvider implementation whenver
* a change in the registration state of the corresponding provider had
* occurred.
* @param evt ProviderStatusChangeEvent the event describing the status
* change.
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
logger.debug("The provider changed state from: "
+ evt.getOldState()
+ " to: " + evt.getNewState());
if (evt.getNewState() == RegistrationState.REGISTERED)
{
opSetPersPresence = (OperationSetPersistentPresenceJabberImpl)
jabberProvider.getSupportedOperationSets()
.get(OperationSetPersistentPresence.class.getName());
jabberProvider.getConnection().addPacketListener(
new SmackMessageListener(),
new AndFilter(
new PacketFilter[]{new GroupMessagePacketFilter(),
new PacketTypeFilter(
org.jivesoftware.smack.packet.Message.class)}));
// run keepalive thread
if(keepAliveSendTask == null && keepAliveEnabled)
{
jabberProvider.getConnection().addPacketListener(
new KeepalivePacketListener(),
new PacketTypeFilter(
KeepAliveEvent.class));
keepAliveSendTask = new KeepAliveSendTask();
keepAliveTimer.scheduleAtFixedRate(
keepAliveSendTask, KEEPALIVE_INTERVAL, KEEPALIVE_INTERVAL);
}
}
}
}
/**
* Delivers the specified event to all registered message listeners.
* @param evt the <tt>EventObject</tt> that we'd like delivered to all
* registered message listerners.
*/
private void fireMessageEvent(EventObject evt)
{
Iterator listeners = null;
synchronized (messageListeners)
{
listeners = new ArrayList(messageListeners).iterator();
}
while (listeners.hasNext())
{
MessageListener listener
= (MessageListener) listeners.next();
if (evt instanceof MessageDeliveredEvent)
{
listener.messageDelivered( (MessageDeliveredEvent) evt);
}
else if (evt instanceof MessageReceivedEvent)
{
listener.messageReceived( (MessageReceivedEvent) evt);
}
else if (evt instanceof MessageDeliveryFailedEvent)
{
listener.messageDeliveryFailed(
(MessageDeliveryFailedEvent) evt);
}
}
}
private class SmackMessageListener
implements PacketListener
{
public void processPacket(Packet packet)
{
if(!(packet instanceof org.jivesoftware.smack.packet.Message))
return;
org.jivesoftware.smack.packet.Message msg =
(org.jivesoftware.smack.packet.Message)packet;
if(msg.getBody() == null)
return;
String fromUserID = StringUtils.parseBareAddress(msg.getFrom());
if(logger.isDebugEnabled())
{
logger.debug("Received from "
+ fromUserID
+ " the message "
+ msg.toXML());
}
Message newMessage = createMessage(msg.getBody());
Contact sourceContact =
opSetPersPresence.findContactByID(fromUserID);
if(msg.getType() == org.jivesoftware.smack.packet.Message.Type.error)
{
logger.info("Message error received from " + fromUserID);
int errorCode = packet.getError().getCode();
int errorResultCode = MessageDeliveryFailedEvent.UNKNOWN_ERROR;
if(errorCode == 503)
{
org.jivesoftware.smackx.packet.MessageEvent msgEvent =
(org.jivesoftware.smackx.packet.MessageEvent)
packet.getExtension("x", "jabber:x:event");
if(msgEvent != null && msgEvent.isOffline())
{
errorResultCode =
MessageDeliveryFailedEvent.OFFLINE_MESSAGES_NOT_SUPPORTED;
}
}
MessageDeliveryFailedEvent ev =
new MessageDeliveryFailedEvent(newMessage,
sourceContact,
errorResultCode,
new Date());
fireMessageEvent(ev);
return;
}
// In the second condition we filter all group chat messages,
// because they are managed by the multi user chat operation set.
if(sourceContact == null)
{
logger.debug("received a message from an unknown contact: "
+ fromUserID);
//create the volatile contact
sourceContact = opSetPersPresence
.createVolatileContact(fromUserID);
}
MessageReceivedEvent msgReceivedEvt
= new MessageReceivedEvent(
newMessage, sourceContact , new Date() );
fireMessageEvent(msgReceivedEvt);
}
}
/**
* Receives incoming KeepAlive Packets
*/
private class KeepalivePacketListener
implements PacketListener
{
public void processPacket(Packet packet)
{
if(packet != null && !(packet instanceof KeepAliveEvent))
return;
KeepAliveEvent keepAliveEvent = (KeepAliveEvent)packet;
if(logger.isDebugEnabled())
{
logger.debug("Received keepAliveEvent from "
+ keepAliveEvent.getFromUserID()
+ " the message : "
+ keepAliveEvent.toXML());
}
receivedKeepAlivePackets.addLast(keepAliveEvent);
}
}
/**
* Task sending packets on intervals.
* The task is runned on specified intervals by the keepAliveTimer
*/
private class KeepAliveSendTask
extends TimerTask
{
public void run()
{
// if we are not registerd do nothing
if(!jabberProvider.isRegistered())
{
logger.trace("provider not registered. "
+"won't send keep alive. acc.id="
+ jabberProvider.getAccountID()
.getAccountUniqueID());
return;
}
KeepAliveEvent keepAliveEvent =
new KeepAliveEvent(jabberProvider.getConnection().getUser());
keepAliveEvent.setSrcOpSetHash(
OperationSetBasicInstantMessagingJabberImpl.this.hashCode());
keepAliveEvent.setSrcProviderHash(jabberProvider.hashCode());
// schedule the check task
keepAliveTimer.schedule(
new KeepAliveCheckTask(), KEEPALIVE_WAIT);
logger.trace(
"send keepalive for acc: "
+ jabberProvider.getAccountID().getAccountUniqueID());
jabberProvider.getConnection().sendPacket(keepAliveEvent);
}
}
/**
* Check if the first received packet in the queue
* is ok and if its not or the queue has no received packets
* the this means there is some network problem, so fire event
*/
private class KeepAliveCheckTask
extends TimerTask
{
public void run()
{
try
{
// check till we find a correct message
// or if NoSuchElementException is thrown
// there is no message
while(!checkFirstPacket());
failedKeepalivePackets = 0;
}
catch (NoSuchElementException ex)
{
logger.error(
"Did not receive last keep alive packet for account "
+ jabberProvider.getAccountID().getAccountUniqueID());
failedKeepalivePackets++;
// if we have 3 keepalive fails then unregister
if(failedKeepalivePackets == 3)
{
logger.error("unregistering.");
// fireUnregisterd();
jabberProvider.reregister();
failedKeepalivePackets = 0;
}
}
}
/**
* Checks whether first packet in queue is ok
* @return boolean
* @throws NoSuchElementException
*/
boolean checkFirstPacket()
throws NoSuchElementException
{
KeepAliveEvent receivedEvent =
(KeepAliveEvent)receivedKeepAlivePackets.removeLast();
if(jabberProvider.hashCode() != receivedEvent.getSrcProviderHash() ||
OperationSetBasicInstantMessagingJabberImpl.this.hashCode() !=
receivedEvent.getSrcOpSetHash() ||
!jabberProvider.getAccountID().getUserID().
equals(receivedEvent.getFromUserID()) )
return false;
else
return true;
}
/**
* Fire Unregistered event
*/
void fireUnregisterd()
{
jabberProvider.fireRegistrationStateChanged(
jabberProvider.getRegistrationState(),
RegistrationState.CONNECTION_FAILED,
RegistrationStateChangeEvent.REASON_INTERNAL_ERROR, null);
opSetPersPresence.fireProviderPresenceStatusChangeEvent(
opSetPersPresence.getPresenceStatus(),
JabberStatusEnum.OFFLINE);
}
}
/**
* Set is keep alive sendin packets to be enabled
* @param keepAliveEnabled boolean
*/
public void setKeepAliveEnabled(boolean keepAliveEnabled)
{
this.keepAliveEnabled = keepAliveEnabled;
}
private class GroupMessagePacketFilter implements PacketFilter
{
public boolean accept(Packet packet)
{
if(!(packet instanceof org.jivesoftware.smack.packet.Message))
return false;
org.jivesoftware.smack.packet.Message msg
= (org.jivesoftware.smack.packet.Message) packet;
if(msg.getType().equals(
org.jivesoftware.smack.packet.Message.Type.groupchat))
return false;
return true;
}
}
}
|