Changeset 904

Show
Ignore:
Timestamp:
10/17/06 19:48:24 (2 years ago)
Author:
alban
Message:

don't log in info the exception when http clients disconnect

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/source/org/kolaka/freecast/player/HttpPlayer.java

    r405 r904  
    3131import java.net.Socket; 
    3232 
    33 import org.apache.commons.io.CopyUtils; 
    3433import org.apache.commons.io.IOUtils; 
    3534import org.apache.commons.lang.Validate; 
     
    5150public class HttpPlayer extends BaseService implements Player, TimerUser { 
    5251 
    53        private Socket socket; 
     52  private Socket socket; 
    5453 
    55        private Consumer consumer; 
     54  private Consumer consumer; 
    5655 
    57        private boolean stopped; 
     56  private boolean stopped; 
    5857 
    59        private ConsumerInputStreamFactory inputFactory; 
     58  private ConsumerInputStreamFactory inputFactory; 
    6059 
    61        public HttpPlayer(Socket socket) { 
    62                Validate.notNull(socket, "No specified socket"); 
    63                this.socket = socket; 
    64        
     60  public HttpPlayer(Socket socket) { 
     61    Validate.notNull(socket, "No specified socket"); 
     62    this.socket = socket; 
     63 
    6564 
    66        private Task sending = new Task() { 
     65  private Task sending = new Task() { 
    6766 
    68                public void run() { 
    69                        OutputStream output = null; 
     67    public void run() { 
     68      OutputStream output = null; 
    7069 
    71                        try { 
    72                                ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream(); 
     70      try { 
     71        ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream(); 
    7372 
    74                                while (true) { 
    75                                        int read = socket.getInputStream().read(); 
    76                                        if (read == -1) { 
    77                                                throw new EOFException("incomplete header"); 
    78                                        
     73        while (true) { 
     74          int read = socket.getInputStream().read(); 
     75          if (read == -1) { 
     76            throw new EOFException("incomplete header"); 
     77         
    7978 
    80                                         if (read == '\n') { 
    81                                                 String headerLine = new String(lineBuffer.toByteArray()) 
    82                                                                 .trim(); 
    83                                                 lineBuffer.reset(); 
     79          if (read == '\n') { 
     80            String headerLine = new String(lineBuffer.toByteArray()).trim(); 
     81            lineBuffer.reset(); 
    8482 
    85                                                 if (headerLine.length() == 0) { 
    86                                                         LogFactory.getLog(getClass()).trace("header ended"); 
    87                                                         break; 
    88                                                 } 
    89                                                 LogFactory.getLog(getClass()).trace( 
    90                                                                 "received header: " + headerLine); 
    91                                         } else { 
    92                                                 lineBuffer.write(read); 
    93                                         } 
    94                                 } 
     83            if (headerLine.length() == 0) { 
     84              LogFactory.getLog(getClass()).trace("header ended"); 
     85              break; 
     86            } 
     87            LogFactory.getLog(getClass()).trace("received header: " + headerLine); 
     88          } else { 
     89            lineBuffer.write(read); 
     90          } 
     91        } 
    9592 
    96                                // socket.setSendBufferSize(1024); 
    97                                output = socket.getOutputStream(); 
     93        // socket.setSendBufferSize(1024); 
     94        output = socket.getOutputStream(); 
    9895 
    99                                 String reply = "HTTP/1.0 200 OK\r\n" 
    100                                                 + "Content-Type: application/ogg\r\nice-name: FreeCast\r\n\r\n"; 
     96        String reply = "HTTP/1.0 200 OK\r\n" + "Content-Type: application/ogg\r\nice-name: FreeCast\r\n\r\n"; 
    10197 
    102                                output.write(reply.getBytes()); 
    103                                output.flush(); 
     98        output.write(reply.getBytes()); 
     99        output.flush(); 
    104100 
    105                                while (!stopped) { 
    106                                        InputStream input = inputFactory.next(); 
    107                                        CopyUtils.copy(input, output); 
    108                                        IOUtils.closeQuietly(input); 
    109                                
    110                        } catch (IOException e) { 
    111                                 LogFactory.getLog(getClass()).info( 
    112                                                "connection with http player ended", e); 
    113                         } catch (Exception e) { 
    114                                 LogFactory.getLog(getClass()).error( 
    115                                                "error in the http player connection", e); 
    116                        } finally { 
    117                                stopImpl(); 
    118                        
    119                
     101        while (!stopped) { 
     102          InputStream input = inputFactory.next(); 
     103          IOUtils.copy(input, output); 
     104          IOUtils.closeQuietly(input); 
     105       
     106      } catch (IOException e) { 
     107        String message = "connection with http player ended"; 
     108        LogFactory.getLog(getClass()).info(message); 
     109        LogFactory.getLog(getClass()).debug(message, e); 
     110      } catch (Exception e) { 
     111        LogFactory.getLog(getClass()).error("error in the http player connection", e); 
     112      } finally { 
     113        stopImpl(); 
     114     
     115   
    120116 
    121        }; 
     117  }; 
    122118 
    123        protected void stopImpl() { 
    124                stopped = true; 
     119  protected void stopImpl() { 
     120    stopped = true; 
    125121 
    126                LogFactory.getLog(getClass()).debug("dispose http player resources"); 
     122    LogFactory.getLog(getClass()).debug("dispose http player resources"); 
    127123 
    128                 try { 
    129                         socket.close(); 
    130                 } catch (IOException e) { 
    131                         LogFactory.getLog(getClass()).error( 
    132                                         "can't close the http player socket", e); 
    133                 } 
     124    try { 
     125      socket.close(); 
     126    } catch (IOException e) { 
     127      LogFactory.getLog(getClass()).error("can't close the http player socket", e); 
     128    } 
    134129 
    135                if (inputFactory != null) { 
    136                        inputFactory.close(); 
    137                
     130    if (inputFactory != null) { 
     131      inputFactory.close(); 
     132   
    138133 
    139                 try { 
    140                         super.stop(); 
    141                 } catch (ControlException e) { 
    142                         LogFactory.getLog(getClass()).error("can't stop this http player", 
    143                                         e); 
    144                 } 
    145         } 
     134    try { 
     135      super.stop(); 
     136    } catch (ControlException e) { 
     137      LogFactory.getLog(getClass()).error("can't stop this http player", e); 
     138    } 
     139  } 
    146140 
    147        public void start() throws ControlException { 
    148                stopped = false; 
     141  public void start() throws ControlException { 
     142    stopped = false; 
    149143 
    150                inputFactory = new ConsumerInputStreamFactory(consumer); 
     144    inputFactory = new ConsumerInputStreamFactory(consumer); 
    151145 
    152                timer.executeLater(sending); 
     146    timer.executeLater(sending); 
    153147 
    154                super.start(); 
    155        
     148    super.start(); 
     149 
    156150 
    157        public void stop() throws ControlException { 
    158                sending.cancel(); 
    159                stopImpl(); 
    160        
     151  public void stop() throws ControlException { 
     152    sending.cancel(); 
     153    stopImpl(); 
     154 
    161155 
    162        public void init() throws ControlException { 
    163                super.init(); 
    164        
     156  public void init() throws ControlException { 
     157    super.init(); 
     158 
    165159 
    166        public void dispose() throws ControlException { 
    167                super.dispose(); 
    168        
     160  public void dispose() throws ControlException { 
     161    super.dispose(); 
     162 
    169163 
    170        public PlayerStatus getPlayerStatus() { 
    171                return PlayerStatus.INACTIVE; 
    172        
     164  public PlayerStatus getPlayerStatus() { 
     165    return PlayerStatus.INACTIVE; 
     166 
    173167 
    174        public void setConsumer(Consumer consumer) { 
    175                this.consumer = consumer; 
    176        
     168  public void setConsumer(Consumer consumer) { 
     169    this.consumer = consumer; 
     170 
    177171 
    178        private Timer timer = DefaultTimer.getInstance(); 
     172  private Timer timer = DefaultTimer.getInstance(); 
    179173 
    180        public void setTimer(Timer timer) { 
    181                Validate.notNull(timer, "No specified Timer"); 
    182                this.timer = timer; 
    183        
     174  public void setTimer(Timer timer) { 
     175    Validate.notNull(timer, "No specified Timer"); 
     176    this.timer = timer; 
     177 
    184178 
    185179}