swpchat.server
Class ControlProtocolHandler

java.lang.Object
  |
  +--swpchat.server.ProtocolHandler
        |
        +--swpchat.server.ControlProtocolHandler
All Implemented Interfaces:
ChannelCollectionListener, ChatTokens, ControlServer, ControlTokens, ListenerBase, NotificationTokens, SenderReceiverTokens

public class ControlProtocolHandler
extends ProtocolHandler
implements ControlServer, ControlTokens, NotificationTokens, ChannelCollectionListener

class that handles the ControlProtocol with one client

Author:
Andre Brunner, Christian Stuellenberg, Marc Sturm

Field Summary
private  boolean alreadyJoined
           
private  ChannelCollection channelCollection
           
private static java.lang.String CLS
           
private  boolean connected
           
private  ConnectedClientCollection connectedClientCollection
           
 boolean continueRunning
           
private  Channel controlChannel
           
private  InformerThread informer
           
private  java.lang.String newNickName
           
private  NickNameCollection nickCollection
           
private  java.lang.String nickName
           
 java.io.BufferedWriter ostream
           
private  java.util.Random rdm
           
private  ControlProtocolReceiverThread receiver
           
private  java.lang.String sessionKey
           
 java.net.Socket socket
           
private static java.lang.String STR_STANDARD
           
 ChatStreamTokenizer tokenizer
           
 java.util.Vector tokenlist
           
private  UserInfoHandler userInfoHandler
           
 
Fields inherited from interface swpchat.protocol.ControlTokens
STR_EMOTION, STR_LOGIN, STR_LOGIN_BEGIN, STR_LOGIN_RE_BEGIN, STR_LOGOUT, STR_LOGOUT_BEGIN, STR_LOGOUT_RE_BEGIN, STR_MEMBER, STR_MEMBER_BEGIN, STR_MEMBER_INFO, STR_MEMBER_JOIN, STR_MEMBER_LEAVE, STR_MEMBER_SHIP, STR_NEW_CHANNEL, STR_NOTIFICATION, STR_NOTIFICATION_BEGIN, STR_NOTIFY_REQUEST, STR_REMOVE_CHANNEL, STR_RENAME_CHANNEL, STR_REQUEST_IP, STR_REQUST_IP_RE_BEGIN, STR_SEND_USERINFO, STR_SET_NICK, STR_SET_NICK_BEGIN, STR_SET_NICK_RE_BEGIN, STR_SET_SESSIONKEY, STR_STOP_NOTIFICATION, STR_TELEPHONE_REQUEST
 
Fields inherited from interface swpchat.protocol.ChatTokens
CHAR_ARG_SEP, CHAR_MSG_END, CHAR_MSG_EOL, CHAR_QUOTE, CHAR_STRING_DELIMITER, STR_ARG_SEP, STR_AUTHORIZE, STR_MSG_END, STR_MSG_EOL, STR_NEG_ARG, STR_NEGATIVE, STR_POS_ARG, STR_POSITIVE, STR_QUOTE, STR_REPLY, STR_STRING_DELIMITER, STR_WAIT
 
Fields inherited from interface swpchat.protocol.NotificationTokens
STR_STATE_JOINED, STR_STATE_LEFT, STR_STATE_OFFLINE, STR_STATE_ONLINE
 
Fields inherited from interface swpchat.protocol.SenderReceiverTokens
STR_JOIN, STR_JOINPASSWD, STR_LEAVE, STR_OFF, STR_ON, STR_PLAY, STR_PLAY_BEGIN, STR_PLAY_RE_BEGIN, STR_RECEIVE, STR_RECEIVE_BEGIN, STR_RECEIVE_USERINFO, STR_REQUEST_USERINFO, STR_SEND, STR_SEND_BEGIN, STR_SEND_RE_BEGIN, STR_SETTOPIC, STR_SETTOPIC_BEGIN, STR_SHIP, STR_SHIP_BEGIN, STR_TOPICCHANGED, STR_WHISPER, STR_WHISPRECEIVE
 
Constructor Summary
ControlProtocolHandler(ChannelCollection channelCollection, NickNameCollection nickCollection, ConnectedClientCollection connectedClientCollection)
          Constructor
 
