1 |
/* |
/* |
2 |
* $Id$ |
* ComsatClient.java |
3 |
* Copyright (C) 2004 The Free Software Foundation |
* Copyright (C) 2004 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
58 |
|
|
59 |
protected DatagramSocket socket; |
protected DatagramSocket socket; |
60 |
|
|
61 |
public ComsatClient () |
public ComsatClient() |
62 |
throws IOException |
throws IOException |
63 |
{ |
{ |
64 |
this (DEFAULT_PORT, 0); |
this(DEFAULT_PORT, 0); |
65 |
} |
} |
66 |
|
|
67 |
public ComsatClient (int port, int timeout) |
public ComsatClient(int port, int timeout) |
68 |
throws IOException |
throws IOException |
69 |
{ |
{ |
70 |
if (port < 0) |
if (port < 0) |
72 |
port = DEFAULT_PORT; |
port = DEFAULT_PORT; |
73 |
} |
} |
74 |
|
|
75 |
socket = new DatagramSocket (port); |
socket = new DatagramSocket(port); |
76 |
if (timeout > 0) |
if (timeout > 0) |
77 |
{ |
{ |
78 |
socket.setSoTimeout (timeout); |
socket.setSoTimeout(timeout); |
79 |
} |
} |
80 |
socket.setReceiveBufferSize (1024); |
socket.setReceiveBufferSize(1024); |
81 |
} |
} |
82 |
|
|
83 |
public void close () |
public void close() |
84 |
throws IOException |
throws IOException |
85 |
{ |
{ |
86 |
socket.close (); |
socket.close(); |
87 |
} |
} |
88 |
|
|
89 |
public ComsatInfo read () |
public ComsatInfo read() |
90 |
throws IOException |
throws IOException |
91 |
{ |
{ |
92 |
byte[] buf = new byte[socket.getReceiveBufferSize ()]; |
byte[] buf = new byte[socket.getReceiveBufferSize()]; |
93 |
int len = buf.length; |
int len = buf.length; |
94 |
DatagramPacket packet = new DatagramPacket (buf, len); |
DatagramPacket packet = new DatagramPacket(buf, len); |
95 |
socket.receive (packet); |
socket.receive(packet); |
96 |
buf = packet.getData (); |
buf = packet.getData(); |
97 |
len = packet.getLength (); |
len = packet.getLength(); |
98 |
String data = new String (buf, 0, len, "ISO-8859-1"); |
String data = new String(buf, 0, len, "ISO-8859-1"); |
99 |
|
|
100 |
ComsatInfo info = new ComsatInfo (); |
ComsatInfo info = new ComsatInfo(); |
101 |
StringTokenizer st = new StringTokenizer (data, "\n"); |
StringTokenizer st = new StringTokenizer(data, "\n"); |
102 |
String mailbox = st.nextToken (); |
String mailbox = st.nextToken(); |
103 |
info.setMailbox (mailbox); |
info.setMailbox(mailbox); |
104 |
boolean inBody = false; |
boolean inBody = false; |
105 |
String lastHeader = null; |
String lastHeader = null; |
106 |
while (st.hasMoreTokens ()) |
while (st.hasMoreTokens()) |
107 |
{ |
{ |
108 |
String line = st.nextToken (); |
String line = st.nextToken(); |
109 |
if (inBody) |
if (inBody) |
110 |
{ |
{ |
111 |
String body = info.getBody (); |
String body = info.getBody(); |
112 |
if (body == null) |
if (body == null) |
113 |
{ |
{ |
114 |
body = line; |
body = line; |
117 |
{ |
{ |
118 |
body += "\n" + line; |
body += "\n" + line; |
119 |
} |
} |
120 |
info.setBody (body); |
info.setBody(body); |
121 |
} |
} |
122 |
else |
else |
123 |
{ |
{ |
124 |
if (line.length () == 0) |
if (line.length() == 0) |
125 |
{ |
{ |
126 |
inBody = true; |
inBody = true; |
127 |
} |
} |
128 |
else |
else |
129 |
{ |
{ |
130 |
int ci = line.indexOf (':'); |
int ci = line.indexOf(':'); |
131 |
if (ci != -1) |
if (ci != -1) |
132 |
{ |
{ |
133 |
lastHeader = line.substring (0, ci); |
lastHeader = line.substring(0, ci); |
134 |
info.setHeader (lastHeader, |
info.setHeader(lastHeader, |
135 |
line.substring (ci + 1).trim ()); |
line.substring(ci + 1).trim()); |
136 |
} |
} |
137 |
else |
else |
138 |
{ |
{ |
139 |
String val = info.getHeader (lastHeader); |
String val = info.getHeader(lastHeader); |
140 |
val += "\n" + line; |
val += "\n" + line; |
141 |
info.setHeader (lastHeader, val); |
info.setHeader(lastHeader, val); |
142 |
} |
} |
143 |
} |
} |
144 |
} |
} |
147 |
} |
} |
148 |
|
|
149 |
} |
} |
150 |
|
|