001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon.subsystem;
027:
028: import com.sshtools.daemon.session.*;
029:
030: import com.sshtools.j2ssh.*;
031: import com.sshtools.j2ssh.subsystem.*;
032: import com.sshtools.j2ssh.transport.*;
033: import com.sshtools.j2ssh.util.*;
034:
035: import org.apache.commons.logging.*;
036:
037: import java.io.*;
038:
039: /**
040: *
041: *
042: * @author $author$
043: * @version $Revision: 1.12 $
044: */
045: public abstract class SubsystemServer implements Runnable {
046: private static Log log = LogFactory.getLog(SubsystemServer.class);
047: private SubsystemMessageStore incoming = new SubsystemMessageStore();
048: private SubsystemMessageStore outgoing = new SubsystemMessageStore();
049: private SubsystemInputStream in = new SubsystemInputStream(outgoing);
050: private SubsystemOutputStream out = new SubsystemOutputStream(
051: incoming);
052: private SshThread thread;
053: private StartStopState state = new StartStopState(
054: StartStopState.STOPPED);
055:
056: /** */
057: protected SessionChannelServer session;
058:
059: /**
060: * Creates a new SubsystemServer object.
061: */
062: public SubsystemServer() {
063: }
064:
065: /**
066: *
067: *
068: * @param session
069: */
070: public void setSession(SessionChannelServer session) {
071: this .session = session;
072: }
073:
074: /**
075: *
076: *
077: * @return
078: *
079: * @throws IOException
080: */
081: public InputStream getInputStream() throws IOException {
082: return in;
083: }
084:
085: /**
086: *
087: *
088: * @return
089: *
090: * @throws IOException
091: */
092: public OutputStream getOutputStream() throws IOException {
093: return out;
094: }
095:
096: /**
097: *
098: */
099: public void run() {
100: state.setValue(StartStopState.STARTED);
101:
102: try {
103: while (state.getValue() == StartStopState.STARTED) {
104: SubsystemMessage msg = incoming.nextMessage();
105:
106: if (msg != null) {
107: onMessageReceived(msg);
108: }
109: }
110: } catch (MessageStoreEOFException meof) {
111: }
112:
113: thread = null;
114: }
115:
116: /**
117: *
118: */
119: public void start() {
120: if (Thread.currentThread() instanceof SshThread) {
121: thread = ((SshThread) Thread.currentThread()).cloneThread(
122: this , "SubsystemServer");
123: thread.start();
124: } else {
125: log
126: .error("Subsystem Server must be called from within an SshThread context");
127: stop();
128: }
129: }
130:
131: /**
132: *
133: */
134: public void stop() {
135: state.setValue(StartStopState.STOPPED);
136: incoming.close();
137: outgoing.close();
138: }
139:
140: /**
141: *
142: *
143: * @return
144: */
145: public StartStopState getState() {
146: return state;
147: }
148:
149: /**
150: *
151: *
152: * @param msg
153: */
154: protected abstract void onMessageReceived(SubsystemMessage msg);
155:
156: /**
157: *
158: *
159: * @param messageId
160: * @param implementor
161: */
162: protected void registerMessage(int messageId, Class implementor) {
163: incoming.registerMessage(messageId, implementor);
164: }
165:
166: /**
167: *
168: *
169: * @param msg
170: */
171: protected void sendMessage(SubsystemMessage msg) {
172: outgoing.addMessage(msg);
173: }
174: }
|