]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/email/TestEmailProvider.java
UPD: added functional mail add
[gigi.git] / src / org / cacert / gigi / email / TestEmailProvider.java
index 9f37be97f3966bcb7091c1fa328eae5446de830f..35c4b3fe81351af1aaf6df7a44a983899a92ee30 100644 (file)
@@ -13,26 +13,21 @@ class TestEmailProvider extends EmailProvider {
        Socket client;
        DataOutputStream out;
        DataInputStream in;
+
        protected TestEmailProvider(Properties props) {
                try {
-                       servs = new ServerSocket(Integer.parseInt(props
-                                       .getProperty("emailProvider.port")), 10,
-                                       InetAddress.getByName("127.0.0.1"));
+                       servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10,
+                               InetAddress.getByName("127.0.0.1"));
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
+
        @Override
-       public synchronized void sendmail(String to, String subject,
-                       String message, String from, String replyto, String toname,
-                       String fromname, String errorsto, boolean extra) throws IOException {
-               boolean sent = false;
-               while (!sent) {
-                       if (client == null || client.isClosed()) {
-                               client = servs.accept();
-                               out = new DataOutputStream(client.getOutputStream());
-                               in = new DataInputStream(client.getInputStream());
-                       }
+       public synchronized void sendmail(String to, String subject, String message, String from, String replyto,
+               String toname, String fromname, String errorsto, boolean extra) throws IOException {
+               while (true) {
+                       assureLocalConnection();
                        try {
                                out.writeUTF("mail");
                                write(to);
@@ -41,18 +36,40 @@ class TestEmailProvider extends EmailProvider {
                                write(from);
                                write(replyto);
                                out.flush();
-                               sent = true;
+                               return;
                        } catch (IOException e) {
                                client = null;
                        }
                }
        }
+
+       private void assureLocalConnection() throws IOException {
+               if (out != null) {
+                       try {
+                               out.writeUTF("ping");
+                       } catch (IOException e) {
+                               client = null;
+                       }
+               }
+               if (client == null || client.isClosed()) {
+                       client = servs.accept();
+                       out = new DataOutputStream(client.getOutputStream());
+                       in = new DataInputStream(client.getInputStream());
+               }
+       }
+
        @Override
-       public String checkEmailServer(int forUid, String address)
-                       throws IOException {
-               out.writeUTF("challengeAddrBox");
-               out.writeUTF(address);
-               return in.readUTF();
+       public synchronized String checkEmailServer(int forUid, String address) throws IOException {
+               while (true) {
+                       assureLocalConnection();
+                       try {
+                               out.writeUTF("challengeAddrBox");
+                               out.writeUTF(address);
+                               return in.readUTF();
+                       } catch (IOException e) {
+                               client = null;
+                       }
+               }
        }
 
        private void write(String to) throws IOException {