Simplified synchronization. (Left a few markers to double-check.)

fix-message-formatting
Danny van Heumen 11 years ago
parent 4c8997cae1
commit 1622cf7598

@ -49,17 +49,15 @@ public class IrcStack
*/ */
private final ProtocolProviderServiceIrcImpl provider; private final ProtocolProviderServiceIrcImpl provider;
/**
* Container for channels that are being joined at the moment.
*
* TODO Consider removing field 'joining' and use field 'joined' with a null
* ChatRoomIrcImpl instance.
*/
private final Map<String, Result<Object, Exception>> joining = Collections
.synchronizedMap(new HashMap<String, Result<Object, Exception>>());
/** /**
* Container for joined channels. * Container for joined channels.
*
* There are two different cases:
*
* <pre>
* - null value: joining is initiated but still in progress.
* - non-null value: joining is finished, chat room instance is available.
* </pre>
*/ */
private final Map<String, ChatRoomIrcImpl> joined = Collections private final Map<String, ChatRoomIrcImpl> joined = Collections
.synchronizedMap(new HashMap<String, ChatRoomIrcImpl>()); .synchronizedMap(new HashMap<String, ChatRoomIrcImpl>());
@ -148,7 +146,6 @@ public void connect(String host, int port, String password,
return; return;
// Make sure we start with an empty joined-channel list. // Make sure we start with an empty joined-channel list.
this.joining.clear();
this.joined.clear(); this.joined.clear();
final IRCServer server; final IRCServer server;
@ -297,13 +294,16 @@ public void disconnect()
if (this.connectionState == null && this.irc == null) if (this.connectionState == null && this.irc == null)
return; return;
// TODO Handle aborting joining attempts registered in this.joining.
synchronized (this.joined) synchronized (this.joined)
{ {
// Leave all joined channels. // Leave all joined channels.
for (ChatRoomIrcImpl channel : this.joined.values()) for (ChatRoomIrcImpl channel : this.joined.values())
{ {
if (channel == null)
{
// TODO how to cancel running joining attempt
continue;
}
leave(channel); leave(channel);
} }
} }
@ -405,7 +405,8 @@ public void setSubject(ChatRoomIrcImpl chatroom, String subject)
*/ */
public boolean isJoined(ChatRoomIrcImpl chatroom) public boolean isJoined(ChatRoomIrcImpl chatroom)
{ {
return this.joined.containsKey(chatroom.getIdentifier()); // TODO Do we really consider a joining attempt as joined?
return this.joined.get(chatroom.getIdentifier()) != null;
} }
/** /**
@ -512,7 +513,7 @@ public void join(final ChatRoomIrcImpl chatroom, final String password)
.trace("Issue join channel command to IRC library and wait for" .trace("Issue join channel command to IRC library and wait for"
+ " join operation to complete (un)successfully."); + " join operation to complete (un)successfully.");
this.joining.put(chatroom.getIdentifier(), joinSignal); this.joined.put(chatroom.getIdentifier(), null);
// TODO Refactor this ridiculous nesting of functions and // TODO Refactor this ridiculous nesting of functions and
// classes. // classes.
this.irc.joinChannel(chatroom.getIdentifier(), password, this.irc.joinChannel(chatroom.getIdentifier(), password,
@ -552,7 +553,9 @@ public void onSuccess(IRCChannel channel)
+ "since that channel was not " + "since that channel was not "
+ "announced."); + "announced.");
} }
IrcStack.this.joining.remove(chatRoomId); // FIXME Can it go wrong if we unconditionally
// remove the chatroomId entry?
IrcStack.this.joined.remove(chatRoomId);
IrcStack.this.provider IrcStack.this.provider
.getMUC() .getMUC()
.fireLocalUserPresenceEvent( .fireLocalUserPresenceEvent(
@ -573,7 +576,6 @@ public void onSuccess(IRCChannel channel)
try try
{ {
IrcStack.this.joined.put(chatRoomId, chatroom); IrcStack.this.joined.put(chatRoomId, chatroom);
IrcStack.this.joining.remove(chatRoomId);
IrcStack.this.irc IrcStack.this.irc
.addListener(new ChatRoomListener( .addListener(new ChatRoomListener(
chatroom)); chatroom));
@ -621,7 +623,7 @@ public void onFailure(Exception e)
{ {
try try
{ {
IrcStack.this.joining.remove(chatRoomId); IrcStack.this.joined.remove(chatRoomId);
IrcStack.this.provider IrcStack.this.provider
.getMUC() .getMUC()
.fireLocalUserPresenceEvent( .fireLocalUserPresenceEvent(
@ -881,12 +883,11 @@ public void onServerNumericMessage(ServerNumericMessage msg)
// Synchronize the section that checks then adds a chat // Synchronize the section that checks then adds a chat
// room. This way we can be sure that there are no 2 // room. This way we can be sure that there are no 2
// simultaneous creation events. // simultaneous creation events.
if (IrcStack.this.joining.containsKey(channelName) if (IrcStack.this.joined.containsKey(channelName))
|| IrcStack.this.joined.containsKey(channelName))
{ {
LOGGER.trace("Chat room '" + channelName LOGGER.trace("Chat room '" + channelName
+ "' join event was announced. Stop " + "' join event was announced or already "
+ "handling this event."); + "finished. Stop handling this event.");
break; break;
} }
// We aren't currently attempting to join, so this join is // We aren't currently attempting to join, so this join is

Loading…
Cancel
Save