Log exactly which part of the connection has died.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / Connection.java
1 /*
2  * Sonitus - Connection.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data;
19
20 import java.io.IOException;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 /**
25  * A connection reads bytes from a {@link Source} and feeds it to a sink. This
26  * class is meant to be subclassed by each {@link Sink}, overriding the {@link
27  * #feed(byte[])} method to actually feed the data into the sink. The {@link
28  * #feed(byte[])} method is also responsible for blocking for an appropriate
29  * amount of time; this method determines how fast a {@link Source} is
30  * consumed.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public abstract class Connection implements Runnable {
35
36         /** The logger. */
37         private static final Logger logger = Logger.getLogger(Connection.class.getName());
38
39         /** The source to consume. */
40         private final Source source;
41
42         /**
43          * Creates a new connection that will read from the given source.
44          *
45          * @param source
46          *              The source to read
47          */
48         public Connection(Source source) {
49                 this.source = source;
50         }
51
52         //
53         // RUNNABLE METHODS
54         //
55
56         @Override
57         public void run() {
58                 while (true) {
59                         byte[] buffer = null;
60                         try {
61                                 buffer = source.get(bufferSize());
62                         } catch (IOException ioe1) {
63                                 logger.log(Level.WARNING, "Source died!", ioe1);
64                                 break;
65                         }
66                         try {
67                                 feed(buffer);
68                         } catch (IOException ioe1) {
69                                 logger.log(Level.WARNING, "Sink died!", ioe1);
70                                 break;
71                         }
72                 }
73                 try {
74                         logger.info("Connection finished.");
75                         finish();
76                 } catch (IOException ioe1) {
77                         /* well, what can we do? nothing. */
78                 }
79         }
80
81         //
82         // SUBCLASS METHODS
83         //
84
85         /**
86          * Returns the number of bytes that will be requested from the source.
87          *
88          * @return The number of bytes that will be requested from the source
89          */
90         protected abstract int bufferSize();
91
92         /**
93          * Feeds the read data into the sink. The given buffer is always filled and
94          * never contains excess elements.
95          *
96          * @param buffer
97          *              The data
98          * @throws IOException
99          *              if an I/O error occurs
100          */
101         protected abstract void feed(byte[] buffer) throws IOException;
102
103         /**
104          * Notifies the sink that the source does not deliver any more data.
105          *
106          * @throws IOException
107          *              if an I/O error occurs
108          */
109         protected abstract void finish() throws IOException;
110
111 }