1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* http://www.i-scream.org |
4 |
+ |
* Copyright (C) 2000-2002 i-scream |
5 |
+ |
* |
6 |
+ |
* This program is free software; you can redistribute it and/or |
7 |
+ |
* modify it under the terms of the GNU General Public License |
8 |
+ |
* as published by the Free Software Foundation; either version 2 |
9 |
+ |
* of the License, or (at your option) any later version. |
10 |
+ |
* |
11 |
+ |
* This program is distributed in the hope that it will be useful, |
12 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
+ |
* GNU General Public License for more details. |
15 |
+ |
* |
16 |
+ |
* You should have received a copy of the GNU General Public License |
17 |
+ |
* along with this program; if not, write to the Free Software |
18 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
+ |
*/ |
20 |
+ |
|
21 |
|
//---PACKAGE DECLARATION--- |
22 |
< |
package uk.ac.ukc.iscream.util; |
22 |
> |
package uk.org.iscream.cms.util; |
23 |
|
|
24 |
|
//---IMPORTS--- |
25 |
|
import java.net.*; |
26 |
|
import java.io.*; |
27 |
|
|
28 |
|
/** |
29 |
< |
* The Simple Mail Transfer Protocol class. This class was borrowed |
30 |
< |
* for the GJT, and scaled to do just what we require. |
29 |
> |
* The Simple Mail Transfer Protocol class. This class was inspired |
30 |
> |
* by the GJT. |
31 |
|
* |
32 |
|
* @author $Author$ |
33 |
|
* @version $Id$ |
67 |
|
_socket = new Socket(server, port); |
68 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
69 |
|
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
70 |
< |
_socketIn.readLine(); // get 220 welcome header |
51 |
< |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
70 |
> |
init(); |
71 |
|
} |
72 |
|
|
73 |
|
//---PUBLIC METHODS--- |
135 |
|
public void sendCommand(String cmd, int reply) throws IOException { |
136 |
|
_socketOut.println(cmd); |
137 |
|
String temp = _socketIn.readLine(); |
138 |
< |
if (!temp.startsWith(new Integer(reply).toString())) { |
138 |
> |
if (temp == null) { |
139 |
> |
throw new IOException ("IO error reading from socket, connection to server died?"); |
140 |
> |
} |
141 |
> |
if (!temp.startsWith(String.valueOf(reply))) { |
142 |
|
throw new IOException ("Expected " + reply + ", got " + temp); |
143 |
|
} |
144 |
|
} |
147 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
148 |
|
* method to provide clean logging (every class should have this). |
149 |
|
* |
150 |
< |
* This uses the uk.ac.ukc.iscream.util.FormatName class |
150 |
> |
* This uses the uk.org.iscream.cms.server.util.FormatName class |
151 |
|
* to format the toString() |
152 |
|
* |
153 |
|
* @return the name of this class and its CVS revision |
160 |
|
} |
161 |
|
|
162 |
|
//---PRIVATE METHODS--- |
163 |
+ |
|
164 |
+ |
/** |
165 |
+ |
* Check the server sends a 220 message, and |
166 |
+ |
* then send our HELO. |
167 |
+ |
* |
168 |
+ |
* @throws IOException if something goes wrong |
169 |
+ |
*/ |
170 |
+ |
private void init() throws IOException { |
171 |
+ |
String temp = _socketIn.readLine(); |
172 |
+ |
// skip over any 220- lines |
173 |
+ |
while(temp != null && temp.startsWith("220-")) { |
174 |
+ |
temp = _socketIn.readLine(); |
175 |
+ |
} |
176 |
+ |
// check if we got a 220 welcome header |
177 |
+ |
if(temp == null || !temp.startsWith("220 ")) { |
178 |
+ |
throw new IOException ("Server did not initialise with a 220 message"); |
179 |
+ |
} |
180 |
+ |
// server gave a 220, it's ok to do a HELO |
181 |
+ |
sendCommand("HELO " + InetAddress.getLocalHost().getHostName(), 250); |
182 |
+ |
} |
183 |
|
|
184 |
|
//---ACCESSOR/MUTATOR METHODS--- |
185 |
|
|