Method Summary
 void addChannel(java.lang.String name)
          Implementation of interface ChannelCollectionListener.
 void disconnect()
          finally we have to remove the session from connectedClientCollection and we have to remove our nick and sessionKey from nickNameCollection
 java.lang.String getIdentifier()
          Implementaion of interface ListenerBase.
 void handleNormalRequests(java.util.Vector tokenlist)
          method handles the command that is saved in the tokenlist
 void notify(java.lang.String nick, java.lang.String state, java.lang.String channels)
          Notify ClientConnection that 'nick' assumed 'state' concerning 'channels'.
 void removeChannel(java.lang.String name)
          Implementation of interface ChannelCollectionListener.
 void renameChannel(java.lang.String oldName, java.lang.String newName)
          Implementation of interface ChannelCollectionListener.
 void sendControlMessage(java.lang.String command, java.lang.String message1, java.lang.String message2)
          method interrupts receiverThread and then receives itself all incoming messages until the reply, we have been waiting (after sending request) for, occurs or comes not within 5 seconds.
 void sendRequestMessage(java.lang.String request, java.lang.String info1, java.lang.String info2, java.lang.String info3)
          method interrupts receiverThread and then receives itself all incoming messages until the reply, we have been waiting (after sending request) for, occurs or comes not within 5 seconds.
 void serve(java.net.Socket socket, ClientConnection client, java.io.InputStream inputStream)
          implementation of method serve from ProtocolHandler instantiate new ControlProtocolReceiverThread
 
Methods inherited from class swpchat.server.ProtocolHandler
isEmotion, isJoin, isJoinPasswd, isLeave, isLogin, isLogout, isMemberRequest, isNewChannel, isNewChannelReply, isNotificationRequest, isPlay, isPutShip, isRemoveChannel, isRemoveChannelReply, isRenameChannel, isRenameChannelReply, isRequestIPAddress, isRequestIPAddressReply, isRequestUserInfo, isSend, isSendTelephoneRequest, isSendTelephoneRequestReply, isSendUserInfo, isSetNick, isSetSessionKey, isSetTopic, isStopNotification, isWhisper, readCommand, reply, send, send, send, send, send, writeLine
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

CLS

private static final java.lang.String CLS

STR_STANDARD

private static final java.lang.String STR_STANDARD

channelCollection

private ChannelCollection channelCollection

controlChannel

private Channel controlChannel

nickCollection

private NickNameCollection nickCollection

connectedClientCollection

private ConnectedClientCollection connectedClientCollection

sessionKey

private java.lang.String sessionKey

connected

private boolean connected

rdm

private java.util.Random rdm

tokenizer

public ChatStreamTokenizer tokenizer

ostream

public java.io.BufferedWriter ostream

socket

public java.net.Socket socket

tokenlist

public java.util.Vector tokenlist

continueRunning

public boolean continueRunning

nickName

private java.lang.String nickName

newNickName

private java.lang.String newNickName

receiver

private ControlProtocolReceiverThread receiver

informer

private InformerThread informer

userInfoHandler

private UserInfoHandler userInfoHandler

alreadyJoined

private boolean alreadyJoined
Constructor Detail

ControlProtocolHandler

public ControlProtocolHandler(ChannelCollection channelCollection,
                              NickNameCollection nickCollection,
                              ConnectedClientCollection connectedClientCollection)
Constructor
Parameters:
channelCollection, - Collection of all possible Channels
nickCollection, - Collection of all Nicknames
connectedClientCollection, - Collection of all connected Clients
Method Detail

serve

public void serve(java.net.Socket socket,
                  ClientConnection client,
                  java.io.InputStream inputStream)
           throws TimeoutException,
                  java.io.IOException,
                  java.security.InvalidParameterException
implementation of method serve from ProtocolHandler instantiate new ControlProtocolReceiverThread
Specified by:
serve in interface ControlServer
Overrides:
serve in class ProtocolHandler
Parameters:
{@link - java.net.Socket}, the socket that must be served
{@link - swpchat.server.ClientConnection}, the Thread that runs
{@link - java.io.InputStream}, the Stream that must be read from
Throws:
TimeoutException - thrown if connection to the client is lost or too slow.
java.io.IOException - thrown for any I/O error.
java.security.InvalidParameterException - possibly useful

