Package msg

Source Code of msg.MessageSender

package msg;


import network.Host;
import network.NetworkClient;
import network.StatusManager;
import util.Constants;
import util.Globals;


public class MessageSender implements Runnable
{
    private Message message;
   
    @Override
    public void run()
    {
        NetworkClient sender = new NetworkClient(message.getReceiverIP(), Constants.PORT_TCP);
        if (!sender.sendMessage(message))
        {
            sendErrorMessage(message);
            StatusManager.updateAnotherHostStatus(Host.findHostByIP(message.getReceiverIP()), false);
        }
    }

    public MessageSender(Message message)
    {
        this.message = message;
    }
   
    /**
     * Envia un mensaje de error al no poder enviar un mensaje o imagen
     * @param message
     */
    private void sendErrorMessage(Message message)
    {
        // No avisar mensajes de error para cambios de status
        if (message.getMessageKind() == Constants.MESSAGE_KIND_STATUS)
        {
            System.out.println(" ** ERROR ENVIANDO "+ message.getMessageKindDescription() +" a " + message.getReceiver() + " ** ");
            return;
        }
       
        // Nuevo mensaje de error
        Message errorMessage = new Message();
        errorMessage.setSender(message.getSender());
        errorMessage.setSenderIP(message.getSenderIP());
        errorMessage.setReceiver(message.getSender());
        errorMessage.setReceiverIP(message.getSenderIP());
        errorMessage.setMessageContent(" <font color=red><i> ** ERROR ENVIANDO "+ message.getMessageKindDescription() +" a " + message.getReceiver() + " ** </i></font>");
        errorMessage.setMessageKind(Constants.MESSAGE_KIND_TEXT);
        errorMessage.setToAll(false);
       
        new Thread(new MessageSender(errorMessage)).start();
    }
   
}
TOP

Related Classes of msg.MessageSender

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.