View Javadoc
1   package gov.usgs.volcanoes.winston.legacyServer.cmd;
2   
3   import java.nio.ByteBuffer;
4   import java.nio.channels.SocketChannel;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.logging.Level;
8   
9   import gov.usgs.net.NetTools;
10  import gov.usgs.volcanoes.core.CodeTimer;
11  import gov.usgs.volcanoes.core.time.J2kSec;
12  import gov.usgs.volcanoes.core.time.Time;
13  import gov.usgs.volcanoes.winston.db.WinstonDatabase;
14  import gov.usgs.volcanoes.winston.legacyServer.WWS;
15  
16  /**
17   * Winston implementation of earthworm wave_serverV GETSCNLRAW command
18   *
19   * @author Dan Cervelli
20   */
21  public class GetSCNLRawCommand extends BaseCommand {
22    public GetSCNLRawCommand(final NetTools nt, final WinstonDatabase db, final WWS wws) {
23      super(nt, db, wws);
24    }
25  
26    public void doCommand(final Object info, final SocketChannel channel) {
27  
28      final CodeTimer ct = new CodeTimer("GetSCNLRaw");
29      final String cmd = (String) info;
30  
31      final String[] ss = cmd.split(" ");
32      if (ss.length < 8)
33        return; // malformed command
34  
35      final String id = ss[1];
36      final String s = ss[2];
37      final String c = ss[3];
38      final String n = ss[4];
39      final String l = ss[5];
40      double t1 = Double.NaN;
41      double t2 = Double.NaN;
42      try {
43        t1 = Time.ewToj2k(Double.parseDouble(ss[6]));
44        t1 = timeOrMaxDays(t1);
45  
46        t2 = Time.ewToj2k(Double.parseDouble(ss[7]));
47        t2 = timeOrMaxDays(t2);
48      } catch (final Exception e) {
49      }
50  
51      if (id == null || s == null || c == null || n == null || Double.isNaN(t1) || Double.isNaN(t2))
52        return; // malformed command
53  
54      final int sid = emulator.getChannelID(s, c, n, l);
55      if (sid == -1) {
56        sendNoChannelResponse(id, 0, s, c, n, l, channel);
57        return;
58      }
59  
60      final double[] bounds = checkTimes(sid, t1, t2);
61      if (!allowTransaction(bounds)) {
62        final String error =
63            id + " " + sid + " " + s + " " + c + " " + n + " " + l + " " + getError(bounds) + "\n";
64        netTools.writeString(error, channel);
65        return;
66      }
67  
68      final Object[] result = emulator.getWaveServerRaw(s, c, n, l, t1, t2);
69  
70      ct.stop();
71      if (wws.getSlowCommandTime() > 0 && ct.getRunTimeMillis() > wws.getSlowCommandTime() * .75)
72        wws.log(Level.INFO,
73            String.format(
74                "slow db query (%1.2f ms) GETSCNLRAW " + s + "$" + c + "$" + n + "$" + l + " " + t1
75                    + " -> " + t2 + " (" + decimalFormat.format(t2 - t1) + ") ",
76                ct.getRunTimeMillis()),
77            channel);
78  
79      int totalBytes = 0;
80      if (result != null) {
81        final String hdr = id + " " + (String) result[0] + "\n";
82        final int bytes = ((Integer) result[1]).intValue();
83        final List<?> items = (List<?>) result[2];
84        final ByteBuffer bb = ByteBuffer.allocate(bytes);
85        for (final Iterator<?> it = items.iterator(); it.hasNext();) {
86          bb.put((byte[]) it.next());
87        }
88        bb.flip();
89  
90        ct.start();
91        netTools.writeString(hdr, channel);
92        totalBytes = netTools.writeByteBuffer(bb, channel);
93        ct.stop();
94        if (wws.getSlowCommandTime() > 0 && ct.getRunTimeMillis() > wws.getSlowCommandTime() * .75)
95          wws.log(Level.INFO,
96              String.format(
97                  "slow network (%1.2f ms) GETSCNLRAW " + s + "$" + c + "$" + n + "$" + l + " " + t1
98                      + " -> " + t2 + " (" + decimalFormat.format(t2 - t1) + ") ",
99                  ct.getRunTimeMillis()),
100             channel);
101     } else {
102       // must be a gap
103       netTools.writeString(id + " " + sid + " " + s + " " + c + " " + n + " " + l + " FG s4\n",
104           channel);
105     }
106 
107     final String scnl = s + "_" + c + "_" + n + "_" + l;
108     final String time = J2kSec.toDateString(t1) + " - " + J2kSec.toDateString(t2);
109     wws.log(Level.FINER, "GETSCNLRAW " + scnl + " : " + time + ", " + totalBytes + " bytes.",
110         channel);
111   }
112 }