1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
//---PACKAGE DECLARATION--- |
21 |
|
22 |
//---IMPORTS--- |
23 |
import java.io.*; |
24 |
import java.net.*; |
25 |
import java.util.ArrayList; |
26 |
|
27 |
import org.xml.sax.*; |
28 |
import javax.xml.parsers.SAXParserFactory; |
29 |
import javax.xml.parsers.ParserConfigurationException; |
30 |
import javax.xml.parsers.SAXParser; |
31 |
|
32 |
/** |
33 |
* XMLStreamReaderThread. |
34 |
* |
35 |
* @author $Author: pjm2 $ |
36 |
* @version $Id: XMLStreamReaderThread.java,v 1.1 2001/01/14 21:58:11 pjm2 Exp $ |
37 |
*/ |
38 |
public class XMLStreamReaderThread extends Thread { |
39 |
|
40 |
//---FINAL ATTRIBUTES--- |
41 |
|
42 |
/** |
43 |
* The current CVS revision of this class |
44 |
*/ |
45 |
public final String REVISION = "$Revision: 1.1 $"; |
46 |
|
47 |
//---STATIC METHODS--- |
48 |
|
49 |
//---CONSTRUCTORS--- |
50 |
|
51 |
// Constructor |
52 |
public XMLStreamReaderThread (String serverName, int serverPort, TerminalScreen screen) { |
53 |
_serverName = serverName; |
54 |
_serverPort = serverPort; |
55 |
_screen = screen; |
56 |
} |
57 |
|
58 |
//---PUBLIC METHODS--- |
59 |
|
60 |
public void run() { |
61 |
|
62 |
final int maxPacketSize = 8196; |
63 |
final int startByte = 0; |
64 |
final int endByte = 1; |
65 |
|
66 |
BufferedReader br = null; |
67 |
try { |
68 |
Socket conn = new Socket(_serverName, _serverPort); |
69 |
InputStreamReader isr = new InputStreamReader(conn.getInputStream()); |
70 |
br = new BufferedReader(isr); |
71 |
|
72 |
} |
73 |
catch(Exception e) { |
74 |
_screen.clear(); |
75 |
System.out.println("Cannot connect to "+_serverName+":"+_serverPort); |
76 |
System.exit(1); |
77 |
} |
78 |
|
79 |
DataLayout layout = new DataLayout(_screen, JavaCLIParameters.showData); |
80 |
|
81 |
// Use the default (non-validating) parser |
82 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
83 |
while (1 == 1) { |
84 |
try { |
85 |
String xml = br.readLine(); |
86 |
|
87 |
// Parse the input |
88 |
XMLPacket packet = new XMLPacket(); |
89 |
SAXParser saxParser = factory.newSAXParser(); |
90 |
saxParser.parse(new InputSource(new StringReader(xml)), new XMLStreamParser(packet)); |
91 |
|
92 |
|
93 |
// For out information |
94 |
//_screen.gotoxy(4, 5); |
95 |
//_screen.print(packet.printAll(), TerminalScreen.RED); |
96 |
|
97 |
layout.updateScreen(packet); |
98 |
|
99 |
} |
100 |
catch (IOException e) { |
101 |
System.out.println("Connection to server broken."); |
102 |
break; |
103 |
} |
104 |
catch (Exception e) { |
105 |
System.out.println("Error caught in the Thread: "+e); |
106 |
break; |
107 |
} |
108 |
} |
109 |
|
110 |
_screen.clear(); |
111 |
|
112 |
} |
113 |
|
114 |
|
115 |
//---PRIVATE METHODS--- |
116 |
|
117 |
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
119 |
//---ATTRIBUTES--- |
120 |
|
121 |
private String _serverName; |
122 |
private int _serverPort; |
123 |
private TerminalScreen _screen; |
124 |
|
125 |
//---STATIC ATTRIBUTES--- |
126 |
|
127 |
} |