List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:edu.hawaii.soest.kilonalu.ctd.CTDSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *//*from ww w. ja v a2 s . c om*/ protected boolean execute() { logger.debug("CTDSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; // test the connection type if (this.connectionType.equals("serial")) { // create a serial connection to the local serial port this.channel = getSerialConnection(); } else if (this.connectionType.equals("socket")) { // otherwise create a TCP or UDP socket connection to the remote host this.channel = getSocketConnection(); } else { logger.info("There was an error establishing either a serial or " + "socket connection to the instrument. Please be sure " + "the connection type is set to either 'serial' or 'socket'."); return false; } // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // Declare sample variables to be used in the response parsing byte[] sampleArray; // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); // while there are bytes to read from the channel ... while (this.channel.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); logger.debug("b1: " + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: " + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: " + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: " + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: " + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t" + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t" + "buffer rem: " + buffer.remaining() + "\t" + "state: " + this.state); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (this.state) { case 0: // wake up the instrument // check for instrument metadata fields if (this.enableSendCommands && !this.hasMetadata) { // wake the instrument with an initial '\r\n' command this.command = this.commandSuffix; this.sentCommand = queryInstrument(this.command); this.sentCommand = queryInstrument(this.command); streamingThread.sleep(2000); this.state = 1; break; } else { this.state = 11; break; } case 1: // stop the sampling // be sure the instrument woke (look for S> prompt) //if (byteOne == 0x3E && byteTwo == 0x53 ) { // // sampleByteCount = 0; // sampleBuffer.clear(); // // // send the stop sampling command this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); sampleBuffer.clear(); sampleByteCount = 0; this.state = 2; break; //} else { // // handle instrument hardware response // sampleByteCount++; // add the last byte found to the count // // // add the last byte found to the sample buffer // if ( sampleBuffer.remaining() > 0 ) { // sampleBuffer.put(byteOne); // // } else { // sampleBuffer.compact(); // sampleBuffer.put(byteOne); // // } // // break; // continue reading bytes // //} case 2: // based on outputType, get metadata from the instrument // the response should end in <Executed/> if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { sampleBuffer.clear(); sampleByteCount = 0; this.samplingIsStopped = true; // for newer firmware CTDs, use xml-based query commands if (getOutputType().equals("xml")) { // create the CTD parser instance used to parse CTD output this.ctdParser = new CTDParser(); this.state = 3; break; // otherwise, use text-based query commands } else if (getOutputType().equals("text")) { this.state = 12; // process DS and DCal commands break; } else { logger.info("The CTD output type is not recognized. " + "Please set the output type to either " + "'xml' or 'text'."); failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); return !failed; } } // disconnect from the RBNB if (isConnected()) { disconnect(); } return !failed; } } else { // handle instrument hardware response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } break; // continue reading bytes } case 3: // get the instrument status metadata if (!this.ctdParser.getHasStatusMetadata()) { this.command = this.commandPrefix + this.getStatusCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 4; break; } else { // get the configuration metadata this.command = this.commandPrefix + this.getConfigurationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 5; break; } case 4: // handle instrument status response // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument configuration metadata if (!this.ctdParser.getHasConfigurationMetadata()) { this.command = this.commandPrefix + this.getConfigurationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 5; break; } else { // get the calibration metadata this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 6; break; } } else { break; // continue reading bytes } case 5: // handle the instrument configuration metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument configration response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument calibration metadata if (!this.ctdParser.getHasCalibrationMetadata()) { this.command = this.commandPrefix + this.getCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 6; break; } else { this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 7; break; } } else { break; // continue reading bytes } case 6: // handle the instrument calibration metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument calibration response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument event metadata if (!this.ctdParser.getHasEventMetadata()) { this.command = this.commandPrefix + this.getEventsCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 7; break; } else { this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 8; break; } } else { break; // continue reading bytes } case 7: // handle instrument event metadata // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument events response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument hardware metadata if (!this.ctdParser.getHasHardwareMetadata()) { this.command = this.commandPrefix + this.getHardwareCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 8; break; } else { this.state = 9; break; } } else { break; // continue reading bytes } case 8: // handle the instrument hardware response // command response ends with <Executed/> (so find: ed/>) if (byteOne == 0x3E && byteTwo == 0x2F && byteThree == 0x64 && byteFour == 0x65) { // handle instrument hardware response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // set the CTD metadata int executedIndex = this.responseString.indexOf("<Executed/>"); this.responseString = this.responseString.substring(0, executedIndex - 1); this.ctdParser.setMetadata(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // sync the clock if it is not synced if (!this.clockIsSynced) { this.state = 9; break; } else { this.state = 10; break; } } else { break; // continue reading bytes } case 9: // set the instrument clock // is sampling stopped? if (!this.samplingIsStopped) { // wake the instrument with an initial '\r\n' command this.command = this.commandSuffix; this.sentCommand = queryInstrument(this.command); streamingThread.sleep(2000); // then stop the sampling this.command = this.commandPrefix + this.stopSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); this.samplingIsStopped = true; } // now set the clock if (this.sentCommand) { this.clockSyncDate = new Date(); DATE_FORMAT.setTimeZone(TZ); String dateAsString = DATE_FORMAT.format(this.clockSyncDate); this.command = this.commandPrefix + this.setDateTimeCommand + dateAsString + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.clockIsSynced = true; logger.info("The instrument clock has bee synced at " + this.clockSyncDate.toString()); this.state = 10; break; } else { break; // try the clock sync again due to failure } case 10: // restart the instrument sampling if (this.samplingIsStopped) { this.hasMetadata = true; this.command = this.commandPrefix + this.startSamplingCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); if (this.sentCommand) { this.state = 11; break; } else { break; // try starting the sampling again due to failure } } else { break; } case 11: // read bytes to the next EOL characters // sample line is terminated by \r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D) { sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // test if the sample is not just an instrument message if (this.responseString.matches("^# [0-9].*\r\n") || this.responseString.matches("^# [0-9].*\r\n") || this.responseString.matches("^ [0-9].*\r\n")) { // add the data observations string to the CTDParser object // and populate the CTDParser data fields //this.ctdParser.setData(this.responseString); //this.ctdParser.parse(); // build the channel map with all of the data and metadata channels: int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutTimeAuto("server"); // add the ASCII sample data field rbnbChannelMap.PutDataAsString(channelIndex, this.responseString); // add other metadata and data fields to the map if metadata was collected if (this.hasMetadata && this.ctdParser != null) { // add the samplingMode field data channelIndex = rbnbChannelMap.Add("samplingMode"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getSamplingMode()); // String // add the temperatureSerialNumber field data channelIndex = rbnbChannelMap.Add("temperatureSerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTemperatureSerialNumber()); // String // add the conductivitySerialNumber field data channelIndex = rbnbChannelMap.Add("conductivitySerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getConductivitySerialNumber()); // String // add the mainBatteryVoltage field data channelIndex = rbnbChannelMap.Add("mainBatteryVoltage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getMainBatteryVoltage() }); // double // add the lithiumBatteryVoltage field data channelIndex = rbnbChannelMap.Add("lithiumBatteryVoltage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getLithiumBatteryVoltage() }); // double // add the operatingCurrent field data channelIndex = rbnbChannelMap.Add("operatingCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getOperatingCurrent() }); // double // add the pumpCurrent field data channelIndex = rbnbChannelMap.Add("pumpCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPumpCurrent() }); // double // add the channels01ExternalCurrent field data channelIndex = rbnbChannelMap.Add("channels01ExternalCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getChannels01ExternalCurrent() }); // double // add the channels23ExternalCurrent field data channelIndex = rbnbChannelMap.Add("channels23ExternalCurrent"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getChannels23ExternalCurrent() }); // double // add the loggingStatus field data channelIndex = rbnbChannelMap.Add("loggingStatus"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getLoggingStatus()); // String // add the numberOfScansToAverage field data channelIndex = rbnbChannelMap.Add("numberOfScansToAverage"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfScansToAverage() }); // int // add the numberOfSamples field data channelIndex = rbnbChannelMap.Add("numberOfSamples"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfSamples() }); // int // add the numberOfAvailableSamples field data channelIndex = rbnbChannelMap.Add("numberOfAvailableSamples"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfAvailableSamples() }); // int // add the sampleInterval field data channelIndex = rbnbChannelMap.Add("sampleInterval"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getSampleInterval() }); // int // add the measurementsPerSample field data channelIndex = rbnbChannelMap.Add("measurementsPerSample"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getMeasurementsPerSample() }); // int // add the transmitRealtime field data channelIndex = rbnbChannelMap.Add("transmitRealtime"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTransmitRealtime()); // String // add the numberOfCasts field data channelIndex = rbnbChannelMap.Add("numberOfCasts"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getNumberOfCasts() }); // int // add the minimumConductivityFrequency field data channelIndex = rbnbChannelMap.Add("minimumConductivityFrequency"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getMinimumConductivityFrequency() }); // int // add the pumpDelay field data channelIndex = rbnbChannelMap.Add("pumpDelay"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsInt32(channelIndex, new int[] { this.ctdParser.getPumpDelay() }); // int // add the automaticLogging field data channelIndex = rbnbChannelMap.Add("automaticLogging"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getAutomaticLogging()); // String // add the ignoreMagneticSwitch field data channelIndex = rbnbChannelMap.Add("ignoreMagneticSwitch"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getIgnoreMagneticSwitch()); // String // add the batteryType field data channelIndex = rbnbChannelMap.Add("batteryType"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryType()); // String // add the batteryCutoff field data channelIndex = rbnbChannelMap.Add("batteryCutoff"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getBatteryCutoff()); // String // add the pressureSensorType field data channelIndex = rbnbChannelMap.Add("pressureSensorType"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSensorType()); // String // add the pressureSensorRange field data channelIndex = rbnbChannelMap.Add("pressureSensorRange"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSensorRange()); // String // add the sbe38TemperatureSensor field data channelIndex = rbnbChannelMap.Add("sbe38TemperatureSensor"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getSbe38TemperatureSensor()); // String // add the gasTensionDevice field data channelIndex = rbnbChannelMap.Add("gasTensionDevice"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getGasTensionDevice()); // String // add the externalVoltageChannelZero field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelZero"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelZero()); // String // add the externalVoltageChannelOne field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelOne"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelOne()); // String // add the externalVoltageChannelTwo field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelTwo"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelTwo()); // String // add the externalVoltageChannelThree field data channelIndex = rbnbChannelMap.Add("externalVoltageChannelThree"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getExternalVoltageChannelThree()); // String // add the echoCommands field data channelIndex = rbnbChannelMap.Add("echoCommands"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getEchoCommands()); // String // add the outputFormat field data channelIndex = rbnbChannelMap.Add("outputFormat"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getOutputFormat()); // String // add the temperatureCalibrationDate field data channelIndex = rbnbChannelMap.Add("temperatureCalibrationDate"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getTemperatureCalibrationDate()); // String // add the temperatureCoefficientTA0 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA0() }); // double // add the temperatureCoefficientTA1 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA1() }); // double // add the temperatureCoefficientTA2 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA2() }); // double // add the temperatureCoefficientTA3 field data channelIndex = rbnbChannelMap.Add("temperatureCoefficientTA3"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA3() }); // double // add the temperatureOffsetCoefficient field data channelIndex = rbnbChannelMap.Add("temperatureOffsetCoefficient"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getTemperatureOffsetCoefficient() }); // double // add the conductivityCalibrationDate field data channelIndex = rbnbChannelMap.Add("conductivityCalibrationDate"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getConductivityCalibrationDate()); // String // add the conductivityCoefficientG field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientG"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientG() }); // double // add the conductivityCoefficientH field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientH"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientH() }); // double // add the conductivityCoefficientI field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientI"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientI() }); // double // add the conductivityCoefficientJ field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientJ"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientJ() }); // double // add the conductivityCoefficientCF0 field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCF0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCF0() }); // double // add the conductivityCoefficientCPCOR field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCPCOR"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCPCOR() }); // double // add the conductivityCoefficientCTCOR field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCTCOR"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCTCOR() }); // double // add the conductivityCoefficientCSLOPE field data channelIndex = rbnbChannelMap.Add("conductivityCoefficientCSLOPE"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCSLOPE() }); // double // add the pressureSerialNumber field data channelIndex = rbnbChannelMap.Add("pressureSerialNumber"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, this.ctdParser.getPressureSerialNumber()); // String // add the pressureCoefficientPA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA0() }); // double // add the pressureCoefficientPA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA1() }); // double // add the pressureCoefficientPA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA2() }); // double // add the pressureCoefficientPTCA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA0() }); // double // add the pressureCoefficientPTCA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA1() }); // double // add the pressureCoefficientPTCA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA2() }); // double // add the pressureCoefficientPTCB0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB0() }); // double // add the pressureCoefficientPTCB1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB1() }); // double // add the pressureCoefficientPTCB2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTCB2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB2() }); // double // add the pressureCoefficientPTEMPA0 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA0"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA0() }); // double // add the pressureCoefficientPTEMPA1 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA1"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA1() }); // double // add the pressureCoefficientPTEMPA2 field data channelIndex = rbnbChannelMap.Add("pressureCoefficientPTEMPA2"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA2() }); // double // add the pressureOffsetCoefficient field data channelIndex = rbnbChannelMap.Add("pressureOffsetCoefficient"); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat64(channelIndex, new double[] { this.ctdParser.getPressureOffsetCoefficient() }); // double } // send the sample to the data turbine getSource().Flush(rbnbChannelMap); logger.info("Sent sample to the DataTurbine: " + this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; channelIndex = 0; rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); // check if the clock needs syncing (daily) if (this.enableSendCommands) { // get the current datetime Calendar currentCalendar = Calendar.getInstance(); currentCalendar.setTime(new Date()); Calendar lastSyncedCalendar = Calendar.getInstance(); lastSyncedCalendar.setTime(this.clockSyncDate); // round the dates to the day currentCalendar.clear(Calendar.MILLISECOND); currentCalendar.clear(Calendar.SECOND); currentCalendar.clear(Calendar.MINUTE); currentCalendar.clear(Calendar.HOUR); lastSyncedCalendar.clear(Calendar.MILLISECOND); lastSyncedCalendar.clear(Calendar.SECOND); lastSyncedCalendar.clear(Calendar.MINUTE); lastSyncedCalendar.clear(Calendar.HOUR); // sync the clock daily if (currentCalendar.before(lastSyncedCalendar)) { this.state = 8; } } // otherwise stay in state = 11 break; // the sample looks more like an instrument message, don't flush } else { logger.info("This string does not look like a sample, " + "and was not sent to the DataTurbine."); logger.info("Skipping sample: " + this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; //rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); this.state = 11; break; } } else { // not 0x0A0D // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0A0D EOL case 12: // alternatively use legacy DS and DCal commands if (this.enableSendCommands) { // start by getting the DS status output this.command = this.commandPrefix + this.displayStatusCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 13; break; } else { this.state = 0; break; } case 13: // handle the DS command response // command should end with the S> prompt if (byteOne == 0x7E && byteTwo == 0x53) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount - 2]; //subtract "S>" sampleBuffer.flip(); sampleBuffer.get(sampleArray); this.responseString = new String(sampleArray, "US-ASCII"); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; // then get the instrument calibration metadata this.command = this.commandPrefix + this.displayCalibrationCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); streamingThread.sleep(5000); this.state = 14; break; } else { break; // continue reading bytes } case 14: // handle the DCal command response // command should end with the S> prompt if (byteOne == 0x7E && byteTwo == 0x53) { // handle instrument status response sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract the sampleByteCount length from the sampleBuffer sampleArray = new byte[sampleByteCount - 2]; // subtract "S>" sampleBuffer.flip(); sampleBuffer.get(sampleArray); // append the DCal output to the DS output this.responseString = this.responseString.concat(new String(sampleArray, "US-ASCII")); // and add the data delimiter expected in the CTDParser this.responseString = this.responseString.concat("*END*\r\n\r\n"); // build the CTDParser object with legacy DS and DCal metadata this.ctdParser = new CTDParser(this.responseString); // reset variables for the next sample sampleBuffer.clear(); sampleByteCount = 0; this.state = 9; // set the clock and start sampling break; } else { break; // continue reading bytes } } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more channel bytes to read) this.channel.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } e.printStackTrace(); return !failed; } catch (InterruptedException intde) { // in the event that the streamingThread is interrupted failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } intde.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. //this.channel.close(); failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } sapie.printStackTrace(); return !failed; } catch (ParseException pe) { failed = true; this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } // disconnect from the RBNB if (isConnected()) { disconnect(); } logger.info("There was an error parsing the metadata response. " + "The error message was: " + pe.getMessage()); return !failed; } finally { this.state = 0; // close the serial or socket channel if (this.channel != null && this.channel.isOpen()) { try { this.channel.close(); } catch (IOException cioe) { logger.debug("An error occurred trying to close the byte channel. " + " The error message was: " + cioe.getMessage()); } } } return !failed; }
From source file:edu.hawaii.soest.kilonalu.tchain.TChainSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *///from w w w . j ava 2 s . com protected boolean execute() { logger.debug("TChainSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; SocketChannel socket = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); // while there are bytes to read from the socket ... while (socket.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); logger.debug("char: " + (char) byteOne + "\t" + "b1: " + new String(Hex.encodeHex((new byte[] { byteOne }))) + "\t" + "b2: " + new String(Hex.encodeHex((new byte[] { byteTwo }))) + "\t" + "b3: " + new String(Hex.encodeHex((new byte[] { byteThree }))) + "\t" + "b4: " + new String(Hex.encodeHex((new byte[] { byteFour }))) + "\t" + "sample pos: " + sampleBuffer.position() + "\t" + "sample rem: " + sampleBuffer.remaining() + "\t" + "sample cnt: " + sampleByteCount + "\t" + "buffer pos: " + buffer.position() + "\t" + "buffer rem: " + buffer.remaining() + "\t" + "state: " + state); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (state) { case 0: // sample line ending is '\r\n' (carraige return, newline) // note bytes are in reverse order in the FIFO window if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) { // we've found the end of a sample, move on state = 1; break; } else { break; } case 1: // read the rest of the bytes to the next EOL characters // sample line is terminated by record delimiter bytes (usually \r\n or \n) // note bytes are in reverse order in the FIFO window if (byteOne == this.firstDelimiterByte && byteTwo == this.secondDelimiterByte) { // rewind the sample to overwrite the line ending so we can add // in the timestamp (then add the line ending) sampleBuffer.position(sampleBuffer.position() - 1); --sampleByteCount; // add the delimiter to the end of the sample. byte[] delimiterAsBytes = getFieldDelimiter().getBytes("US-ASCII"); for (byte delim : delimiterAsBytes) { sampleBuffer.put(delim); sampleByteCount++; } // then add a timestamp to the end of the sample DATE_FORMAT.setTimeZone(TZ); byte[] sampleDateAsBytes = DATE_FORMAT.format(new Date()).getBytes("US-ASCII"); for (byte b : sampleDateAsBytes) { sampleBuffer.put(b); sampleByteCount++; } // add the last two bytes found (usually \r\n) to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); sampleByteCount++; sampleBuffer.put(byteTwo); sampleByteCount++; } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); sampleByteCount++; sampleBuffer.put(byteTwo); sampleByteCount++; } // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); // send the sample to the data turbine rbnbChannelMap.PutTimeAuto("server"); String sampleString = new String(sampleArray, "US-ASCII"); int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, sampleString); getSource().Flush(rbnbChannelMap); logger.info("Sample: " + sampleString.substring(0, sampleString.length() - 2) + " sent data to the DataTurbine. "); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; sampleBuffer.clear(); sampleByteCount = 0; rbnbChannelMap.Clear(); logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); //state = 0; } else { // not 0x0D20 // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0D20 EOL } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socket bytes to read) socket.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } return !failed; }
From source file:com.app.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;/*w w w . ja va 2s. c o m*/ ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //log.info(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //log.info("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //log.info("In read obect"); object = ois.readObject(); //log.info("Class Cast"); //log.info("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //log.info("readObject"); //log.info("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //log.info("classLoaderMap" // + urlClassLoaderMap); //log.info(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //log.info(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap.get(deployDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // log.info("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // log.info("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * log.info( * "current context classloader" + * classLoader); */ //log.info("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //log.info("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //log.info(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //log.info("bytesRead<length"); iterator.remove(); continue; } //log.info(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //log.info("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //log.info("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; Enumeration<NodeResourceInfo> noderesourceInfos = addressmap.elements(); NodeResourceInfo noderesourceinfo = null; String ip = ""; int port = 1000; long memavailable = 0; long memcurr = 0; if (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); ; } } while (noderesourceInfos.hasMoreElements()) { noderesourceinfo = noderesourceInfos.nextElement(); if (noderesourceinfo.getMax() != null) { memcurr = Long.parseLong(noderesourceinfo.getMax()) - Long.parseLong(noderesourceinfo.getUsed()); if (memavailable <= memcurr) { ip = noderesourceinfo.getHost(); port = Integer.parseInt(noderesourceinfo.getPort()); memavailable = memcurr; } } } ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket(ip, port); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //log.info("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // log.info("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //log.info("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //log.info("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //log.info(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //log.info("In write1"); numberOfServicesRequests++; //log.info("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); //ex.printStackTrace(); } } } catch (Exception ex) { log.error("Error in executing the executor services thread", ex); } }
From source file:com.web.services.ExecutorServiceThread.java
public void run() { // create a selector that will by used for multiplexing. The selector // registers the socketserverchannel as // well as all socketchannels that are created String CLIENTCHANNELNAME = "clientChannel"; String SERVERCHANNELNAME = "serverChannel"; String channelType = "channelType"; ConcurrentHashMap<SelectionKey, Object> resultMap = new ConcurrentHashMap<SelectionKey, Object>(); ClassLoader classLoader = null; ByteArrayOutputStream bstr;// w w w.j a v a2 s .c o m ByteBuffer buffer = null; ByteBuffer lengthBuffer = ByteBuffer.allocate(4); int bytesRead; InputStream bais; ObjectInputStream ois; Object object; ExecutorServiceInfo executorServiceInfo = null; Random random = new Random(System.currentTimeMillis()); // register the serversocketchannel with the selector. The OP_ACCEPT // option marks // a selection key as ready when the channel accepts a new connection. // When the // socket server accepts a connection this key is added to the list of // selected keys of the selector. // when asked for the selected keys, this key is returned and hence we // know that a new connection has been accepted. try { Selector selector = Selector.open(); SelectionKey socketServerSelectionKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); // SelectionKey socketServerSelectionKey1 = // channel.register(selector1, // SelectionKey.OP_ACCEPT); // set property in the key that identifies the channel Map<String, String> properties = new ConcurrentHashMap<String, String>(); properties.put(channelType, SERVERCHANNELNAME); socketServerSelectionKey.attach(properties); // wait for the selected keys SelectionKey key = null; Set<SelectionKey> selectedKeys; // logger.info("Instance Number"+instanceNumber); Iterator<SelectionKey> iterator = null; SocketChannel clientChannel = null; while (true) { try { // the select method is a blocking method which returns when // atleast // one of the registered // channel is selected. In this example, when the socket // accepts // a // new connection, this method // will return. Once a socketclient is added to the list of // registered channels, then this method // would also return when one of the clients has data to be // read // or // written. It is also possible to perform a nonblocking // select // using the selectNow() function. // We can also specify the maximum time for which a select // function // can be blocked using the select(long timeout) function. if (selector.select() >= 0) { selectedKeys = selector.selectedKeys(); iterator = selectedKeys.iterator(); } while (iterator.hasNext()) { try { key = iterator.next(); // the selection key could either by the // socketserver // informing // that a new connection has been made, or // a socket client that is ready for read/write // we use the properties object attached to the // channel // to // find // out the type of channel. if (((Map) key.attachment()).get(channelType).equals(SERVERCHANNELNAME)) { // a new connection has been obtained. This // channel // is // therefore a socket server. ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // accept the new connection on the server // socket. // Since // the // server socket channel is marked as non // blocking // this channel will return null if no client is // connected. SocketChannel clientSocketChannel = serverSocketChannel.accept(); if (clientSocketChannel != null) { // set the client connection to be non // blocking clientSocketChannel.configureBlocking(false); SelectionKey clientKey = clientSocketChannel.register(selector, SelectionKey.OP_READ, SelectionKey.OP_WRITE); Map<String, String> clientproperties = new ConcurrentHashMap<String, String>(); clientproperties.put(channelType, CLIENTCHANNELNAME); clientKey.attach(clientproperties); clientKey.interestOps(SelectionKey.OP_READ); // clientSocketChannel.close(); // write something to the new created client /* * CharBuffer buffer = * CharBuffer.wrap("Hello client"); while * (buffer.hasRemaining()) { * clientSocketChannel.write * (Charset.defaultCharset() * .encode(buffer)); } * clientSocketChannel.close(); * buffer.clear(); */ } } else { // data is available for read // buffer for reading clientChannel = (SocketChannel) key.channel(); if (key.isReadable()) { // the channel is non blocking so keep it // open // till // the // count is >=0 clientChannel = (SocketChannel) key.channel(); if (resultMap.get(key) == null) { //System.out.println(key); bstr = new ByteArrayOutputStream(); object = null; clientChannel.read(lengthBuffer); int length = lengthBuffer.getInt(0); lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } buffer.clear(); //System.out.println("Message1"+new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ois = new ObjectInputStream(bais); // Offending // line. // Produces // the // StreamCorruptedException. //System.out.println("In read obect"); object = ois.readObject(); //System.out.println("Class Cast"); //System.out.println("Class Cast1"); ois.close(); byte[] params = bstr.toByteArray(); bstr.close(); //System.out.println("readObject"); //System.out.println("After readObject"); if (object instanceof CloseSocket) { resultMap.remove(key); clientChannel.close(); key.cancel(); } // clientChannel.close(); String serviceurl = (String) object; String[] serviceRegistry = serviceurl.split("/"); //System.out.println("classLoaderMap" // + urlClassLoaderMap); //System.out.println(deployDirectory // + "/" + serviceRegistry[0]); int servicenameIndex; //System.out.println(earServicesDirectory // + "/" + serviceRegistry[0] // + "/" + serviceRegistry[1]); if (serviceRegistry[0].endsWith(".ear")) { classLoader = (VFSClassLoader) urlClassLoaderMap .get(earServicesDirectory + "/" + serviceRegistry[0] + "/" + serviceRegistry[1]); servicenameIndex = 2; } else if (serviceRegistry[0].endsWith(".jar")) { classLoader = (WebClassLoader) urlClassLoaderMap .get(jarservicesDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } else { classLoader = (WebClassLoader) urlClassLoaderMap .get(deployDirectory + "/" + serviceRegistry[0]); servicenameIndex = 1; } String serviceName = serviceRegistry[servicenameIndex]; // System.out.println("servicename:"+serviceName);; synchronized (executorServiceMap) { executorServiceInfo = (ExecutorServiceInfo) executorServiceMap .get(serviceName.trim()); } ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = new ExecutorServiceInfoClassLoader(); classLoaderExecutorServiceInfo.setClassLoader(classLoader); classLoaderExecutorServiceInfo.setExecutorServiceInfo(executorServiceInfo); resultMap.put(key, classLoaderExecutorServiceInfo); // key.interestOps(SelectionKey.OP_READ); // System.out.println("Key interested Ops"); // continue; } //Thread.sleep(100); /* * if (classLoader == null) throw new * Exception( * "Could able to obtain deployed class loader" * ); */ /* * System.out.println( * "current context classloader" + * classLoader); */ //System.out.println("In rad object"); bstr = new ByteArrayOutputStream(); lengthBuffer.clear(); int numberofDataRead = clientChannel.read(lengthBuffer); //System.out.println("numberofDataRead" // + numberofDataRead); int length = lengthBuffer.getInt(0); if (length <= 0) { iterator.remove(); continue; } lengthBuffer.clear(); //System.out.println(length); buffer = ByteBuffer.allocate(length); buffer.clear(); if ((bytesRead = clientChannel.read(buffer)) > 0) { // buffer.flip(); // System.out // .println(bytesRead); bstr.write(buffer.array(), 0, bytesRead); buffer.clear(); } if (bytesRead <= 0 || bytesRead < length) { //System.out.println("bytesRead<length"); iterator.remove(); continue; } //System.out.println(new String(bstr // .toByteArray())); bais = new ByteArrayInputStream(bstr.toByteArray()); ExecutorServiceInfoClassLoader classLoaderExecutorServiceInfo = (ExecutorServiceInfoClassLoader) resultMap .get(key); ois = new ClassLoaderObjectInputStream( (ClassLoader) classLoaderExecutorServiceInfo.getClassLoader(), bais); // Offending // line. // Produces // the // StreamCorruptedException. object = ois.readObject(); ois.close(); bstr.close(); executorServiceInfo = classLoaderExecutorServiceInfo.getExecutorServiceInfo(); //System.out // .println("inputStream Read Object"); //System.out.println("Object=" // + object.getClass()); // Thread.currentThread().setContextClassLoader(currentContextLoader); if (object instanceof ExecutorParams) { ExecutorParams exeParams = (ExecutorParams) object; Object returnValue = null; //System.out.println("test socket1"); String ataKey; ATAConfig ataConfig; ConcurrentHashMap ataServicesMap; ATAExecutorServiceInfo servicesAvailable; Socket sock1 = new Socket("0.0.0.0", Integer.parseInt(nodesport[random.nextInt(nodesport.length)])); OutputStream outputStr = sock1.getOutputStream(); ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStr); NodeInfo nodeInfo = new NodeInfo(); nodeInfo.setClassNameWithPackage( executorServiceInfo.getExecutorServicesClass().getName()); nodeInfo.setMethodName(executorServiceInfo.getMethod().getName()); nodeInfo.setWebclassLoaderURLS(((WebClassLoader) classLoader).geturlS()); NodeInfoMethodParam nodeInfoMethodParam = new NodeInfoMethodParam(); nodeInfoMethodParam.setMethodParams(exeParams.getParams()); nodeInfoMethodParam .setMethodParamTypes(executorServiceInfo.getMethodParams()); //System.out.println("Serializable socket="+sock); //nodeInfo.setSock(sock); //nodeInfo.setOstream(sock.getOutputStream()); objOutputStream.writeObject(nodeInfo); objOutputStream = new ObjectOutputStream(outputStr); objOutputStream.writeObject(nodeInfoMethodParam); ObjectInputStream objInputStream1 = new ObjectInputStream( sock1.getInputStream()); returnValue = objInputStream1.readObject(); objOutputStream.close(); objInputStream1.close(); sock1.close(); /*returnValue = executorServiceInfo .getMethod() .invoke(executorServiceInfo .getExecutorServicesClass() .newInstance(), exeParams.getParams());*/ // Thread.currentThread().setContextClassLoader(oldCL); // System.out.println("Written Value=" // + returnValue.toString()); resultMap.put(key, returnValue); } key.interestOps(SelectionKey.OP_WRITE); //System.out.println("Key interested Ops1"); } else if (key.isWritable()) { // the channel is non blocking so keep it // open // till the // count is >=0 //System.out.println("In write"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // make // a // BAOS // stream ObjectOutputStream oos = new ObjectOutputStream(baos); // wrap and OOS around the // stream Object result = resultMap.get(key); oos.writeObject(result); // write an object // to // the stream oos.flush(); oos.close(); byte[] objData = baos.toByteArray(); // get // the // byte // array baos.close(); buffer = ByteBuffer.wrap(objData); // wrap // around // the // data buffer.rewind(); // buffer.flip(); //prep for writing //System.out.println(new String(objData)); //while (buffer.hasRemaining()) clientChannel.write(buffer); // write resultMap.remove(key); buffer.clear(); key.cancel(); clientChannel.close(); //System.out.println("In write1"); numberOfServicesRequests++; //System.out.println("Key interested Ops2"); } } iterator.remove(); } catch (Exception ex) { ex.printStackTrace(); key.cancel(); clientChannel.close(); resultMap.remove(key); //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } } } catch (Exception ex) { //ex.printStackTrace(); } }
From source file:edu.hawaii.soest.kilonalu.adcp.ADCPSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *///from www . j a v a 2 s .c o m protected boolean execute() { // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; SocketChannel socket = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the ensemble bytes as they are read ByteBuffer ensembleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each ensemble will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); // while there are bytes to read from the socket ... while (socket.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire ensemble, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the ensemble in the Sink client code System.out.print("\rProcessed byte # " + ensembleByteCount + " " + new String(Hex.encodeHex((new byte[] { byteOne }))) + " - log msg is: "); switch (state) { case 0: // find ensemble header id if (byteOne == 0x7F && byteTwo == 0x7F) { ensembleByteCount++; // add Header ID ensembleChecksum += (byteTwo & 0xFF); ensembleByteCount++; // add Data Source ID ensembleChecksum += (byteOne & 0xFF); state = 1; break; } else { break; } case 1: // find the Ensemble Length (LSB) ensembleByteCount++; // add Ensemble Byte Count (LSB) ensembleChecksum += (byteOne & 0xFF); state = 2; break; case 2: // find the Ensemble Length (MSB) ensembleByteCount++; // add Ensemble Byte Count (MSB) ensembleChecksum += (byteOne & 0xFF); int upperEnsembleByte = (byteOne & 0xFF) << 8; int lowerEnsembleByte = (byteTwo & 0xFF); ensembleBytes = upperEnsembleByte + lowerEnsembleByte; logger.debug("Number of Bytes in the Ensemble: " + ensembleBytes); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteFour); ensembleBuffer.put(byteThree); ensembleBuffer.put(byteTwo); ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteFour); ensembleBuffer.put(byteThree); ensembleBuffer.put(byteTwo); ensembleBuffer.put(byteOne); } state = 3; break; // verify that the header is real, not a random 0x7F7F case 3: // find the number of data types in the ensemble // set the numberOfDataTypes byte if (ensembleByteCount == NUMBER_OF_DATA_TYPES_OFFSET - 1) { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); numberOfDataTypes = (byteOne & 0xFF); // calculate the number of bytes to the Fixed Leader ID dataTypeOneOffset = 6 + (2 * numberOfDataTypes); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } state = 4; break; } else { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } case 4: // find the offset to data type #1 and verify the header ID if ((ensembleByteCount == dataTypeOneOffset + 1) && byteOne == 0x00 && byteTwo == 0x00) { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); // we are confident that the previous sequence of 0x7F7F is truly // an headerID and not a random occurrence in the stream because // we have identified the Fixed Leader ID (0x0000) the correct // number of bytes beyond the 0x7F7F headerIsVerified = true; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } state = 5; break; } else { if (ensembleByteCount > dataTypeOneOffset + 1) { // We've hit a random 0x7F7F byte sequence that is not a true // ensemble header id. Reset the processing and look for the // next 0x7F7F sequence in the stream ensembleByteCount = 0; ensembleChecksum = 0; dataTypeOneOffset = 0; numberOfDataTypes = 0; headerIsVerified = false; ensembleBuffer.clear(); rbnbChannelMap.Clear(); channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 0; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } else { // We are still parsing bytes between the purported header ID // and fixed leader ID. Keep parsing until we hit the fixed // leader ID, or until we are greater than the dataTypeOneOffset // stated value. ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } } case 5: // read the rest of the bytes to the next Header ID // if we've made it to the next ensemble's header id, prepare to // flush the data. Also check that the calculated byte count // is greater than the recorded byte count in case of finding an // arbitrary 0x7f 0x7f sequence in the data stream if (byteOne == 0x7F && byteTwo == 0x7F && (ensembleByteCount == ensembleBytes + 3) && headerIsVerified) { // remove the last bytes from the count (byteOne and byteTwo) ensembleByteCount -= 1; // remove the last three bytes from the checksum: // the two checksum bytes are not included, and the two 0x7f //bytes belong to the next ensemble, and one of them was // previously added. Reset the buffer position due to this too. //ensembleChecksum -= (byteOne & 0xFF); ensembleChecksum -= (byteTwo & 0xFF); ensembleChecksum -= (byteThree & 0xFF); ensembleChecksum -= (byteFour & 0xFF); // We are consistently 1 byte over in the checksum. Trim it. We need to // troubleshoot why this is. CSJ 12/18/2007 ensembleChecksum = ensembleChecksum - 1; // jockey byteThree into LSB, byteFour into MSB int upperChecksumByte = (byteThree & 0xFF) << 8; int lowerChecksumByte = (byteFour & 0xFF); int trueChecksum = upperChecksumByte + lowerChecksumByte; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put((byte) lowerChecksumByte); ensembleBuffer.put((byte) (upperChecksumByte >> 8)); } else { ensembleBuffer.compact(); ensembleBuffer.put((byte) lowerChecksumByte); ensembleBuffer.put((byte) (upperChecksumByte >> 8)); } // check if the calculated checksum (modulo 65535) is equal // to the true checksum; if so, flush to the data turbine // Also, if the checksums are off by 1 byte, also flush the // data. We need to troubleshoot this bug CSJ 06/11/2008 if (((ensembleChecksum % 65535) == trueChecksum) || ((ensembleChecksum + 1) % 65535 == trueChecksum) || ((ensembleChecksum - 1) % 65535 == trueChecksum)) { // extract just the length of the ensemble bytes out of the // ensemble buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] ensembleArray = new byte[ensembleByteCount]; ensembleBuffer.flip(); ensembleBuffer.get(ensembleArray); // send the ensemble to the data turbine rbnbChannelMap.PutTimeAuto("server"); rbnbChannelMap.PutDataAsByteArray(channelIndex, ensembleArray); getSource().Flush(rbnbChannelMap); logger.debug("flushed: " + ensembleByteCount + " " + "ens cksum: " + ensembleChecksum + "\t\t" + "ens pos: " + ensembleBuffer.position() + "\t" + "ens rem: " + ensembleBuffer.remaining() + "\t" + "buf pos: " + buffer.position() + "\t" + "buf rem: " + buffer.remaining() + "\t" + "state: " + state); logger.info("Sent ADCP ensemble to the data turbine."); // only clear all four bytes if we are not one or two bytes // from the end of the byte buffer (i.e. the header id // is split or is all in the previous buffer) if (byteOne == 0x7f && byteTwo == 0x7f && ensembleByteCount > ensembleBytes && buffer.position() == 0) { byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ONLY b3, b4."); } else if (byteOne == 0x7f && ensembleByteCount > ensembleBytes && buffer.position() == 1) { buffer.position(buffer.position() - 1); byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ONLY b2, b3, b4."); } else { byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ALL b1, b2, b3, b4."); } //rewind the position to before the next ensemble's header id if (buffer.position() >= 2) { buffer.position(buffer.position() - 2); logger.debug("Moved position back two, now: " + buffer.position()); } ensembleBuffer.clear(); ensembleByteCount = 0; ensembleBytes = 0; ensembleChecksum = 0; state = 0; break; } else { // The checksums don't match, move on logger.info("not equal: " + "calc chksum: " + (ensembleChecksum % 65535) + "\tens chksum: " + trueChecksum + "\tbuf pos: " + buffer.position() + "\tbuf rem: " + buffer.remaining() + "\tens pos: " + ensembleBuffer.position() + "\tens rem: " + ensembleBuffer.remaining() + "\tstate: " + state); rbnbChannelMap.Clear(); channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); ensembleBuffer.clear(); ensembleByteCount = 0; ensembleChecksum = 0; ensembleBuffer.clear(); state = 0; break; } } else { // still in the middle of the ensemble, keep adding bytes ensembleByteCount++; // add each byte found ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } } // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; logger.debug("remaining:\t" + buffer.remaining() + "\tstate:\t" + state + "\tens byte count:\t" + ensembleByteCount + "\tens bytes:\t" + ensembleBytes + "\tver:\t" + headerIsVerified + "\tbyte value:\t" + new String(Hex.encodeHex((new byte[] { byteOne })))); } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socket bytes to read) socket.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } return !failed; }
From source file:edu.hawaii.soest.kilonalu.ctd.SeahorseSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *///from www . j av a2 s.c o m protected boolean execute() { logger.debug("SeahorseSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; this.socketChannel = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // define a byte array that will be used to manipulate the incoming bytes byte[] resultArray; String resultString; // Create a buffer that will store the result bytes as they are read ByteBuffer resultBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); this.rbnbChannelMap = new ChannelMap(); this.channelIndex = 0; // initiate the session with the modem, test if is network registered this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); // while there are bytes to read from the socketChannel ... while (socketChannel.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); //logger.debug("b1: " + new String(Hex.encodeHex((new byte[]{byteOne}))) + "\t" + // "b2: " + new String(Hex.encodeHex((new byte[]{byteTwo}))) + "\t" + // "b3: " + new String(Hex.encodeHex((new byte[]{byteThree}))) + "\t" + // "b4: " + new String(Hex.encodeHex((new byte[]{byteFour}))) + "\t" + // "result pos: " + resultBuffer.position() + "\t" + // "result rem: " + resultBuffer.remaining() + "\t" + // "result cnt: " + resultByteCount + "\t" + // "buffer pos: " + buffer.position() + "\t" + // "buffer rem: " + buffer.remaining() + "\t" + // "state: " + state //); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the sample in the Sink client code switch (state) { case 0: // the network registration status should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) { logger.debug("Received the registration status result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the network registration status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Network Registration Result: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // send a request for the signal strength this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 1; break; } else { this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } break; } case 1: // report the signal strength of the Iridium modem // the signal strength status should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0A && byteTwo == 0x0D && byteThree == 0x4B && byteFour == 0x4F) { logger.debug("Received the signal strength result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the signal strength status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Signal Strength Result: " + resultString.trim()); int signalStrengthIndex = resultString.indexOf(this.SIGNAL_STRENGTH) + 5; int signalStrength = new Integer( resultString.substring(signalStrengthIndex, signalStrengthIndex + 1)) .intValue(); // test if the signal strength is above the threshold if (signalStrength > SIGNAL_THRESHOLD) { resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 2; break; // the signal strength is too low, check again } else { resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // resend a request for the signal strength this.command = this.MODEM_COMMAND_PREFIX + this.SIGNAL_STRENGTH_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 1; break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 2: // handle the RING command from the instrument // listen for the RING command // note bytes are in reverse order in the FIFO window if (byteOne == 0x47 && byteTwo == 0x4E && byteThree == 0x49 && byteFour == 0x52) { logger.debug("Received the RING command."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // answer the call this.command = this.MODEM_COMMAND_PREFIX + this.ANSWER_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 3; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 3: // acknowledge the connection // the ready status string should end in READY\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x59 && byteThree == 0x44 && byteFour == 0x41) { logger.debug("Received the ready status result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the connect rate and ready status string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); // test the connect rate logger.debug("Result from ATA: " + resultString); if (resultString.indexOf(this.CONNECT_RATE) > 0) { logger.debug("Connect Rate Result: " + this.CONNECT_RATE); // test the ready status if (resultString.indexOf(this.READY_STATUS) > 0) { logger.debug("Connect Rate Result: " + this.READY_STATUS); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // acknowledge the ready status this.command = this.ACKNOWLEDGE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to receive the ACK streamingThread.sleep(this.SLEEP_INTERVAL); // query the instrument id this.command = this.ID_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 4; break; } else { logger.debug("The ready status differs from: " + this.READY_STATUS); // throw an exception here? break; } } else { logger.debug("The connect rate differs from: " + this.CONNECT_RATE); // throw an exception here? break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 4: // get the instrument id // the instrument ID string should end in \r if (byteOne == 0x0D) { logger.debug("Received the instrument ID result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the instrument ID string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse Instrument ID: " + resultString.trim()); // set the platformID variable this.platformID = resultString.substring(0, resultString.length() - 1); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the battery voltage this.command = this.BATTERY_VOLTAGE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 5; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 5: // get the seahorse battery voltage // the battery voltage string should end in \r if (byteOne == 0x0D) { logger.debug("Received the instrument battery voltage result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the battery voltage string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse Battery Voltage: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the GPS location this.command = this.GPRMC_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 6; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 6: // the GPRMC string should end in END\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x44 && byteThree == 0x4E && byteFour == 0x45) { logger.debug("Received the GPRMS result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the GPRMC string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Seahorse GPRMC string: " + resultString.trim()); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; // query the file name for transfer this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); state = 7; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 7: // the file name string should end in .Z\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x5A && byteThree == 0x2E) { logger.debug("Received the file name result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the file name string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("File name result: " + resultString.trim()); resultString = resultString.trim(); int fileNameIndex = resultString.indexOf(this.FILENAME_PREFIX); //extract just the filename from the result (excise the "FILE=") this.fileNameToDownload = resultString.substring( (fileNameIndex + (this.FILENAME_PREFIX).length()), resultString.length()); logger.debug("File name to download: " + this.fileNameToDownload); // test to see if the GFN command returns FILES=NONE if (!(resultString.indexOf(this.END_OF_FILES) > 0)) { // there is a file to download. parse the file name, // get the number of blocks to transfer this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 8; break; } else { // We have downloaded all files. Parse the data string, // build the channel map, and flush the data to the Dataturbine // by iterating through the data matrix. The metadata and // ASCII data strings are flushed once with the first matrix // row. // Parse the data file, not the cast file. try { // parse the CTD data file this.ctdParser = new CTDParser(this.dataFileString); // convert the raw frequencies and voltages to engineering // units and return the data as a matrix CTDConverter ctdConverter = new CTDConverter(this.ctdParser); ctdConverter.convert(); RealMatrix convertedDataMatrix = ctdConverter.getConvertedDataValuesMatrix(); // Register the data and metadata channels; failed = register(); if (!failed) { // format the first sample date and use it as the first insert // date. Add the sampleInterval on each iteration to insert // subsequent data rows. Sample interval is by default // 4 scans/second for the CTD. DATE_FORMAT.setTimeZone(TZ); this.sampleDateTime = Calendar.getInstance(); this.sampleDateTime .setTime(DATE_FORMAT.parse(ctdParser.getFirstSampleTime())); for (int row = 0; row < convertedDataMatrix.getRowDimension(); row++) { // Only insert the metadata fields and full ASCII text strings // with the first row of data if (row == 0) { // Add the samplingMode data to the channel map this.channelIndex = this.rbnbChannelMap.Add("samplingMode"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSamplingMode()); // Add the firstSampleTime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("firstSampleTime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getFirstSampleTime()); // Add the fileName data to the channel map this.channelIndex = this.rbnbChannelMap.Add("fileName"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getFileName()); // Add the temperatureSerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureSerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTemperatureSerialNumber()); // Add the conductivitySerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivitySerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getConductivitySerialNumber()); // Add the systemUpLoadTime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("systemUpLoadTime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSystemUpLoadTime()); // Add the cruiseInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("cruiseInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getCruiseInformation()); // Add the stationInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("stationInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getStationInformation()); // Add the shipInformation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("shipInformation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getShipInformation()); // Add the chiefScientist data to the channel map this.channelIndex = this.rbnbChannelMap.Add("chiefScientist"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getChiefScientist()); // Add the organization data to the channel map this.channelIndex = this.rbnbChannelMap.Add("organization"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getOrganization()); // Add the areaOfOperation data to the channel map this.channelIndex = this.rbnbChannelMap.Add("areaOfOperation"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getAreaOfOperation()); // Add the instrumentPackage data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentPackage"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getInstrumentPackage()); // Add the mooringNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("mooringNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getMooringNumber()); // Add the instrumentLatitude data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentLatitude"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getInstrumentLatitude() }); // Add the instrumentLongitude data to the channel map this.channelIndex = this.rbnbChannelMap.Add("instrumentLongitude"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getInstrumentLongitude() }); // Add the depthSounding data to the channel map this.channelIndex = this.rbnbChannelMap.Add("depthSounding"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getDepthSounding() }); // Add the profileNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("profileNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getProfileNumber()); // Add the profileDirection data to the channel map this.channelIndex = this.rbnbChannelMap.Add("profileDirection"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getProfileDirection()); // Add the deploymentNotes data to the channel map this.channelIndex = this.rbnbChannelMap.Add("deploymentNotes"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getDeploymentNotes()); // Add the mainBatteryVoltage data to the channel map this.channelIndex = this.rbnbChannelMap.Add("mainBatteryVoltage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getMainBatteryVoltage() }); // Add the lithiumBatteryVoltage data to the channel map this.channelIndex = this.rbnbChannelMap .Add("lithiumBatteryVoltage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getLithiumBatteryVoltage() }); // Add the operatingCurrent data to the channel map this.channelIndex = this.rbnbChannelMap.Add("operatingCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getOperatingCurrent() }); // Add the pumpCurrent data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pumpCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPumpCurrent() }); // Add the channels01ExternalCurrent data to the channel map this.channelIndex = this.rbnbChannelMap .Add("channels01ExternalCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getChannels01ExternalCurrent() }); // Add the channels23ExternalCurrent data to the channel map this.channelIndex = this.rbnbChannelMap .Add("channels23ExternalCurrent"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getChannels23ExternalCurrent() }); // Add the loggingStatus data to the channel map this.channelIndex = this.rbnbChannelMap.Add("loggingStatus"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getLoggingStatus()); // Add the numberOfScansToAverage data to the channel map this.channelIndex = this.rbnbChannelMap .Add("numberOfScansToAverage"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfScansToAverage() }); // Add the numberOfSamples data to the channel map this.channelIndex = this.rbnbChannelMap.Add("numberOfSamples"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfSamples() }); // Add the numberOfAvailableSamples data to the channel map this.channelIndex = this.rbnbChannelMap .Add("numberOfAvailableSamples"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfAvailableSamples() }); // Add the sampleInterval data to the channel map this.channelIndex = this.rbnbChannelMap.Add("sampleInterval"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getSampleInterval() }); // Add the measurementsPerSample data to the channel map this.channelIndex = this.rbnbChannelMap .Add("measurementsPerSample"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getMeasurementsPerSample() }); // Add the transmitRealtime data to the channel map this.channelIndex = this.rbnbChannelMap.Add("transmitRealtime"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTransmitRealtime()); // Add the numberOfCasts data to the channel map this.channelIndex = this.rbnbChannelMap.Add("numberOfCasts"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getNumberOfCasts() }); // Add the minimumConductivityFrequency data to the channel map this.channelIndex = this.rbnbChannelMap .Add("minimumConductivityFrequency"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getMinimumConductivityFrequency() }); // Add the pumpDelay data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pumpDelay"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsInt32(this.channelIndex, new int[] { this.ctdParser.getPumpDelay() }); // Add the automaticLogging data to the channel map this.channelIndex = this.rbnbChannelMap.Add("automaticLogging"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getAutomaticLogging()); // Add the ignoreMagneticSwitch data to the channel map this.channelIndex = this.rbnbChannelMap.Add("ignoreMagneticSwitch"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getIgnoreMagneticSwitch()); // Add the batteryType data to the channel map this.channelIndex = this.rbnbChannelMap.Add("batteryType"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getBatteryType()); // Add the batteryCutoff data to the channel map this.channelIndex = this.rbnbChannelMap.Add("batteryCutoff"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getBatteryCutoff()); // Add the pressureSensorType data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSensorType"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSensorType()); // Add the pressureSensorRange data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSensorRange"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSensorRange()); // Add the sbe38TemperatureSensor data to the channel map this.channelIndex = this.rbnbChannelMap .Add("sbe38TemperatureSensor"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getSbe38TemperatureSensor()); // Add the gasTensionDevice data to the channel map this.channelIndex = this.rbnbChannelMap.Add("gasTensionDevice"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getGasTensionDevice()); // Add the externalVoltageChannelZero data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelZero"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelZero()); // Add the externalVoltageChannelOne data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelOne"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelOne()); // Add the externalVoltageChannelTwo data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelTwo"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelTwo()); // Add the externalVoltageChannelThree data to the channel map this.channelIndex = this.rbnbChannelMap .Add("externalVoltageChannelThree"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getExternalVoltageChannelThree()); // Add the echoCommands data to the channel map this.channelIndex = this.rbnbChannelMap.Add("echoCommands"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getEchoCommands()); // Add the outputFormat data to the channel map this.channelIndex = this.rbnbChannelMap.Add("outputFormat"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getOutputFormat()); // Add the temperatureCalibrationDate data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCalibrationDate"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getTemperatureCalibrationDate()); // Add the temperatureCoefficientTA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA0() }); // Add the temperatureCoefficientTA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA1() }); // Add the temperatureCoefficientTA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA2() }); // Add the temperatureCoefficientTA3 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureCoefficientTA3"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureCoefficientTA3() }); // Add the temperatureOffsetCoefficient data to the channel map this.channelIndex = this.rbnbChannelMap .Add("temperatureOffsetCoefficient"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getTemperatureOffsetCoefficient() }); // Add the conductivityCalibrationDate data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCalibrationDate"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getConductivityCalibrationDate()); // Add the conductivityCoefficientG data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientG"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientG() }); // Add the conductivityCoefficientH data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientH"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientH() }); // Add the conductivityCoefficientI data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientI"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientI() }); // Add the conductivityCoefficientJ data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientJ"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientJ() }); // Add the conductivityCoefficientCF0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCF0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCF0() }); // Add the conductivityCoefficientCPCOR data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCPCOR"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCPCOR() }); // Add the conductivityCoefficientCTCOR data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCTCOR"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getConductivityCoefficientCTCOR() }); // Add the conductivityCoefficientCSLOPE data to the channel map this.channelIndex = this.rbnbChannelMap .Add("conductivityCoefficientCSLOPE"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser .getConductivityCoefficientCSLOPE() }); // Add the pressureSerialNumber data to the channel map this.channelIndex = this.rbnbChannelMap.Add("pressureSerialNumber"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.ctdParser.getPressureSerialNumber()); // Add the pressureCoefficientPA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA0() }); // Add the pressureCoefficientPA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA1() }); // Add the pressureCoefficientPA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPA2() }); // Add the pressureCoefficientPTCA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA0() }); // Add the pressureCoefficientPTCA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA1() }); // Add the pressureCoefficientPTCA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCA2() }); // Add the pressureCoefficientPTCB0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB0() }); // Add the pressureCoefficientPTCB1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB1() }); // Add the pressureCoefficientPTCB2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTCB2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTCB2() }); // Add the pressureCoefficientPTEMPA0 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA0"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA0() }); // Add the pressureCoefficientPTEMPA1 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA1"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA1() }); // Add the pressureCoefficientPTEMPA2 data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureCoefficientPTEMPA2"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureCoefficientPTEMPA2() }); // Add the pressureOffsetCoefficient data to the channel map this.channelIndex = this.rbnbChannelMap .Add("pressureOffsetCoefficient"); this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { this.ctdParser.getPressureOffsetCoefficient() }); // Insert the file into the channel map. this.channelIndex = this.rbnbChannelMap.Add(this.rbnbChannelName); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.dataFileString); this.channelIndex = this.rbnbChannelMap.Add("ASCIICastData"); this.rbnbChannelMap.PutMime(this.channelIndex, "text/plain"); this.rbnbChannelMap.PutDataAsString(this.channelIndex, this.castFileString); } // Add in the matrix data row to the map here List<String> variableNames = ctdParser.getDataVariableNames(); List<String> variableUnits = ctdParser.getDataVariableUnits(); // iterate through the variable names and add them to // the channel map. for (int variableIndex = 0; variableIndex < variableNames .size(); variableIndex++) { // Add the variable name to the channel map this.channelIndex = this.rbnbChannelMap .Add(variableNames.get(variableIndex)); // The matrix is a double array, so set the data type below this.rbnbChannelMap.PutMime(this.channelIndex, "application/octet-stream"); // add the data to the map from the [row,column] of the // matrix (row is from the outer for loop) this.rbnbChannelMap.PutDataAsFloat64(this.channelIndex, new double[] { convertedDataMatrix.getEntry(row, variableIndex) }); } // Flush the channel map to the RBNB double sampleTimeAsSecondsSinceEpoch = (double) (this.sampleDateTime .getTimeInMillis() / 1000); this.rbnbChannelMap.PutTime(sampleTimeAsSecondsSinceEpoch, 0d); getSource().Flush(this.rbnbChannelMap); logger.info("Flushed data to the DataTurbine."); this.rbnbChannelMap.Clear(); // samples are taken 4x per second, so increment the // sample time by 250 milliseconds for the next insert this.sampleDateTime.add(Calendar.MILLISECOND, 250); } // end for loop } // end if !failed } catch (Exception e) { logger.debug("Failed to parse the CTD data file: " + e.getMessage()); } // there are no more files to read. close the Tx session. this.command = this.CLOSE_TRANSFER_SESSION_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); // clean up resultBuffer.clear(); this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 10; break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 8: // the number of blocks string should end in \r if (byteOne == 0x0D) { logger.debug("Received the number of blocks result."); this.resultByteCount++; // add the last byte found to the count // add the last byte found to the result buffer if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); resultBuffer.put(byteOne); } // report the number of blocks string resultArray = new byte[this.resultByteCount]; resultBuffer.flip(); resultBuffer.get(resultArray); resultString = new String(resultArray, "US-ASCII"); logger.debug("Number of bytes reported: " + resultString.trim()); int numberOfBlocksIndex = resultString.indexOf(this.BLOCKSIZE_PREFIX); // If 'BLOCKSIZE=' is not found, set the index to 0 if (numberOfBlocksIndex == -1) { numberOfBlocksIndex = 0; } resultString = resultString.substring( (numberOfBlocksIndex + (this.BLOCKSIZE_PREFIX).length()), resultString.length()); // convert the string to an integer try { this.numberOfBlocks = new Integer(resultString.trim()).intValue(); logger.debug("Number of bytes to download: " + this.numberOfBlocks); } catch (java.lang.NumberFormatException nfe) { failed = true; nfe.printStackTrace(); logger.debug("Failed to convert returned string value " + "to an integer value. The returned string is: " + this.numberOfBlocks); } // test to see if the GNB command returns DONE\r if (!(resultString.indexOf(this.TRANSFER_COMPLETE) > 0)) { // there are bytes to transfer. send the transfer command this.command = this.TRANSFER_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); dont clear the buffer this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 9; break; } else { // there are no more bytes to transfer. // Decompress the file, which is under zlib compression. Inflater inflater = new Inflater(); inflater.setInput(resultBuffer.array()); byte[] output = new byte[resultBuffer.capacity()]; int numDecompressed = inflater.inflate(output); // set the appropriate string variable if (this.fileNameToDownload.indexOf(DATA_FILE_PREFIX) > 0) { this.dataFileString = new String(output); //report the file contents to the log logger.debug("File " + this.fileNameToDownload + ": "); logger.debug(this.dataFileString); } else { this.castFileString = new String(output); //report the file contents to the log logger.debug("File " + this.fileNameToDownload + ": "); logger.debug(this.castFileString); } // Ask for the next file. this.command = this.FILENAME_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); dont clear the buffer this.resultByteCount = 0; resultArray = new byte[0]; resultString = ""; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 7; //back to the file name state break; } } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 9: // transfer up to the reported number of bytes if (this.resultByteCount == this.numberOfBlocks) { // we have downloaded the reported bytes. get the next section. // get the number of blocks to transfer this.command = this.NUMBER_OF_BLOCKS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); //resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 8; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 10: // the response from the modem should end in BYE\r // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x45 && byteThree == 0x59 && byteFour == 0x42) { logger.debug("Received the BYE command."); // continue to disconnect. send the escape sequence this.command = this.ESCAPE_SEQUENCE_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 11; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 11: // the response from the modem should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) { // now hang up. this.command = this.MODEM_COMMAND_PREFIX + this.HANGUP_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 12; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } case 12: // the response from the modem should end in OK\r\n // note bytes are in reverse order in the FIFO window if (byteOne == 0x0D && byteTwo == 0x0A && byteThree == 0x4B && byteFour == 0x4F) { // we are done. re-test if is network registered this.command = this.MODEM_COMMAND_PREFIX + this.REGISTRATION_STATUS_COMMAND + this.MODEM_COMMAND_SUFFIX; this.sentCommand = queryInstrument(this.command); // allow time for the modem to respond streamingThread.sleep(this.SLEEP_INTERVAL); resultBuffer.clear(); this.resultByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 0; break; } else { // still in the middle of the result, keep adding bytes this.resultByteCount++; // add each byte found if (resultBuffer.remaining() > 0) { resultBuffer.put(byteOne); } else { resultBuffer.compact(); logger.debug("Compacting resultBuffer ..."); resultBuffer.put(byteOne); } break; } } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socketChannel bytes to read) socketChannel.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (java.lang.InterruptedException ine) { failed = true; ine.printStackTrace(); return !failed; } catch (java.util.zip.DataFormatException dfe) { failed = true; dfe.printStackTrace(); return !failed; } return !failed; }
From source file:org.apache.geode.internal.cache.Oplog.java
private void flush(OplogFile olf, ByteBuffer b1, ByteBuffer b2) throws IOException { try {// w w w . j a va 2 s . c om // No need to get the backup lock prior to synchronizing (correct lock order) since the // synchronized block does not attempt to get the backup lock (incorrect lock order) synchronized (this.lock/* olf */) { if (olf.RAFClosed) { return; } this.bbArray[0] = b1; this.bbArray[1] = b2; b1.flip(); long flushed = 0; do { flushed += olf.channel.write(this.bbArray); } while (b2.hasRemaining()); this.bbArray[0] = null; this.bbArray[1] = null; // update bytesFlushed after entire writeBuffer is flushed to fix bug 41201 olf.bytesFlushed += flushed; b1.clear(); } } catch (ClosedChannelException ignore) { // It is possible for a channel to be closed when our code does not // explicitly call channel.close (when we will set RAFclosed). // This can happen when a thread is doing an io op and is interrupted. // That thread will see ClosedByInterruptException but it will also // close the channel and then we will see ClosedChannelException. } }
From source file:org.apache.geode.internal.cache.Oplog.java
private void flush(OplogFile olf, boolean doSync) throws IOException { try {/* w ww. ja va 2 s . c o m*/ // No need to get the backup lock prior to synchronizing (correct lock order) since the // synchronized block does not attempt to get the backup lock (incorrect lock order) synchronized (this.lock/* olf */) { if (olf.RAFClosed) { return; } ByteBuffer bb = olf.writeBuf; if (bb != null && bb.position() != 0) { bb.flip(); int flushed = 0; int numChannelRetries = 0; do { int channelBytesWritten = 0; final int bbStartPos = bb.position(); final long channelStartPos = olf.channel.position(); // differentiate between bytes written on this channel.write() iteration and the // total number of bytes written to the channel on this call channelBytesWritten = olf.channel.write(bb); // Expect channelBytesWritten and the changes in pp.position() and channel.position() to // be the same. If they are not, then the channel.write() silently failed. The following // retry separates spurious failures from permanent channel failures. if (channelBytesWritten != bb.position() - bbStartPos) { if (numChannelRetries++ < MAX_CHANNEL_RETRIES) { // Reset the ByteBuffer position, but take into account anything that did get // written to the channel channelBytesWritten = (int) (olf.channel.position() - channelStartPos); bb.position(bbStartPos + channelBytesWritten); } else { throw new IOException("Failed to write Oplog entry to" + olf.f.getName() + ": " + "channel.write() returned " + channelBytesWritten + ", " + "change in channel position = " + (olf.channel.position() - channelStartPos) + ", " + "change in source buffer position = " + (bb.position() - bbStartPos)); } } flushed += channelBytesWritten; } while (bb.hasRemaining()); // update bytesFlushed after entire writeBuffer is flushed to fix bug // 41201 olf.bytesFlushed += flushed; bb.clear(); } if (doSync) { if (SYNC_WRITES) { // Synch Meta Data as well as content olf.channel.force(true); } } } } catch (ClosedChannelException ignore) { // It is possible for a channel to be closed when our code does not // explicitly call channel.close (when we will set RAFclosed). // This can happen when a thread is doing an io op and is interrupted. // That thread will see ClosedByInterruptException but it will also // close the channel and then we will see ClosedChannelException. } }
From source file:edu.harvard.iq.dataverse.dataaccess.TabularSubsetGenerator.java
public Object[] subsetObjectVector(File tabfile, int column, int varcount, int casecount, int columntype, boolean compatmode) throws IOException { Object[] retVector = null;/*from w w w . j a v a2 s . c o m*/ boolean isString = false; boolean isDouble = false; boolean isLong = false; boolean isFloat = false; //Locale loc = new Locale("en", "US"); if (columntype == COLUMN_TYPE_STRING) { isString = true; retVector = new String[casecount]; } else if (columntype == COLUMN_TYPE_DOUBLE) { isDouble = true; retVector = new Double[casecount]; } else if (columntype == COLUMN_TYPE_LONG) { isLong = true; retVector = new Long[casecount]; } else if (columntype == COLUMN_TYPE_FLOAT) { isFloat = true; retVector = new Float[casecount]; } else { throw new IOException("Unsupported column type: " + columntype); } File rotatedImageFile = getRotatedImage(tabfile, varcount, casecount); long[] columnEndOffsets = extractColumnOffsets(rotatedImageFile, varcount, casecount); long columnOffset = 0; long columnLength = 0; if (column > 0) { columnOffset = columnEndOffsets[column - 1]; columnLength = columnEndOffsets[column] - columnEndOffsets[column - 1]; } else { columnOffset = varcount * 8; columnLength = columnEndOffsets[0] - varcount * 8; } FileChannel fc = (FileChannel.open(Paths.get(rotatedImageFile.getAbsolutePath()), StandardOpenOption.READ)); fc.position(columnOffset); int MAX_COLUMN_BUFFER = 8192; ByteBuffer in = ByteBuffer.allocate(MAX_COLUMN_BUFFER); if (columnLength < MAX_COLUMN_BUFFER) { in.limit((int) (columnLength)); } long bytesRead = 0; long bytesReadTotal = 0; int caseindex = 0; int byteoffset = 0; byte[] leftover = null; while (bytesReadTotal < columnLength) { bytesRead = fc.read(in); byte[] columnBytes = in.array(); int bytecount = 0; while (bytecount < bytesRead) { if (columnBytes[bytecount] == '\n') { /* String token = new String(columnBytes, byteoffset, bytecount-byteoffset, "UTF8"); if (leftover != null) { String leftoverString = new String (leftover, "UTF8"); token = leftoverString + token; leftover = null; } */ /* * Note that the way I was doing it at first - above - * was not quite the correct way - because I was creating UTF8 * strings from the leftover bytes, and the bytes in the * current buffer *separately*; which means, if a multi-byte * UTF8 character got split in the middle between one buffer * and the next, both chunks of it would become junk * characters, on each side! * The correct way of doing it, of course, is to create a * merged byte buffer, and then turn it into a UTF8 string. * -- L.A. 4.0 */ String token = null; if (leftover == null) { token = new String(columnBytes, byteoffset, bytecount - byteoffset, "UTF8"); } else { byte[] merged = new byte[leftover.length + bytecount - byteoffset]; System.arraycopy(leftover, 0, merged, 0, leftover.length); System.arraycopy(columnBytes, byteoffset, merged, leftover.length, bytecount - byteoffset); token = new String(merged, "UTF8"); leftover = null; merged = null; } if (isString) { if ("".equals(token)) { // An empty string is a string missing value! // An empty string in quotes is an empty string! retVector[caseindex] = null; } else { // Strip the outer quotes: token = token.replaceFirst("^\\\"", ""); token = token.replaceFirst("\\\"$", ""); // We need to restore the special characters that // are stored in tab files escaped - quotes, new lines // and tabs. Before we do that however, we need to // take care of any escaped backslashes stored in // the tab file. I.e., "foo\t" should be transformed // to "foo<TAB>"; but "foo\\t" should be transformed // to "foo\t". This way new lines and tabs that were // already escaped in the original data are not // going to be transformed to unescaped tab and // new line characters! String[] splitTokens = token.split(Matcher.quoteReplacement("\\\\"), -2); // (note that it's important to use the 2-argument version // of String.split(), and set the limit argument to a // negative value; otherwise any trailing backslashes // are lost.) for (int i = 0; i < splitTokens.length; i++) { splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\\""), "\""); splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\t"), "\t"); splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\n"), "\n"); splitTokens[i] = splitTokens[i].replaceAll(Matcher.quoteReplacement("\\r"), "\r"); } // TODO: // Make (some of?) the above optional; for ex., we // do need to restore the newlines when calculating UNFs; // But if we are subsetting these vectors in order to // create a new tab-delimited file, they will // actually break things! -- L.A. Jul. 28 2014 token = StringUtils.join(splitTokens, '\\'); // "compatibility mode" - a hack, to be able to produce // unfs identical to those produced by the "early" // unf5 jar; will be removed in production 4.0. // -- L.A. (TODO: ...) if (compatmode && !"".equals(token)) { if (token.length() > 128) { if ("".equals(token.trim())) { // don't ask... token = token.substring(0, 129); } else { token = token.substring(0, 128); //token = String.format(loc, "%.128s", token); token = token.trim(); //dbgLog.info("formatted and trimmed: "+token); } } else { if ("".equals(token.trim())) { // again, don't ask; // - this replicates some bugginness // that happens inside unf5; token = "null"; } else { token = token.trim(); } } } retVector[caseindex] = token; } } else if (isDouble) { try { // TODO: verify that NaN and +-Inf are // handled correctly here! -- L.A. // Verified: new Double("nan") works correctly, // resulting in Double.NaN; // Double("[+-]Inf") doesn't work however; // (the constructor appears to be expecting it // to be spelled as "Infinity", "-Infinity", etc. if ("inf".equalsIgnoreCase(token) || "+inf".equalsIgnoreCase(token)) { retVector[caseindex] = java.lang.Double.POSITIVE_INFINITY; } else if ("-inf".equalsIgnoreCase(token)) { retVector[caseindex] = java.lang.Double.NEGATIVE_INFINITY; } else if (token == null || token.equals("")) { // missing value: retVector[caseindex] = null; } else { retVector[caseindex] = new Double(token); } } catch (NumberFormatException ex) { dbgLog.warning("NumberFormatException thrown for " + token + " as Double"); retVector[caseindex] = null; // missing value // TODO: ? } } else if (isLong) { try { retVector[caseindex] = new Long(token); } catch (NumberFormatException ex) { retVector[caseindex] = null; // assume missing value } } else if (isFloat) { try { if ("inf".equalsIgnoreCase(token) || "+inf".equalsIgnoreCase(token)) { retVector[caseindex] = java.lang.Float.POSITIVE_INFINITY; } else if ("-inf".equalsIgnoreCase(token)) { retVector[caseindex] = java.lang.Float.NEGATIVE_INFINITY; } else if (token == null || token.equals("")) { // missing value: retVector[caseindex] = null; } else { retVector[caseindex] = new Float(token); } } catch (NumberFormatException ex) { dbgLog.warning("NumberFormatException thrown for " + token + " as Float"); retVector[caseindex] = null; // assume missing value (TODO: ?) } } caseindex++; if (bytecount == bytesRead - 1) { byteoffset = 0; } else { byteoffset = bytecount + 1; } } else { if (bytecount == bytesRead - 1) { // We've reached the end of the buffer; // This means we'll save whatever unused bytes left in // it - i.e., the bytes between the last new line // encountered and the end - in the leftover buffer. // *EXCEPT*, there may be a case of a very long String // that is actually longer than MAX_COLUMN_BUFFER, in // which case it is possible that we've read through // an entire buffer of bytes without finding any // new lines... in this case we may need to add this // entire byte buffer to an already existing leftover // buffer! if (leftover == null) { leftover = new byte[(int) bytesRead - byteoffset]; System.arraycopy(columnBytes, byteoffset, leftover, 0, (int) bytesRead - byteoffset); } else { if (byteoffset != 0) { throw new IOException( "Reached the end of the byte buffer, with some leftover left from the last read; yet the offset is not zero!"); } byte[] merged = new byte[leftover.length + (int) bytesRead]; System.arraycopy(leftover, 0, merged, 0, leftover.length); System.arraycopy(columnBytes, byteoffset, merged, leftover.length, (int) bytesRead); //leftover = null; leftover = merged; merged = null; } byteoffset = 0; } } bytecount++; } bytesReadTotal += bytesRead; in.clear(); if (columnLength - bytesReadTotal < MAX_COLUMN_BUFFER) { in.limit((int) (columnLength - bytesReadTotal)); } } fc.close(); if (caseindex != casecount) { throw new IOException("Faile to read " + casecount + " tokens for column " + column); //System.out.println("read "+caseindex+" tokens instead of expected "+casecount+"."); } return retVector; }
From source file:edu.hawaii.soest.kilonalu.adam.AdamSource.java
/** * A method that processes the data ByteBuffer passed in for the given IP * address of the ADAM sensor, parses the binary ADAM data, and flushes the * data to the DataTurbine given the sensor properties in the XMLConfiguration * passed in.//from w ww.j a va 2s .c o m * * @param datagramAddress - the IP address of the datagram of this packet of data * @param xmlConfig - the XMLConfiguration object containing the list of * sensor properties * @param sampleBuffer - the binary data sample as a ByteBuffer */ protected boolean process(String datagramAddress, XMLConfiguration xmlConfig, ByteBuffer sampleBuffer) { logger.debug("AdamSource.process() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; try { // add channels of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. Information // on each channel is found in the XMLConfiguration file (sensors.properties.xml) // and the AdamParser object (to get the actual voltages for each ADAM channel) ChannelMap rbnbChannelMap = new ChannelMap(); // used to flush channels ChannelMap registerChannelMap = new ChannelMap(); // used to register channels int channelIndex = 0; this.adamParser = new AdamParser(sampleBuffer); logger.debug("\n" + "channelZero : " + this.adamParser.getChannelZero() + "\n" + "channelOne : " + this.adamParser.getChannelOne() + "\n" + "channelTwo : " + this.adamParser.getChannelTwo() + "\n" + "channelThree : " + this.adamParser.getChannelThree() + "\n" + "channelFour : " + this.adamParser.getChannelFour() + "\n" + "channelFive : " + this.adamParser.getChannelFive() + "\n" + "channelSix : " + this.adamParser.getChannelSix() + "\n" + "channelSeven : " + this.adamParser.getChannelSeven() + "\n" + "channelAverage : " + this.adamParser.getChannelAverage() + "\n" + "channelZeroMax : " + this.adamParser.getChannelZeroMax() + "\n" + "channelOneMax : " + this.adamParser.getChannelOneMax() + "\n" + "channelTwoMax : " + this.adamParser.getChannelTwoMax() + "\n" + "channelThreeMax : " + this.adamParser.getChannelThreeMax() + "\n" + "channelFourMax : " + this.adamParser.getChannelFourMax() + "\n" + "channelFiveMax : " + this.adamParser.getChannelFiveMax() + "\n" + "channelSixMax : " + this.adamParser.getChannelSixMax() + "\n" + "channelSevenMax : " + this.adamParser.getChannelSevenMax() + "\n" + "channelAverageMax : " + this.adamParser.getChannelAverageMax() + "\n" + "channelZeroMin : " + this.adamParser.getChannelZeroMin() + "\n" + "channelOneMin : " + this.adamParser.getChannelOneMin() + "\n" + "channelTwoMin : " + this.adamParser.getChannelTwoMin() + "\n" + "channelThreeMin : " + this.adamParser.getChannelThreeMin() + "\n" + "channelFourMin : " + this.adamParser.getChannelFourMin() + "\n" + "channelFiveMin : " + this.adamParser.getChannelFiveMin() + "\n" + "channelSixMin : " + this.adamParser.getChannelSixMin() + "\n" + "channelSevenMin : " + this.adamParser.getChannelSevenMin() + "\n" + "channelAverageMin : " + this.adamParser.getChannelAverageMin() + "\n" ); // create a TreeMap to hold the voltageChannel and its associated // RBNB ChannelMap channel string. When the RBNB ChannelMap is // populated, this TreeMap will be consulted TreeMap<Integer, String> voltageChannelTreeMap = new TreeMap<Integer, String>(); // create a character string to store characters from the voltage values StringBuilder decimalASCIISampleData = new StringBuilder(); // Create a list of sensors from the properties file, and iterate through // the list, matching the datagram IP address to the address in the // xml configuration file. If there is a match, find the correct voltage // channel to measurement mappings, create a corresponding RBNB channel // map, and flush the data to the DataTurbine. List sensorList = xmlConfig.getList("sensor.address"); // declare the properties that will be pulled from the // sensor.properties.xml file String address = ""; String sourceName = ""; String description = ""; String type = ""; String cacheSize = ""; String archiveSize = ""; String archiveChannel = ""; String portNumber = ""; String voltageChannel = ""; String measurement = ""; // evaluate each sensor listed in the sensor.properties.xml file for (Iterator sIterator = sensorList.iterator(); sIterator.hasNext();) { // get each property value of the sensor int index = sensorList.indexOf(sIterator.next()); address = (String) xmlConfig.getProperty("sensor(" + index + ").address"); sourceName = (String) xmlConfig.getProperty("sensor(" + index + ").name"); description = (String) xmlConfig.getProperty("sensor(" + index + ").description"); type = (String) xmlConfig.getProperty("sensor(" + index + ").type"); logger.debug("Sensor details:" + "\n\t\t\t\t\t\t\t\t\t\taddress : " + address + "\n\t\t\t\t\t\t\t\t\t\tname : " + sourceName + "\n\t\t\t\t\t\t\t\t\t\tdescription : " + description + "\n\t\t\t\t\t\t\t\t\t\ttype : " + type); // move to the next sensor if this doesn't match the RBNB source name if (!sourceName.equals(getRBNBClientName())) { continue; } List portList = xmlConfig.getList("sensor(" + index + ").ports.port[@number]"); // get each port of the sensor, along with the port properties for (Iterator pIterator = portList.iterator(); pIterator.hasNext();) { int pindex = portList.indexOf(pIterator.next()); // get the port number value portNumber = (String) xmlConfig .getProperty("sensor(" + index + ").ports.port(" + pindex + ")[@number]"); logger.debug("\tport " + portNumber + " details:"); List measurementList = xmlConfig .getList("sensor(" + index + ").ports.port(" + pindex + ").measurement[@label]"); // get each measurement and voltageChannel for the given port for (Iterator mIterator = measurementList.iterator(); mIterator.hasNext();) { int mindex = measurementList.indexOf(mIterator.next()); // build the property paths into the config file String voltagePath = "sensor(" + index + ").ports.port(" + pindex + ").measurement(" + mindex + ").voltageChannel"; String measurementPath = "sensor(" + index + ").ports.port(" + pindex + ").measurement(" + mindex + ")[@label]"; // get the voltageChannel and measurement label values voltageChannel = (String) xmlConfig.getProperty(voltagePath); measurement = (String) xmlConfig.getProperty(measurementPath); logger.debug("\t\t" + "voltageChannel: " + voltageChannel + "\n\t\t\t\t\t\t\t\t\t\t\t" + "measurement label: " + measurement); // Match the datagram address with the address in the xmlConfig file if (datagramAddress.equals(address)) { // and only add channel data for this class instance RBNB Source name if (sourceName.equals(getRBNBClientName())) { // create an Integer out of the voltageChannel Integer voltageChannelInt = new Integer(voltageChannel); // build the RBNB channel path string String channelPath = "port" + "/" + portNumber + "/" + measurement; voltageChannelTreeMap.put(voltageChannelInt, channelPath); } else { logger.debug("\t\tSource names don't match: " + sourceName + " != " + getRBNBClientName()); } // end sourceName if() statement } else { logger.debug("\t\tNo IP address match. " + datagramAddress + " != " + address); } //end IP address if() statement } // end for each channel } // end for each port // now that we've found the correct sensor, exit the sensor loop break; } // end for each sensor // Build the RBNB channel map from the entries in the tree map // by doing a lookup of the ADAM voltage channel values based // on the voltage channel number in the treemap. Also add the voltages // to the DecimalASCIISampleData string (and then channel) for (Iterator vcIterator = voltageChannelTreeMap.keySet().iterator(); vcIterator.hasNext();) { int voltageChannelFromMap = ((Integer) vcIterator.next()).intValue(); String channelPathFromMap = voltageChannelTreeMap.get(voltageChannelFromMap); float voltageValue = -9999.0f; // look up the voltage value from the AdamParser object based // on the voltage channel set in the xmlConfig file (via the treemap) switch (voltageChannelFromMap) { case 0: voltageValue = this.adamParser.getChannelZero(); break; case 1: voltageValue = this.adamParser.getChannelOne(); break; case 2: voltageValue = this.adamParser.getChannelTwo(); break; case 3: voltageValue = this.adamParser.getChannelThree(); break; case 4: voltageValue = this.adamParser.getChannelFour(); break; case 5: voltageValue = this.adamParser.getChannelFive(); break; case 6: voltageValue = this.adamParser.getChannelSix(); break; case 7: voltageValue = this.adamParser.getChannelSeven(); break; } // now add the channel and the voltage value to the RBNB channel maps channelIndex = registerChannelMap.Add(channelPathFromMap); registerChannelMap.PutUserInfo(channelIndex, "units=volts"); registerChannelMap.PutUserInfo(channelIndex, "description=" + description); logger.debug("Voltage Channel Tree Map: " + voltageChannelTreeMap.toString()); // then the channel and voltage channelIndex = rbnbChannelMap.Add(channelPathFromMap); rbnbChannelMap.PutMime(channelIndex, "application/octet-stream"); rbnbChannelMap.PutDataAsFloat32(channelIndex, new float[] { voltageValue }); decimalASCIISampleData.append(String.format("%05.3f", (Object) voltageValue) + ", "); } // and only flush data for this class instance RBNB Source name if (sourceName.equals(getRBNBClientName()) && datagramAddress.equals(address)) { // add the timestamp to the rbnb channel map registerChannelMap.PutTimeAuto("server"); rbnbChannelMap.PutTimeAuto("server"); // then add a timestamp to the end of the ASCII version of the sample DATE_FORMAT.setTimeZone(TZ); String sampleDateAsString = DATE_FORMAT.format(new Date()).toString(); decimalASCIISampleData.append(sampleDateAsString); decimalASCIISampleData.append("\n"); // add the DecimalASCIISampleData channel to the channelMap channelIndex = registerChannelMap.Add(getRBNBChannelName()); channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, decimalASCIISampleData.toString()); // Now register the RBNB channels, and flush the rbnbChannelMap to the // DataTurbine getSource().Register(registerChannelMap); getSource().Flush(rbnbChannelMap); logger.info(getRBNBClientName() + " Sample sent to the DataTurbine: " + decimalASCIISampleData.toString()); registerChannelMap.Clear(); rbnbChannelMap.Clear(); sampleBuffer.clear(); } else { logger.debug("\t\tSource names don't match: " + sourceName + " != " + getRBNBClientName()); registerChannelMap.Clear(); rbnbChannelMap.Clear(); sampleBuffer.clear(); } } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } return !failed; }