/* * 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.service.protocol; import java.util.*; import net.java.sip.communicator.service.protocol.event.*; /** * An abstract class with a default implementation of some of the methods of * the ChatRoom interface. * * @author Boris Grozev */ public abstract class AbstractChatRoom implements ChatRoom { /** * The list of listeners to be notified when a member of the chat room * publishes a ConferenceDescription */ protected final List conferencePublishedListeners = new LinkedList(); /** * The list of all ConferenceDescription that were announced and * are not yet processed. */ protected Map cachedConferenceDescriptions = new HashMap(); /** * {@inheritDoc} */ public void addConferencePublishedListener( ChatRoomConferencePublishedListener listener) { synchronized (conferencePublishedListeners) { conferencePublishedListeners.add(listener); } } /** * {@inheritDoc} */ public void removeConferencePublishedListener( ChatRoomConferencePublishedListener listener) { synchronized (conferencePublishedListeners) { conferencePublishedListeners.remove(listener); } } /** * Returns cached ConferenceDescription instances. * @return the cached ConferenceDescription instances. */ public Map getCachedConferenceDescriptions() { Map tmpCachedConferenceDescriptions; synchronized (cachedConferenceDescriptions) { tmpCachedConferenceDescriptions = new HashMap( cachedConferenceDescriptions); } return tmpCachedConferenceDescriptions; } /** * Returns the number of cached ConferenceDescription instances. * @return the number of cached ConferenceDescription instances. */ public synchronized int getCachedConferenceDescriptionSize() { return cachedConferenceDescriptions.size(); } /** * Creates the corresponding ChatRoomConferencePublishedEvent and * notifies all ChatRoomConferencePublishedListeners that * member has published a conference description. * * @param member the ChatRoomMember that published cd. * @param cd the ConferenceDescription that was published. * @param eventType the type of the event. */ protected void fireConferencePublishedEvent( ChatRoomMember member, ConferenceDescription cd, int eventType) { ChatRoomConferencePublishedEvent evt = new ChatRoomConferencePublishedEvent(eventType, this, member, cd); List listeners; synchronized (conferencePublishedListeners) { listeners = new LinkedList( conferencePublishedListeners); } for (ChatRoomConferencePublishedListener listener : listeners) listener.conferencePublished(evt); } /** * Processes the ConferenceDescription instance and adds/removes * it to the list of conferences. * * @param cd the ConferenceDescription instance to process. * @param participantName the name of the participant that sent the * ConferenceDescription. * @return true on success and false if fail. */ protected boolean processConferenceDescription(ConferenceDescription cd, String participantName) { if(cd.isAvailable()) { if(cachedConferenceDescriptions.containsKey(participantName)) return false; cachedConferenceDescriptions.put(participantName, cd); } else { ConferenceDescription cachedDescription = cachedConferenceDescriptions.get(participantName); if(cachedDescription == null || !cd.compareConferenceDescription(cachedDescription)) return false; cachedConferenceDescriptions.remove(participantName); } return true; } /** * Clears the list with the chat room conferences. */ protected void clearCachedConferenceDescriptionList() { cachedConferenceDescriptions.clear(); } }