KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > receive > Receiver


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
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 org.jsmtpd.core.receive;
22
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.net.InetSocketAddress JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.Socket JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.jsmtpd.generic.threadpool.BusyThreadPoolException;
33 import org.jsmtpd.generic.threadpool.GrowingThreadPool;
34 import org.jsmtpd.generic.threadpool.ThreadPool;
35
36 /**
37  * Listening to a given port<br>
38  * When a connection occurs, picks a waiting thread from ThreadPool
39  * to handle dialog via an instance of ProtoclHandler
40  * @author Jean-Francois POUX
41  */

42 public class Receiver extends Thread JavaDoc {
43
44     private ServerSocket JavaDoc sock = null;
45     private ThreadPool p = null;
46     public boolean runing = true;
47     private Log log = LogFactory.getLog(Receiver.class);
48     private Socket JavaDoc inc = null;
49
50     public Receiver(int port, int maxInst) throws IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc, ClassNotFoundException JavaDoc {
51         p = new GrowingThreadPool(maxInst, "org.jsmtpd.core.receive.ReceiverWorkerImpl","R");
52         sock = new ServerSocket JavaDoc(port);
53         log.info("Listening for connections on port " + port);
54         this.start();
55     }
56
57     public void run() {
58         try {
59             while ((inc = sock.accept()) != null) {
60                 try {
61                     p.assignFreeThread(inc);
62                 } catch (BusyThreadPoolException e1) {
63                     handleBusy(inc);
64                 }
65             }
66             sock.close();
67         } catch (IOException JavaDoc e) {
68         }
69         log.info("Server is no longer listening for incoming connections");
70     }
71
72     private void handleBusy(Socket JavaDoc inc) {
73         String JavaDoc remote = ((InetSocketAddress JavaDoc) inc.getRemoteSocketAddress()).getAddress().getHostAddress();
74         BufferedWriter JavaDoc wr = null;
75         try {
76             log.error("Unable to serve " + remote + ", replied 421 Service unavaible due to heavy load");
77             wr = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(inc.getOutputStream()));
78             wr.write("421 Service unavaible due to heavy load");
79             wr.flush();
80
81         } catch (IOException JavaDoc e) {
82             log.error(e);
83         }
84         try {
85             wr.close();
86         } catch (IOException JavaDoc e1) {
87             log.error(e1);
88         }
89     }
90
91     public void shutdown() {
92         try {
93             sock.close();
94         } catch (IOException JavaDoc e) {
95         }
96         p.forceShutdown();
97     }
98 }
Popular Tags