aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/jabber/OperationSetGeolocationJabberImpl.java
blob: f504f6b798dbb77964a778167f21e0b097ed688a (plain)
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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.jabber;

import java.util.*;

import net.java.sip.communicator.impl.protocol.jabber.extensions.geolocation.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
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.StringUtils;

/**
 * The Jabber implementation of an OperationSetGeolocation done with the
 * XEP-0080: User Geolocation. This class broadcast our own geolocation and
 * manage the geolocation status of our buddies.
 *
 * Currently, we send geolocation message in presence. We passively listen
 * to buddies geolocation when their presence are updated.
 *
 * @author Guillaume Schreiner
 */
public class OperationSetGeolocationJabberImpl
    implements OperationSetGeolocation
{
    /**
     * Our logger.
     */
    private static final Logger logger =
        Logger.getLogger(OperationSetGeolocationJabberImpl.class);

    /**
     * The list of Geolocation status listeners interested in receiving presence
     * notifications of changes in geolocation of contacts in our contact list.
     */
    private final List<GeolocationListener> geolocationContactsListeners
        = new Vector<GeolocationListener>();

    /**
     * A callback to the provider
     */
    private final ProtocolProviderServiceJabberImpl jabberProvider;

    /**
     * A callback to the persistent presence operation set.
     */
    private final OperationSetPersistentPresence opsetprez;

    /**
     * Constuctor
     *
     * @param provider <tt>ProtocolProviderServiceJabberImpl</tt>
     */
    public OperationSetGeolocationJabberImpl(
        ProtocolProviderServiceJabberImpl provider)
    {
        this.jabberProvider = provider;

        this.opsetprez
                = provider
                    .getOperationSet(OperationSetPersistentPresence.class);

        this.jabberProvider.addRegistrationStateChangeListener(
            new RegistrationStateListener());

        // Add the custom GeolocationExtension to the Smack library
        ProviderManager pManager = ProviderManager.getInstance();
        pManager.addExtensionProvider(
            GeolocationPacketExtensionProvider.ELEMENT_NAME
            , GeolocationPacketExtensionProvider.NAMESPACE
            , new GeolocationPacketExtensionProvider());
    }

    /**
     * Broadcast our current Geolocation trough this provider using a Jabber
     * presence message.
     *
     * @param geolocation our current Geolocation ready to be sent
     */
    public void publishGeolocation(Map<String, String> geolocation)
    {
        GeolocationPresence myGeolocPrez = new GeolocationPresence(opsetprez);

        GeolocationPacketExtension geolocExt = GeolocationJabberUtils
            .convertMapToExtension(geolocation);

        myGeolocPrez.setGeolocationExtention(geolocExt);

        this.jabberProvider.getConnection()
            .sendPacket(myGeolocPrez.getGeolocPresence());
    }

    /**
     * Retrieve the geolocation of the given contact.
     * <p>
     * Note: Currently not implemented because we can not actively poll the
     * server for the presence of a given contact ?
     * <p>
     * @param contactIdentifier the <tt>Contact</tt> we want to retrieve its
     * geolocation by its identifier.
     * @return the <tt>Geolocation</tt> of the contact.
     */
    public Map<String, String> queryContactGeolocation(String contactIdentifier)
    {
        /** @todo implement queryContactGeolocation() */
        return null;
    }

    /**
     * Registers a listener that would get notifications any time a contact
     * refreshed its geolocation via Presence.
     *
     * @param listener the <tt>ContactGeolocationPresenceListener</tt> to
     * register
     */
    public void addGeolocationListener(GeolocationListener listener)
    {
        synchronized (geolocationContactsListeners)
        {
            geolocationContactsListeners.add(listener);
        }
    }

    /**
     * Remove a listener that would get notifications any time a contact
     * refreshed its geolocation via Presence.
     *
     * @param listener the <tt>ContactGeolocationPresenceListener</tt> to
     * register
     */
    public void removeGeolocationListener(GeolocationListener listener)
    {
        synchronized (geolocationContactsListeners)
        {
            geolocationContactsListeners.remove(listener);
        }
    }

    /**
     * Our listener that will tell us when we're registered to server
     * and we are ready to launch the listener for GeolocationPacketExtension
     * packets
     */
    private class RegistrationStateListener
        implements RegistrationStateChangeListener
    {
        /**
         * The method is called by a ProtocolProvider implementation whenever
         * 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)
        {
            if (logger.isDebugEnabled())
                logger.debug("The Jabber provider changed state from: "
                         + evt.getOldState()
                         + " to: " + evt.getNewState());

            if (evt.getNewState() == RegistrationState.REGISTERED)
            {

                PacketExtensionFilter filterGeoloc =
                    new PacketExtensionFilter(
                        GeolocationPacketExtensionProvider.ELEMENT_NAME,
                        GeolocationPacketExtensionProvider.NAMESPACE
                    );

                // launch the listener
                try
                {
                    jabberProvider.getConnection().addPacketListener(
                        new GeolocationPresencePacketListener()
                                , filterGeoloc
                        );
                }
                catch (Exception e)
                {
                    logger.error(e);
                }

            }
            else if (evt.getNewState() == RegistrationState.UNREGISTERED
                     || evt.getNewState()
                                == RegistrationState.AUTHENTICATION_FAILED
                     || evt.getNewState()
                                == RegistrationState.CONNECTION_FAILED)
            {

            }
        }
    }

    /**
     * This class listen to GeolocationExtension into Presence Packet.
     * If GeolocationExtension is found, an event is sent.
     *
     * @author Guillaume Schreiner
     */
    private class GeolocationPresencePacketListener
        implements PacketListener
    {
        /**
         * Match incoming packets with geolocation Extension tags for
         * dispatching a new event.
         *
         * @param packet matching Geolocation Extension tags.
         */
        public void processPacket(Packet packet)
        {
            String from = StringUtils.parseBareAddress(packet.getFrom());

            GeolocationPacketExtension geolocExt =
                (GeolocationPacketExtension) packet.getExtension(
                    GeolocationPacketExtensionProvider.ELEMENT_NAME,
                    GeolocationPacketExtensionProvider.NAMESPACE);

            if (geolocExt != null)
            {
                if (logger.isDebugEnabled())
                    logger.debug("GeolocationExtension found from " + from + ":" +
                             geolocExt.toXML());

                Map<String, String> newGeolocation
                    = GeolocationJabberUtils.convertExtensionToMap(geolocExt);

                this.fireGeolocationContactChangeEvent(
                    from,
                    newGeolocation);
            }
        }

        /**
         * Notify registred listeners for a new incoming GeolocationExtension.
         *
         * @param sourceContact which send a new Geolocation.
         * @param newGeolocation the new given Geolocation.
         */
        public void fireGeolocationContactChangeEvent(
                String sourceContact,
                Map<String, String> newGeolocation)
        {
            if (logger.isDebugEnabled())
                logger.debug("Trying to dispatch geolocation contact update for "
                         + sourceContact);

            Contact source = opsetprez.findContactByID(sourceContact);

            GeolocationEvent evt =
                new GeolocationEvent(
                    source
                    , jabberProvider
                    , newGeolocation
                    , OperationSetGeolocationJabberImpl.this);

            if (logger.isDebugEnabled())
                logger.debug("Dispatching  geolocation contact update. Listeners="
                         + geolocationContactsListeners.size()
                         + " evt=" + evt);

            GeolocationListener[] listeners;

            synchronized (geolocationContactsListeners)
            {
                listeners
                    = geolocationContactsListeners.toArray(
                            new GeolocationListener[
                                    geolocationContactsListeners.size()]);
            }

            for (GeolocationListener listener : listeners)
                listener.contactGeolocationChanged(evt);
        }
    }
}