001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.util;
017:
018: import net.sf.jftp.*;
019: import net.sf.jftp.config.*;
020: import net.sf.jftp.gui.framework.*;
021: import net.sf.jftp.net.*;
022: import net.sf.jftp.util.*;
023:
024: import java.awt.*;
025: import java.awt.event.*;
026:
027: import java.io.*;
028:
029: import java.net.*;
030:
031: import java.util.*;
032:
033: import javax.swing.*;
034: import javax.swing.event.*;
035:
036: /*
037: alternative connection class, used for raw tcp/ip connection
038: */
039: public class JRawConnection implements Runnable {
040: private int timeout = Settings.connectionTimeout;
041: private String host;
042: private int port;
043: private PrintStream out;
044: private DataInputStream in;
045: private Socket s;
046: private JReciever jrcv;
047: private boolean isOk = false;
048: private boolean established = false;
049: private boolean reciever = false;
050: private Thread runner;
051:
052: public JRawConnection(String host, int port) {
053: this (host, port, false);
054: }
055:
056: public JRawConnection(String host, int port, boolean reciever) {
057: this .host = host;
058: this .port = port;
059: this .reciever = reciever;
060:
061: runner = new Thread(this );
062: runner.start();
063: }
064:
065: public void run() {
066: try {
067: s = new Socket(host, port);
068:
069: // s.setSoTimeout(Resource.socketTimeout);
070: out = new PrintStream(s.getOutputStream());
071: in = new DataInputStream(s.getInputStream());
072:
073: if (reciever) {
074: JReciever jrcv = new JReciever(in);
075: }
076:
077: isOk = true;
078: } catch (Exception ex) {
079: isOk = false;
080: }
081:
082: established = true;
083: }
084:
085: public boolean isThere() {
086: int cnt = 0;
087:
088: while (!established && (cnt < timeout)) {
089: pause(100);
090: cnt = cnt + 100;
091: }
092:
093: return isOk;
094: }
095:
096: public void send(String data) {
097: try {
098: out.println(data);
099: } catch (Exception ex) {
100: System.out.println(ex + "@JConnection.send()");
101: }
102: }
103:
104: public PrintStream getInetOutputStream() {
105: return out;
106: }
107:
108: public DataInputStream getInetInputStream() {
109: return in;
110: }
111:
112: private void pause(int time) {
113: try {
114: Thread.sleep(time);
115: } catch (Exception ex) {
116: System.out.println(ex);
117: }
118: }
119: }
|