001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.container.server.socket;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    import java.io.PrintWriter;
022    import java.net.InetAddress;
023    
024    import com.hs.mail.io.LineReader;
025    
026    /**
027     * 
028     * @author Won Chul Doh
029     * @since May 28, 2010
030     * 
031     */
032    public class TcpTransport {
033    
034            private TcpSocketChannel channel = null;
035        private InputStream is = null;
036        private OutputStream os = null;
037        private LineReader reader = null;
038        private PrintWriter writer = null;
039        private boolean sessionEnded = false;
040    
041            public TcpTransport() throws IOException {
042            }
043        
044            public void setChannel(TcpSocketChannel channel) throws IOException {
045                    this.channel = channel;
046                    this.is = channel.getInputStream();
047                    this.os = channel.getOutputStream();
048                    this.reader = new LineReader(this.is);
049                    this.writer = new PrintWriter(this.os, true);
050            }
051            
052            public TcpSocketChannel getChannel() {
053                    return channel;
054            }
055            
056            public InetAddress getRemoteAddress() {
057                    return channel.getSocket().getInetAddress();
058            }
059    
060            public InputStream getInputStream() throws IOException {
061                    return is;
062            }
063    
064            public OutputStream getOutputStream() throws IOException {
065                    return os;
066            }
067            
068            public boolean isSessionEnded() {
069                    return sessionEnded;
070            }
071    
072            public void endSession() {
073                    this.sessionEnded = true;
074            }
075    
076            public String readLine() throws IOException {
077                    return reader.readLine();
078            }
079            
080            public void println(String str) {
081                    writer.println(str);
082            }
083            
084    }