controller.java

来自「google公司的用Java写的一个聊天软件的原代码」· Java 代码 · 共 649 行 · 第 1/2 页

JAVA
649
字号
		messages.sendGetTopicMessage();	}	/**	 * Sends a message over the network to notify other clients that this	 * client is still alive.	 */	public void sendIdleMessage()	{		messages.sendIdleMessage();	}	/**	 * Sends a chat message over the network, to all the other users.	 *	 * @param msg The message to send.	 * @throws CommandException If there is no connection to the network,	 * 		or the application user is away,	 * 		or the message is empty,	 * 		or the message is too long.	 */	public void sendChatMessage( final String msg ) throws CommandException	{		if ( !isConnected() )			throw new CommandException( "You can not send a chat message without being connected." );		else if ( me.isAway() )			throw new CommandException( "You can not send a chat message while away." );		else if ( msg.trim().length() == 0 )			throw new CommandException( "You can not send an empty chat message." );		else if ( Tools.getBytes( msg ) > Constants.MESSAGE_MAX_BYTES )			throw new CommandException( "You can not send a chat message with more than " + Constants.MESSAGE_MAX_BYTES + " bytes." );		else			messages.sendChatMessage( msg );	}	/**	 * Sends a message over the network with the current topic.	 */	public void sendTopicMessage()	{		messages.sendTopicMessage( getTopic() );	}	/**	 * Changes the topic, and sends a notification to the other clients.	 *	 * @param newTopic The new topic to set.	 * @throws CommandException If there is no connection to the network,	 * 		or the application user is away,	 * 		or the topic is too long.	 */	public void changeTopic( final String newTopic ) throws CommandException	{		if ( !isConnected() )			throw new CommandException( "You can not change the topic without being connected." );		else if ( me.isAway() )			throw new CommandException( "You can not change the topic while away." );		else if ( Tools.getBytes( newTopic ) > Constants.MESSAGE_MAX_BYTES )			throw new CommandException( "You can not set a topic with more than " + Constants.MESSAGE_MAX_BYTES + " bytes." );		else		{			long time = System.currentTimeMillis();			TopicDTO topic = getTopic();			topic.changeTopic( newTopic, me.getNick(), time );			sendTopicMessage();		}	}	/**	 * Sends a message over the network with the current away message, to	 * notify the other clients that the application user has gone away.	 */	public void sendAwayMessage()	{		messages.sendAwayMessage();	}	/**	 * Sends a message over the network to notify the other clients that	 * the application user is back from away.	 */	public void sendBackMessage()	{		messages.sendBackMessage();	}	/**	 * Sends a message over the network to notify the other clients that	 * a client has tried to logon using the nick name of the	 * application user.	 *	 * @param nick The nick that is already in use by the application user.	 */	public void sendNickCrashMessage( final String nick )	{		messages.sendNickCrashMessage( nick );	}	/**	 * Sends a message over the network to notify the file sender that you	 * aborted the file transfer.	 *	 * @param userCode The user code of the user sending a file.	 * @param fileHash The unique hash code of the file.	 * @param fileName The name of the file.	 */	public void sendFileAbort( final int userCode, final int fileHash, final String fileName )	{		messages.sendFileAbort( userCode, fileHash, fileName );	}	/**	 * Sends a message over the network to notify the file sender that you	 * accepted the file transfer.	 *	 * @param userCode The user code of the user sending a file.	 * @param port The port the file sender can connect to on this client	 * 		to start the file transfer.	 * @param fileHash The unique hash code of the file.	 * @param fileName The name of the file.	 */	public void sendFileAccept( final int userCode, final int port, final int fileHash, final String fileName )	{		messages.sendFileAccept( userCode, port, fileHash, fileName );	}	/**	 * Sends a message over the network to notify another user that the	 * application user wants to send a file.	 *	 * @param sendToUserCode The user code of the user asked to receive a file.	 * @param fileLength The size of the file, in bytes.	 * @param fileHash The unique hash code of the file.	 * @param fileName The name of the file.	 * @throws CommandException If there is no connection to the network,	 * 		or the application user is away,	 * 		or the file name is too long.	 */	public void sendFile( final int sendToUserCode, final long fileLength, final int fileHash, final String fileName ) throws CommandException	{		if ( !isConnected() )			throw new CommandException( "You can not send a file without being connected." );		else if ( me.isAway() )			throw new CommandException( "You can not send a file while away." );		else if ( Tools.getBytes( fileName ) > Constants.MESSAGE_MAX_BYTES )			throw new CommandException( "You can not send a file with a name with more than " + Constants.MESSAGE_MAX_BYTES + " bytes." );		else			messages.sendFile( sendToUserCode, fileLength, fileHash, fileName );	}	/**	 * Gets the list of current transfers.	 *	 * @return The list of transfers.	 */	public TransferList getTransferList()	{		return tList;	}	/**	 * Gets the list of unidentified users.	 *	 * @return The list of unidentified users.	 */	public WaitingList getWaitingList()	{		return wList;	}	/**	 * Restarts the sending and receiving network connections.	 *	 * @return True if the restart was a success.	 */	public boolean restart()	{		messages.restart();		if ( msgParser.restart() )		{			if ( !isConnected() )			{				runDelayedLogon();				sendLogOn();			}			return true;		}		return false;	}	/**	 * If any users have timed out because of missed idle messages, then	 * send a message over the network to ask all clients to identify	 * themselves again.	 */	public void updateAfterTimeout()	{		if ( nickController.isTimeoutUsers() )			messages.sendExposeMessage();	}	/**	 * Sends a message over the network with more information about this client.	 */	public void sendClientInfo()	{		messages.sendClient();	}	/**	 * Sends a private chat message over the network, to the specified user.	 *	 * @param privmsg The private message to send.	 * @param userIP The ip address of the specified user.	 * @param userPort The port to send the private message to.	 * @param userCode The user code of the user to send the private message to.	 * @throws CommandException If there is no connection to the network,	 * 		or the application user is away,	 * 		or the private message is empty,	 * 		or the private message is too long,	 * 		or the specified user has no port to send the private message to.	 */	public void sendPrivateMessage( final String privmsg, final String userIP, final int userPort, final int userCode ) throws CommandException	{		if ( !isConnected() )			throw new CommandException( "You can not send a private chat message without being connected." );		else if ( me.isAway() )			throw new CommandException( "You can not send a private chat message while away." );		else if ( privmsg.trim().length() == 0 )			throw new CommandException( "You can not send an empty private chat message." );		else if ( Tools.getBytes( privmsg ) > Constants.MESSAGE_MAX_BYTES )			throw new CommandException( "You can not send a private chat message with more than " + Constants.MESSAGE_MAX_BYTES + " bytes." );		else if ( userPort == 0 )			throw new CommandException( "You can not send a private chat message to a user with no available port number." );		else			messages.sendPrivateMessage( privmsg, userIP, userPort, userCode );	}	/**	 * Updates if the user has unread private messages for the	 * application user.	 *	 * @param code The user code for the user to update.	 * @param newMsg True if the user has unread private messages.	 */	public void changeNewMessage( final int code, final boolean newMsg )	{		nickController.changeNewMessage( code, newMsg );	}	/**	 * Returns if the client is logged on to the network.	 *	 * @return True if the client is logged on to the network.	 */	public boolean isConnected()	{		return chatState.isConnected();	}	/**	 * Sets if the client is logged on to the network.	 *	 * @param connected True if logged on to the network.	 */	public void setConnected( final boolean connected )	{		chatState.setConnected( connected );	}	/**	 * This timer task sleeps for 1.5 seconds before updating the	 * {@link WaitingList} to set the status to logged on if the	 * client was successful in connecting to the network.	 *	 * @author Christian Ihle	 */	private class DelayedLogonTask extends TimerTask	{		/**		 * The task runs as a thread.		 */		@Override		public void run()		{			try			{				Thread.sleep( 1500 );			}			catch ( final InterruptedException e )			{				LOG.log( Level.SEVERE, e.toString(), e );			}			if ( isConnected() )			{				wList.setLoggedOn( true );				// To stop the timer from running in the background				delayedLogonTimer.cancel();			}		}	}	/**	 * Creates a new instance of the {@link AutoCompleter}, with	 * a {@link CommandAutoCompleteList} and a {@link NickAutoCompleteList}.	 *	 * @return A new instance of a ready-to-use AutoCompleter.	 */	public AutoCompleter getAutoCompleter()	{		AutoCompleter autoCompleter = new AutoCompleter();		autoCompleter.addAutoCompleteList( new CommandAutoCompleteList() );		autoCompleter.addAutoCompleteList(  new NickAutoCompleteList( getNickList() ) );		return autoCompleter;	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?