天天看點

初學socket(ServerThread.java)

import java.io.*;

import java.net.*;

import java.util.*;

public class ServerThread implements Runnable

{

    private ServerGui theDisplay;

    private Warehouse theWarehouse;

    private Socket theSocket;

    private int theClient;

    private ObjectInputStream in = null;

    private ObjectOutputStream out = null;

    private boolean process = true;

    private Customer cust;

    private Branch theBranch;

    private static final String NEWLINE = System.getProperty("line.separator");

    public ServerThread(int clientNo, Socket clientSocket,

            Warehouse aWarehouse, ServerGui display)

    {

        theClient = clientNo;

        theSocket = clientSocket;

        theWarehouse = aWarehouse;

        theDisplay = display;

    }

    public void run()

    {

        theDisplay.statusMessage("Client " + theClient +

                        " Thread started" + NEWLINE);

        try

        {

            in = new ObjectInputStream((theSocket.getInputStream()));

        }

        catch (IOException e)

        {

            theDisplay.statusMessage("Client " + theClient +

                    " inbound connection failed" + NEWLINE);

            System.exit(-1);

        }

        try

        {

            out = new ObjectOutputStream((theSocket.getOutputStream()));

        }

        catch (IOException e)

        {

            theDisplay.statusMessage("Client " + theClient +

                    " outbound connection failed" + NEWLINE);

            System.exit(-1);

        }  

        while(process)

        {

            // get incoming details

            TransmissionData mess = null;

            try

            {

                mess = (TransmissionData) in.readObject();

            }

            catch (IOException e)

            {

                theDisplay.statusMessage("Client " + theClient +

                    " read failed" + NEWLINE);

                System.exit(-1);

            }

            catch (ClassNotFoundException e)

            {

                theDisplay.statusMessage(e.getMessage());

            }

            // procees the incoming message

            mess = processRequest(mess);

            if (process)

            {

                try

                {

                    out.writeObject((TransmissionData) mess);

                }

                catch (IOException e)

                {

                    theDisplay.statusMessage("Client " + theClient +

                        " send failed" + NEWLINE);

                    System.exit(-1);

                }

            }

        }

        theDisplay.statusMessage("Client " + theClient +

                        " Thread completed" + NEWLINE);

    }

    private TransmissionData processRequest(TransmissionData details)

    {

        TransmissionData input = details;

        TransmissionData output = null;

        // login requested

        if (input.getDataType() == TransmissionData.CLT_LOGIN)

        {

            output = processLogin(input);

        }

        // branch selected

        if (input.getDataType() == TransmissionData.CLT_BRANCH)

        {

            output = processBranch(input);

        }

        // client order completion requested

        if (input.getDataType() == TransmissionData.CLT_COMPLETE)

        {

            output = processOrder(input);

        }

        // client closedown requested

        if (input.getDataType() == TransmissionData.CLT_CLOSE)

        {

            processClose();

            output = null;

        }

        return output;

    }

    private TransmissionData processLogin(TransmissionData details)

    {

        TransmissionData input = details;

        TransmissionData output = null;

        // determine customer sent

        String custCode;

        custCode = (String) input.getData();

        theDisplay.statusMessage("Client " + theClient + " Login request "

                + custCode + NEWLINE);

        cust = theWarehouse.getCustomer(custCode);

        // customer exists?

        if (cust == null)

        {

            theDisplay.statusMessage("Client " + theClient +

                    " Customer refused" + NEWLINE);

            output = new TransmissionData(TransmissionData.SRV_ERROR,

                "Customer does not exist.");

        }

        else

        {

            String data1, data2;

            // send branch details

            String branchData = theWarehouse.getBranchCodes_Names();

            StringTokenizer st = new StringTokenizer(branchData, "*");           

            Vector<Branch> branchList = new Vector<Branch>();

            while (st.hasMoreTokens())

            {

                data1 = st.nextToken();

                if (!(data1.equals("END")))

                {

                    data2 = st.nextToken();

                    Branch tempBranch = new Branch(data1, data2);

                    branchList.addElement(tempBranch);

                }

            }

            theDisplay.statusMessage("Client " + theClient +

                    " Customer accepted" + NEWLINE);

            output = new TransmissionData(TransmissionData.SRV_BRANCHLIST,

                    branchList);

        }

        return output;

    }

    private TransmissionData processBranch(TransmissionData details)

    {

        TransmissionData input = details;

        TransmissionData output = null;

        String branchRequest = (String) input.getData();

        theDisplay.statusMessage("Client " + theClient +

                    " Branch request " + branchRequest + NEWLINE);

        //theBranch = theWarehouse.getBranch(branchRequest);

        String stockData = theWarehouse.getBranchStockList(branchRequest);

        String data1, data2, data3;

        int data4;

        // send branch item details

        StringTokenizer st = new StringTokenizer(stockData, "*");           

        Vector<StockItem> stockList = new Vector<StockItem>();

        while (st.hasMoreTokens())

        {

            data1 = st.nextToken();

            if (!(data1.equals("END")))

            {

                data2 = st.nextToken();

                data3 = st.nextToken();

                data4 = Integer.parseInt(st.nextToken());

                StockItem tempItem = new StockItem(data1, data2, data3,

                        data4);

                stockList.addElement(tempItem);

            }

        }

        theDisplay.statusMessage("Client " + theClient +

                    " Branch accepted" + NEWLINE);

        output = new TransmissionData(TransmissionData.SRV_STOCKLIST,

                stockList);

        return output;

    }

    private TransmissionData processOrder(TransmissionData details)

    {

        theDisplay.statusMessage("Client " + theClient +

                " complete Order requested" + NEWLINE);

        TransmissionData input = details;

        TransmissionData output = null;

        String basketDetails;

        basketDetails = cust.getCustomerCode() + "*";

        // warning issued in compilation due to unchecked cast from

        // input.getData() returning an object - see Generics

        Vector<BasketItem> items = (Vector<BasketItem>) input.getData();

        Iterator it = items.iterator();

        while (it.hasNext())

        {

            BasketItem line = (BasketItem) it.next();

            basketDetails = basketDetails + line.getItemCode() + "*";

            basketDetails = basketDetails + line.getDescription() + "*";

            basketDetails = basketDetails + line.getPrice() + "*";

            basketDetails = basketDetails + line.getNumberOrdered() + "*";

        }

        basketDetails = basketDetails + "END*";

        String orderNo = theWarehouse.processBasket(basketDetails);

        output = new TransmissionData(TransmissionData.SRV_ORDER, orderNo);

        theDisplay.statusMessage("Client " + theClient +

                " Order " + orderNo + " completed" + NEWLINE);

        return output;

    }

    private void processClose()

    {

        theDisplay.statusMessage("Client " + theClient +

                " shutdown requested." + NEWLINE);

        try

        {

            theSocket.close();

        }

        catch (IOException e)

        {

            theDisplay.statusMessage("Client " + theClient +

                " shutdown exception " +e.getMessage() + NEWLINE);

        }

        try

        {

            in.close();

        }

        catch (IOException e)

        {

            theDisplay.statusMessage("Client " + theClient +

                " read close exception " +e.getMessage() + NEWLINE);

        }

        try

        {

            out.close();

        }

        catch (IOException e)

        {

            theDisplay.statusMessage("Client " + theClient +

                " write close exception " +e.getMessage() + NEWLINE);

        }

        process = false;

    }

}