Logo Search packages:      
Sourcecode: jaminid version File versions  Download package

void com::prolixtech::jaminid::Connection::run (  )  [inline]

The thread runs here. This essentially allows the Daemon to serve multiple threads. ALL IO is carried out here.

Definition at line 78 of file Connection.java.

References com::prolixtech::jaminid::Request::addRequestLines(), com::prolixtech::jaminid::ContentOracle::demultiplex(), com::prolixtech::jaminid::ContentOracle::getDelegatedOracle(), com::prolixtech::jaminid::Request::getHeader(), com::prolixtech::jaminid::StreamingOracle::getNextBatch(), com::prolixtech::jaminid::Daemon::getOracle(), com::prolixtech::jaminid::Daemon::getProtocol(), com::prolixtech::jaminid::StreamingOracle::getStreamInterval(), com::prolixtech::jaminid::ContentOracle::Instance(), printlog(), com::prolixtech::jaminid::Response::publishHeader(), sendString(), com::prolixtech::jaminid::Response::setHeaderLine(), com::prolixtech::jaminid::Request::switchToBody(), and com::prolixtech::jaminid::Request::switchToCompleted().

                      {
        try {
            
            printlog("Processing Connection.");

            CONNECTED:
            while(true){
            StringBuffer requestInputBuffer = new StringBuffer();

            int byteIn = -1;
            int CRLFState = 0;
            boolean doubleCRLFpassed = false;
            Request serviceRequest = new Request(masterDaemon, this);
            int expectedBodySize = 0;
            int bodyCharsParsed = 0;
            INPUTLOOP:
            do {
                if(socketInput.available()==0){             
                    Timer a = new Timer();
                    ExpireTask timerTask = new ExpireTask();
                    a.schedule(timerTask, lineTimeout);
                    while(timerTask.running){
                        if(socketInput.available()>0) timerTask.running=false;
                        Suspender.suspendMillis(lineCheckInterval);
                      }
                    if(socketInput.available()==0){break INPUTLOOP;}
                }
                
                byteIn = socketInput.read();
                if (byteIn > 0) {

                    requestInputBuffer.append((char) byteIn);
                    if (doubleCRLFpassed)
                        bodyCharsParsed++;

                    // LINEAR CRLF State Detection
                    // case 0: none.
                    // case 1: \r
                    // case 2: \r\n
                    // case 3: \r\n\r
                    // case 4: \r\n\r\n
                    char thischar = ((char) byteIn);

                    switch (CRLFState) {
                    case 0:
                        if ("\r".charAt(0) == (thischar)) {
                            CRLFState++;
                        }
                        break;
                    case 1:
                        if ("\n".charAt(0) == (thischar)) {
                            CRLFState++;
                        } else {
                            CRLFState = 0;
                        }
                        break;
                    case 2:
                        if ("\r".charAt(0) == (thischar)) {
                            CRLFState++;
                        } else {
                            CRLFState = 0;
                        }
                        break;
                    case 3:
                        if ("\n".charAt(0) == (thischar)) {
                            CRLFState++;
                            doubleCRLFpassed = true;
                            serviceRequest.addRequestLines(requestInputBuffer
                                    .toString());

                            requestInputBuffer = new StringBuffer();

                            expectedBodySize = serviceRequest.switchToBody();
                        } else {
                            CRLFState = 0;
                        }
                        break;
                    }

                }

                // System.out.println(doubleCRLFpassed + " B/E: " +
                // bodyCharsParsed + " : " + expectedBodySize);
                // && byteIn!=-1 && socketInput.available()>0
            } while (!doubleCRLFpassed || doubleCRLFpassed
                    && bodyCharsParsed < expectedBodySize || doubleCRLFpassed
                    && byteIn != -1 && socketInput.available() > 0);

            printlog("Request: " + requestInputBuffer.toString());
            serviceRequest.addRequestLines(requestInputBuffer.toString());

            serviceRequest.switchToCompleted();

            Response serviceResponse = new Response(Protocol.OK, masterDaemon
                    .getProtocol(), this.socketOutput, serviceRequest);

            serviceResponse.setHeaderLine(ProtocolResponseHeader.Content_Type, "text/html");


            serviceResponse.setHeaderLine(ProtocolResponseHeader.Date,
                    getGMTString(new Date()));

            ContentOracle chout = masterDaemon.getOracle();
            
            String oracleOutput = null;

            chout = chout.getDelegatedOracle(serviceRequest);

            try {
                oracleOutput = chout.demultiplex(serviceRequest,
                        serviceResponse);
            } catch (Exception e) {
                SingletonLogger.Instance().severe("User Oracle FAILED");
                e.printStackTrace();
                if (chout != ContentOracle.Instance()) {
                    try {
                        oracleOutput = ContentOracle.Instance().demultiplex(
                                serviceRequest, serviceResponse);
                    } catch (Exception f) {
                        f.printStackTrace();
                        SingletonLogger.Instance().severe(
                                "Fallback Oracle FAILED");
                    }
                }

            }

            serviceResponse.publishHeader();

            if (chout instanceof StreamingOracle) {
                StreamingOracle shout = (StreamingOracle) chout;
                while (isalive) {

                    Suspender.suspendMillis(shout.getStreamInterval());
                    this.sendString(shout.getNextBatch(serviceRequest,
                            serviceResponse));
                }
            } else {
                this.sendString(oracleOutput);
            }

            socketOutput.flush();
            
            if( ((String) serviceRequest.getHeader().get(ProtocolResponseHeader.Connection)).equalsIgnoreCase("Close") ){

                serviceResponse.setHeaderLine(ProtocolResponseHeader.Connection, "close");
                socketOutput.close();
                printlog("Connection Done.");
                break CONNECTED;
            }
        }
            
            

        } catch (Exception e) {
            printlog("Exception: " + e);
        } finally {
            try {
                activeSocket.close();
            } catch (Exception e) {
                System.err.println("Socket not closed");
            }
        }

    }


Generated by  Doxygen 1.6.0   Back to index