View Javadoc
1   package gov.usgs.volcanoes.winston.client;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.IOException;
5   import java.nio.ByteBuffer;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import gov.usgs.plot.data.HelicorderData;
11  import gov.usgs.plot.data.Wave;
12  import gov.usgs.volcanoes.core.Zip;
13  import io.netty.buffer.ByteBuf;
14  
15  /**
16   * Receive and process response from a winston GETWAVE request.
17   *
18   * @author Tom Parker
19   */
20  public class GetScnlHeliRawHandler extends WWSCommandHandler {
21  	private static final Logger LOGGER = LoggerFactory.getLogger(GetScnlHeliRawHandler.class);
22  
23  	private final HelicorderData heliData;
24  	private int length;
25  	private final boolean isCompressed;
26  	private ByteArrayOutputStream buf;
27  
28  	public GetScnlHeliRawHandler(HelicorderData heliData, boolean isCompressed) {
29  		this.heliData = heliData;
30  		this.isCompressed = isCompressed;
31  		length = -Integer.MAX_VALUE;
32  		buf = null;
33  	}
34  
35  	@Override
36  	public void handle(Object msg) throws IOException {
37  		ByteBuf msgBuf = (ByteBuf) msg;
38  		if (length < 0) {
39  			String header = ClientUtils.readResponseHeader(msgBuf);
40  			if (header == null) {
41  				LOGGER.debug("Still waiting for full response line.");
42  				return;
43  			} else {
44  				String[] parts = header.split(" ");
45  				length = Integer.parseInt(parts[1]);
46  				buf = new ByteArrayOutputStream(length);
47  				LOGGER.debug("Response length: {}", length);
48  				LOGGER.debug("" + buf);
49  			}
50  		}
51  
52  		msgBuf.readBytes(buf, msgBuf.readableBytes());
53  		if (buf.size() == length) {
54  			LOGGER.debug("Received all bytes.");
55  			byte[] bytes = buf.toByteArray();
56  			if (isCompressed) {
57  				bytes = Zip.decompress(bytes);
58  			}
59  			heliData.fromBinary(ByteBuffer.wrap(bytes));
60  			sem.release();
61  		} else {
62  			LOGGER.debug("Still waiting for bytes. {}/{}", buf.size(), length);
63  		}
64  
65  	}
66  
67  }