Changeset 904
- Timestamp:
- 10/17/06 19:48:24 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/source/org/kolaka/freecast/player/HttpPlayer.java
r405 r904 31 31 import java.net.Socket; 32 32 33 import org.apache.commons.io.CopyUtils;34 33 import org.apache.commons.io.IOUtils; 35 34 import org.apache.commons.lang.Validate; … … 51 50 public class HttpPlayer extends BaseService implements Player, TimerUser { 52 51 53 private Socket socket;52 private Socket socket; 54 53 55 private Consumer consumer;54 private Consumer consumer; 56 55 57 private boolean stopped;56 private boolean stopped; 58 57 59 private ConsumerInputStreamFactory inputFactory;58 private ConsumerInputStreamFactory inputFactory; 60 59 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 } 65 64 66 private Task sending = new Task() {65 private Task sending = new Task() { 67 66 68 public void run() {69 OutputStream output = null;67 public void run() { 68 OutputStream output = null; 70 69 71 try {72 ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream();70 try { 71 ByteArrayOutputStream lineBuffer = new ByteArrayOutputStream(); 73 72 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 } 79 78 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(); 84 82 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 } 95 92 96 // socket.setSendBufferSize(1024);97 output = socket.getOutputStream();93 // socket.setSendBufferSize(1024); 94 output = socket.getOutputStream(); 98 95 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"; 101 97 102 output.write(reply.getBytes());103 output.flush();98 output.write(reply.getBytes()); 99 output.flush(); 104 100 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 } 120 116 121 };117 }; 122 118 123 protected void stopImpl() {124 stopped = true;119 protected void stopImpl() { 120 stopped = true; 125 121 126 LogFactory.getLog(getClass()).debug("dispose http player resources");122 LogFactory.getLog(getClass()).debug("dispose http player resources"); 127 123 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 } 134 129 135 if (inputFactory != null) {136 inputFactory.close();137 }130 if (inputFactory != null) { 131 inputFactory.close(); 132 } 138 133 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 } 146 140 147 public void start() throws ControlException {148 stopped = false;141 public void start() throws ControlException { 142 stopped = false; 149 143 150 inputFactory = new ConsumerInputStreamFactory(consumer);144 inputFactory = new ConsumerInputStreamFactory(consumer); 151 145 152 timer.executeLater(sending);146 timer.executeLater(sending); 153 147 154 super.start();155 }148 super.start(); 149 } 156 150 157 public void stop() throws ControlException {158 sending.cancel();159 stopImpl();160 }151 public void stop() throws ControlException { 152 sending.cancel(); 153 stopImpl(); 154 } 161 155 162 public void init() throws ControlException {163 super.init();164 }156 public void init() throws ControlException { 157 super.init(); 158 } 165 159 166 public void dispose() throws ControlException {167 super.dispose();168 }160 public void dispose() throws ControlException { 161 super.dispose(); 162 } 169 163 170 public PlayerStatus getPlayerStatus() {171 return PlayerStatus.INACTIVE;172 }164 public PlayerStatus getPlayerStatus() { 165 return PlayerStatus.INACTIVE; 166 } 173 167 174 public void setConsumer(Consumer consumer) {175 this.consumer = consumer;176 }168 public void setConsumer(Consumer consumer) { 169 this.consumer = consumer; 170 } 177 171 178 private Timer timer = DefaultTimer.getInstance();172 private Timer timer = DefaultTimer.getInstance(); 179 173 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 } 184 178 185 179 }
