Subversion Repositories jheat

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
package heizung;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.io.PrintWriter;
7
import java.net.Socket;
8
import java.net.SocketTimeoutException;
9
import java.net.UnknownHostException;
10
 
11
public class network
12
{
13
	private boolean DEBUG = false;
14
 
15
	private Socket s = null;
16
	private PrintWriter out = null;
17
	private BufferedReader in = null;
18
	private String strServerName = "localhost";
19
	private int online = 0;
20
 
21
	public network(String sn)
22
	{
23
		if (!sn.isEmpty())
24
			strServerName = sn;
25
 
26
		online = 0;
27
	}
28
 
29
	public boolean connect()
30
	{
31
		try {
32
			online = 2;
33
	        s = new Socket(strServerName, 11001);
34
	        s.setSoTimeout(3000);
35
	        out = new PrintWriter(s.getOutputStream(), true);
36
	        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
37
	    }
38
		catch (UnknownHostException e)
39
		{
40
	        System.err.println("Don't know about host: "+strServerName+": "+e.getMessage());
41
	        online = 0;
42
	        return false;
43
	    }
44
		catch (SocketTimeoutException e)
45
		{
46
			System.err.println("Could't set timeout: "+e.getMessage());
47
			online = 0;
48
			return false;
49
		}
50
		catch (IOException e)
51
		{
52
	        System.err.println("Couldn't get I/O for the connection to: "+strServerName+": "+e.getMessage());
53
	        online = 0;
54
	        return false;
55
	    }
56
 
57
		// Just wait a little to let daemon initialize
58
		try
59
		{
60
			Thread.sleep(500);
61
		}
62
		catch (InterruptedException e1)
63
		{
64
			System.out.println("Error: "+e1.getMessage());
65
		}
66
 
67
		online = 1;
68
		return true;
69
	}
70
 
71
	public int write(String str)
72
	{
73
		if (online == 1 && !str.isEmpty())
74
		{
75
			out.print(str);
76
			out.flush();
77
		}
78
 
79
		if (online != 1)
80
		{
81
			System.err.println("There is no valid network connection!");
82
			return 0;
83
		}
84
 
85
		return str.length();
86
	}
87
 
88
	public String read()
89
	{
90
		char[] ch;
91
		String answer = "";
92
 
93
		if (online != 1)
94
		{
95
			System.err.println("There is no valid network connection!");
96
			return "";
97
		}
98
 
99
		ch = new char[2];
100
 
101
		try
102
		{
103
			while (in.read(ch, 0, 1) != -1)
104
			{
105
				if (ch[0] == ';')
106
				{
107
					if (DEBUG)
108
						System.out.println("Found answer:"+answer);
109
 
110
					return answer;
111
				}
112
 
113
				answer = answer+ch[0];
114
			}
115
		}
116
		catch (SocketTimeoutException e)
117
		{
118
			System.err.println("Error: "+e.getMessage());
119
		}
120
		catch (IOException e)
121
		{
122
			System.err.println("Error reading from stream: "+e.getMessage());
123
		}
124
 
125
		return "";
126
	}
127
 
128
	public void close()
129
	{
130
		if (online != 1)
131
			return;
132
 
133
		out.print("quit;");
134
		out.close();
135
 
136
		try
137
		{
138
			in.close();
139
			s.close();
140
		}
141
		catch (IOException e)
142
		{
143
			System.err.println("Error closing a socket: "+e.getMessage());
144
		}
145
	}
146
}