package apz.pirichat.server;
import java.net.Socket;
import java.io.IOException;
import apz.pirichat.shared.PiriSocket;
import apz.pirichat.shared.ChatMessage;
import apz.pirichat.shared.PiriException;
import apz.pirichat.shared.PiriZeroLengthException;

public class ClientSendThread extends Thread
{
    private Socket mSocket;
    private Connection mConnection;
    private PiriSocket mPiriSocket;

    public ClientSendThread(Connection pConnection, Socket pSocket)
    {
        mConnection = pConnection;
        mSocket = pSocket;
    }

    public PiriSocket getSocket()
    {
        return mPiriSocket;
    }

    public Connection getConnection()
    {
        return mConnection;
    }

    public void run()
    {
        try
        {
            mPiriSocket = new PiriSocket(mSocket);
            while(true)
            {
                ChatMessage chatMessage = mPiriSocket.recieveMessage();
                PiriServer.dataRecieved(getConnection(), chatMessage);
            }
        }
        catch(IOException e)
        {
            PiriServer.fatalError(e);
        }
        catch(PiriException e)
        {
            PiriServer.fatalError(e);
        }
        catch(ClassNotFoundException e)
        {
            PiriServer.fatalError(e);
        }
    }
}
