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 ClientThread extends Thread
{



    private Socket mSocket;
    private PiriSocket mPiriSocket;

    private Status mStatus;

    public ClientThread(Connection pParent, Socket pSocket)
    {
        mSocket = pSocket;
    }

    public void run()
    {
        try
        {
            mPiriSocket = new PiriSocket(mSocket);
            System.out.println("Running client thread");
            while(true)
            {
                ChatMessage chatMessage = mPiriSocket.recieveMessage();
                PiriServer.dataRecieved(this, chatMessage);
            }
        }
        catch(IOException e)
        {
            PiriServer.fatalError(e);
        }
        catch(PiriException e)
        {
            PiriServer.fatalError(e);
        }
        catch(ClassNotFoundException e)
        {
            PiriServer.fatalError(e);
        }
    }

    public PiriSocket getSocket()
    {
        return mPiriSocket;
    }

    public String getUserName()
    {
        return mUserName;
    }

    public void setUserName(String pUserName) throws PiriException
    {
        if (pUserName.length() == 0)
            throw new PiriZeroLengthException("pUserName");
        mUserName = pUserName;
    }

    public Status getStatus()
    {
        return mStatus;
    }

    public void setStatus(Status pStatus)
    {
        mStatus = pStatus;
    }
}
