FileDocCategorySizeDatePackage
Connection.javaAPI DocJMF 2.1.1e4818Mon May 12 12:20:56 BST 2003com.sun.media.rtsp

Connection

public class Connection extends Thread implements Runnable

Fields Summary
public int
connectionId
private Socket
socket
private RtspManager
rtspManager
private MessageProcessor
mp
private boolean
connectionIsAlive
Constructors Summary
public Connection(RtspManager rtspManager, int connectionId, byte[] dstAddress, int port)

        this.rtspManager = rtspManager;
        this.connectionId = connectionId;

        String domain = new String(dstAddress);

        InetAddress dst = InetAddress.getByName(domain);

        try {
            // System.err.println( "dst: " + dst);

            socket = new Socket(dst, port);

            // System.err.println( "TI Socket: " + socket.toString());

            start();
        } catch (IOException e) {
            throw new ConnectException();
        }
    
public Connection(RtspManager rtspManager, int connectionId, Socket socket)

        this.rtspManager = rtspManager;
        this.connectionId = connectionId;
        this.socket = socket;

        start();
    
Methods Summary
public voidcleanup()

        Debug.println("RTSP::Connection:cleanup, id=" + connectionId);

        close();

        rtspManager.removeConnection(connectionId);
    
public voidclose()

        connectionIsAlive = false;

        try {
            if (socket != null) {
                // System.out.println( "close socket");

                socket.close();

                socket = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
private booleaneomReached(byte[] buffer)

        boolean endReached = false;

        int size = buffer.length;

        if (size >= 4) {
            if (buffer[size - 4] == '\r" && buffer[size - 3] == '\n" &&
                    buffer[size - 2] == '\r" && buffer[size - 1] == '\n") {
                endReached = true;
            }
        }

        return endReached;
    
private intgetContentLength(java.lang.String msg_header)

        int length;

        int start = msg_header.indexOf("Content-length");

        if (start == -1) {
            // fix for QTSS:
            start = msg_header.indexOf("Content-Length");
        }

        if (start == -1) {
            length = 0;
        } else {
            start = msg_header.indexOf(':", start) + 2;

            int end = msg_header.indexOf('\r", start);

            String length_str = msg_header.substring(start, end);

            length = new Integer(length_str).intValue();
        }

        return length;
    
public java.lang.StringgetIpAddress()

        return socket.getInetAddress().getHostAddress();
    
public voidrun()

        // System.out.println( "Connection-" + connectionId + " running...");

        connectionIsAlive = true;

        while (connectionIsAlive) {
            try {
                InputStream in = socket.getInputStream();

                DataInputStream din = new DataInputStream(in);

                byte ch = din.readByte();

                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                // read message header:

                baos.write(ch);

                while (!eomReached(baos.toByteArray())) {
                    baos.write(din.readByte());
                }

                // read message body:

                int length = getContentLength(new String(baos.toByteArray()));

                for (int i = 0; i < length; i++) {
                    baos.write(din.readByte());
                }

                if (mp == null) {
                    mp = new MessageProcessor(connectionId, rtspManager);
                }

                mp.processMessage(baos.toByteArray());
            }
            catch (Exception e) {
                // System.out.println( "RTSP Connection terminated");
		
		connectionIsAlive = false;
            }
        }
    
public booleansendData(byte[] message)

        boolean success = false;

        try {
            OutputStream out = socket.getOutputStream();

            out.write(message);

            out.flush();

            success = true;
        } catch (IOException e) {
            // e.printStackTrace();
        }

        return success;