ReaderThreadImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.transport;
27
28import java.io.IOException;
29
30import com.sun.corba.se.pept.transport.Connection;
31import com.sun.corba.se.pept.transport.ReaderThread;
32import com.sun.corba.se.pept.transport.Selector;
33
34import com.sun.corba.se.spi.orb.ORB;
35import com.sun.corba.se.spi.orbutil.threadpool.Work;
36
37import com.sun.corba.se.impl.orbutil.ORBUtility;
38
39public class ReaderThreadImpl
40    implements
41        ReaderThread,
42        Work
43{
44    private ORB orb;
45    private Connection connection;
46    private Selector selector;
47    private boolean keepRunning;
48    private long enqueueTime;
49
50    public ReaderThreadImpl(ORB orb,
51                            Connection connection, Selector selector)
52    {
53        this.orb = orb;
54        this.connection = connection;
55        this.selector = selector;
56        keepRunning = true;
57    }
58
59    ////////////////////////////////////////////////////
60    //
61    // ReaderThread methods.
62    //
63
64    public Connection getConnection()
65    {
66        return connection;
67    }
68
69    public void close()
70    {
71        if (orb.transportDebugFlag) {
72            dprint(".close: " + connection);
73        }
74
75        keepRunning = false;
76    }
77
78    ////////////////////////////////////////////////////
79    //
80    // Work methods.
81    //
82
83    // REVISIT - this needs alot more from previous ReaderThread.
84    public void doWork()
85    {
86        try {
87            if (orb.transportDebugFlag) {
88                dprint(".doWork: Start ReaderThread: " + connection);
89            }
90            while (keepRunning) {
91                try {
92
93                    if (orb.transportDebugFlag) {
94                        dprint(".doWork: Start ReaderThread cycle: "
95                               + connection);
96                    }
97
98                    if (connection.read()) {
99                        // REVISIT - put in pool;
100                        return;
101                    }
102
103                    if (orb.transportDebugFlag) {
104                        dprint(".doWork: End ReaderThread cycle: "
105                               + connection);
106                    }
107
108                } catch (Throwable t) {
109                    if (orb.transportDebugFlag) {
110                        dprint(".doWork: exception in read: " + connection,t);
111                    }
112                    orb.getTransportManager().getSelector(0)
113                        .unregisterForEvent(getConnection().getEventHandler());
114                    getConnection().close();
115                }
116            }
117        } finally {
118            if (orb.transportDebugFlag) {
119                dprint(".doWork: Terminated ReaderThread: " + connection);
120            }
121        }
122    }
123
124    public void setEnqueueTime(long timeInMillis)
125    {
126        enqueueTime = timeInMillis;
127    }
128
129    public long getEnqueueTime()
130    {
131        return enqueueTime;
132    }
133
134    public String getName() { return "ReaderThread"; }
135
136    ////////////////////////////////////////////////////
137    //
138    // Implementation.
139    //
140
141    private void dprint(String msg)
142    {
143        ORBUtility.dprint("ReaderThreadImpl", msg);
144    }
145
146    protected void dprint(String msg, Throwable t)
147    {
148        dprint(msg);
149        t.printStackTrace(System.out);
150    }
151}
152
153// End of file.
154