handleNormalRequests

public void handleNormalRequests(java.util.Vector tokenlist)
                          throws java.io.IOException,
                                 java.net.ProtocolException
method handles the command that is saved in the tokenlist
Specified by:
handleNormalRequests in interface ControlServer
Parameters:
{@link - java.util.Vector} contains the Tokens of the command
Throws:
java.net.ProtocolException - thrown if Protocol is injured.
java.io.IOException - thrown for any I/O error.

sendControlMessage

public void sendControlMessage(java.lang.String command,
                               java.lang.String message1,
                               java.lang.String message2)
method interrupts receiverThread and then receives itself all incoming messages until the reply, we have been waiting (after sending request) for, occurs or comes not within 5 seconds. Finally starts receiverThread again. Threadsave, because many Thread could send messages to the client.
Specified by:
sendControlMessage in interface ControlServer
Parameters:
control - is the {@java.lang.String} that identifys the command
message1 - is a {@java.lang.String} specifying part1 of message
message2 - is a {@java.lang.String} specifying part2 of message

sendRequestMessage

public void sendRequestMessage(java.lang.String request,
                               java.lang.String info1,
                               java.lang.String info2,
                               java.lang.String info3)
method interrupts receiverThread and then receives itself all incoming messages until the reply, we have been waiting (after sending request) for, occurs or comes not within 5 seconds. Finally starts receiverThread again. Threadsave, because many Thread could send messages to the client.
Specified by:
sendRequestMessage in interface ControlServer
Parameters:
request - is the {@java.lang.String} that identifys the request
info1 - is a {@java.lang.String} specifying part1 of message
info2 - is a {@java.lang.String} specifying part2 of message
info3 - is a {@java.lang.String} specifying part3 of message

notify

public void notify(java.lang.String nick,
                   java.lang.String state,
                   java.lang.String channels)
Notify ClientConnection that 'nick' assumed 'state' concerning 'channels'. Example: 'nick' is NotificationTokens.STR_STATE_ONLINE and joined some channels, whereas channels contains a list of the channels simply divided by ';'s.
Specified by:
notify in interface ControlServer
Parameters:
nick - String specifying the nickname of the concerning the message
state - String specifying the state 'nick' assumed. The valid states can be found in NotificationTokens.
channels - String specifying the channels joined or left devided by ';'s. Example: 'nick' joined 'test' and 'swp' up to now. Then channels can be "test;swp".

disconnect

public void disconnect()
finally we have to remove the session from connectedClientCollection and we have to remove our nick and sessionKey from nickNameCollection
Specified by:
disconnect in interface ControlServer
Overrides:
disconnect in class ProtocolHandler

getIdentifier

public java.lang.String getIdentifier()
Implementaion of interface ListenerBase.
Specified by:
getIdentifier in interface ListenerBase
Following copied from interface: swpchat.server.ListenerBase
Returns:
a unique ID. So the sessionKey is a perfect candidate for this.

addChannel

public void addChannel(java.lang.String name)
Implementation of interface ChannelCollectionListener.
Specified by:
addChannel in interface ChannelCollectionListener
Following copied from interface: swpchat.server.ChannelCollectionListener
Parameters:
name - is the name of the new channel

renameChannel

public void renameChannel(java.lang.String oldName,
                          java.lang.String newName)
Implementation of interface ChannelCollectionListener.
Specified by:
renameChannel in interface ChannelCollectionListener
Following copied from interface: swpchat.server.ChannelCollectionListener
Parameters:
oldName - is the old name of the new channel
newName - is the new name of the new channel

removeChannel

public void removeChannel(java.lang.String name)
Implementation of interface ChannelCollectionListener.
Specified by:
removeChannel in interface ChannelCollectionListener
Following copied from interface: swpchat.server.ChannelCollectionListener
Parameters:
name - is the name of the channel that has been removed.