⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientservicer.java

📁 无线传感器网络操作系统源代码
💻 JAVA
字号:
/* * "Copyright (c) 2001 and The Regents of the University * of California.  All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * $\Id$ *//** * File: ServerReceivingThread.java * * Description: * The ServerReceivingThread listens for requests * from a connected Aggregator Server.  If a data * packet is received, it is sent on to the serial * port. * * @author <a href="mailto:bwhull@sourceforge.net">Bret Hull</a> * */package net.tinyos.SerialForwarder;import java.net.*;import java.io.*;import java.util.*;public class ClientServicer extends Thread{    // communications with client    private Socket              m_socket        = null;    private int                 m_nTimeout      = 5000;    private InputStream         input           = null;    public OutputStream        output          = null;    // host name    private String              m_strHstNm      = null;    // listen server to which thread is registered    private ListenServer        lstnSrvr      = null;    // shutdown flag    private boolean             bShutdown     = false;    private boolean             bFirstTime    = true;    private static Vector              vctServicers  = new Vector();    private ClientServicer ( Socket socket )    {        m_socket = socket;        m_strHstNm = socket.getInetAddress().toString();        SerialForward.DEBUG ( "ServerReceivingThread created to service host " + m_strHstNm );    }    private void InitConnection ( )    {        try        {          output = m_socket.getOutputStream();          input = m_socket.getInputStream();          SerialPortIO.RegisterPacketForwarder( this );        }        catch (Exception e )        {          e.printStackTrace();          bShutdown = true;          return;        }        return;    }    public void run()    {        //open socket inputstream        SerialForward.VERBOSE( "client connected" );        InitConnection ( );        //read packets from stream        ReadPackets ( );        //close socket        Shutdown ();        // thread about to die, remove from listen server receiving threads vector        SerialForward.VERBOSE("client disconnected");        SerialForward.DEBUG ( "ClientServicer: terminating host = " + m_strHstNm );    }    private synchronized void ReadPackets ( )    {        int nBytesRead = 0;        int nBytesReturned = 0;        byte[] currentPacket = new byte[SerialForward.PACKET_SIZE];        try {            nBytesReturned = input.read ( currentPacket, nBytesRead,                                          SerialForward.PACKET_SIZE - nBytesRead );            while ( nBytesReturned != -1 && (!bShutdown || bFirstTime) )            {                bFirstTime = false;                nBytesRead += nBytesReturned;                if ( nBytesRead == SerialForward.PACKET_SIZE )                {                    // send packet to serial port                    nBytesRead = 0;                    HandlePacket ( currentPacket );                }                nBytesReturned = input.read ( currentPacket, nBytesRead,                                              SerialForward.PACKET_SIZE - nBytesRead );            }        }        catch (IOException e)        {            SerialForward.DEBUG ( "ClientServicer: connection was closed to host " + m_strHstNm );        }    }    private void HandlePacket (byte[] currentPckt)    {        SerialForward.DEBUG ( "Packet received from " + m_strHstNm );        SerialForward.IncrementPacketsWritten();        SerialPortIO.WriteBytes ( currentPckt );    }    public void Shutdown ( )    {        if ( !bShutdown ) {            //unregister output stream            bShutdown = true;            SerialPortIO.UnregisterPacketForwarder ( this );            vctServicers.remove ( this );            SerialForward.DecrementClients ();            try{ m_socket.close(); }            catch ( IOException e ) { e.printStackTrace(); }            this.interrupt();        }    }    public static ClientServicer AddClientServicer (Socket clntSocket)    {        ClientServicer newServicer = new ClientServicer ( clntSocket );        vctServicers.add ( newServicer );        newServicer.start();        return newServicer;    }    public static void ShutdownAllClientServicers ( )    {        SerialForward.VERBOSE( "CLIENTSERVICER: Shutting down all client connections" );        ClientServicer crrntServicer;        while ( vctServicers.size() != 0 )        {            crrntServicer = (ClientServicer) vctServicers.firstElement();            crrntServicer.Shutdown();            try {  crrntServicer.join(1000); }            catch (InterruptedException e) { e.printStackTrace(); };        }    }}

⌨️ 快捷键说明

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