public void mainloop() { while (!client.isConnected()) { try { System.out.println("Connecting to " + hostname + ":" + port); client.connect(hostname, port); } catch (IOException e) { } if (!client.isConnected()) { System.out.println("Couldn't connect. waiting"); try { Thread.sleep(1000); } catch (InterruptedException e) { run = false; System.exit(-1); } } } // Load the header information in one go into a bytebuffer byte[] rawbytebuf = new byte[BUFFERSIZE]; int n = 0; try { n = headerReader.read(rawbytebuf); } catch (IOException e) { e.printStackTrace(); } // Byte-buffer used to parse the byte-stream. Force native ordering ByteBuffer hdrBuf = ByteBuffer.wrap(rawbytebuf, 0, n); hdrBuf.order(ByteOrder.nativeOrder()); Header hdr = new Header(hdrBuf); if (VERB > 0) { System.out.println("Sending header: " + hdr.toString()); } hdr.nSamples = 0; // reset number of samples to 0 try { client.putHeader(hdr); } catch (IOException e) { e.printStackTrace(); } // Interval between sending samples to the buffer int pktSamples = hdr.nChans * blockSize; // number data samples in each buffer packet int pktBytes = pktSamples * DataType.wordSize[hdr.dataType]; int nsamp = 0; // sample counter int nblk = 0; int nevent = 0; byte[] samples = new byte[pktBytes]; // Size of the event header: type,type_numel,val,val_numel,sample,offset,duration,bufsz int evtHdrSz = DataType.wordSize[DataType.INT32] * 8; byte[] evtRawBuf = new byte[BUFFERSIZE]; // buffer to hold complete event structure // Byte-buffer used to parse the byte-stream. Force native ordering ByteBuffer evtBuf = ByteBuffer.wrap(evtRawBuf); evtBuf.order(ByteOrder.nativeOrder()); int payloadSz = 0; int evtSample = 0; int evtSz = 0; long sample_ms = 0; long starttime_ms = java.lang.System.currentTimeMillis(); long elapsed_ms = 0; long print_ms = 0; // Now do the data forwarding boolean eof = false; while (!eof && run) { // The run switch allows control of stopping the thread and getting out of the // loop // Read one buffer packets worth of samples // increment the cursor position if (VERB > 0 && elapsed_ms > print_ms + 500) { print_ms = elapsed_ms; System.out.println( nblk + " " + nsamp + " " + nevent + " " + (elapsed_ms / 1000) + " (blk,samp,event,sec)\r"); } // read and write the samples try { n = dataReader.read(samples); } catch (IOException e) { e.printStackTrace(); } if (n <= 0) { eof = true; break; } // stop if run out of samples try { client.putRawData(blockSize, hdr.nChans, hdr.dataType, samples); } catch (IOException e) { e.printStackTrace(); } // update the sample count nsamp += blockSize; while (evtSample <= nsamp) { if (evtSample > 0) { // send the current event try { client.putRawEvent(evtRawBuf, 0, evtSz); } catch (IOException e) { e.printStackTrace(); } nevent++; } // read the next event try { n = eventReader.read(evtRawBuf, 0, evtHdrSz); // read the fixed size header } catch (IOException e) { e.printStackTrace(); } if (n <= 0) { eof = true; break; } evtSample = ((ByteBuffer) evtBuf.position(4 * 4)).getInt(); // sample index for this event payloadSz = ((ByteBuffer) evtBuf.position(4 * 7)).getInt(); // payload size for this event evtSz = evtHdrSz + payloadSz; // read the variable part try { n = eventReader.read(evtRawBuf, evtHdrSz, payloadSz); } catch (IOException e) { e.printStackTrace(); } if (n <= 0) { eof = true; break; } // print the event we just read if (VERB > 1) { ByteBuffer tmpev = ByteBuffer.wrap(evtRawBuf, 0, evtSz); tmpev.order(evtBuf.order()); BufferEvent evt = new BufferEvent(tmpev); System.out.println("Read Event: " + evt); } } // sleep until the next packet should be send OR EOF /*when to send the next sample */ sample_ms = (long) ((float) (nsamp * 1000) / hdr.fSample / (float) speedup); elapsed_ms = java.lang.System.currentTimeMillis() - starttime_ms; // current time if (sample_ms > elapsed_ms) try { Thread.sleep(sample_ms - elapsed_ms); } catch (InterruptedException e) { e.printStackTrace(); } nblk++; } stop(); }
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) { java.lang.System.arraycopy(src, srcPos, dest, destPos, length); if (length > 0) RobotMonitor.incrementBytecodes(length); }
/** * DOCUMENT ME! * * @param out DOCUMENT ME! */ public void WriteHeaders(OutputStream out) throws IOException { int i; int j; int index; int offset; int length; int[] tempArray; // the SOI marker byte[] SOI = {(byte) 0xFF, (byte) 0xD8}; WriteMarker(SOI, out); // The order of the following headers is quiet inconsequential. // the JFIF header byte[] JFIF = new byte[18]; JFIF[0] = (byte) 0xff; JFIF[1] = (byte) 0xe0; JFIF[2] = (byte) 0x00; JFIF[3] = (byte) 0x10; JFIF[4] = (byte) 0x4a; JFIF[5] = (byte) 0x46; JFIF[6] = (byte) 0x49; JFIF[7] = (byte) 0x46; JFIF[8] = (byte) 0x00; JFIF[9] = (byte) 0x01; JFIF[10] = (byte) 0x00; JFIF[11] = (byte) 0x00; JFIF[12] = (byte) 0x00; JFIF[13] = (byte) 0x01; JFIF[14] = (byte) 0x00; JFIF[15] = (byte) 0x01; JFIF[16] = (byte) 0x00; JFIF[17] = (byte) 0x00; WriteArray(JFIF, out); // Comment Header String comment = new String(); comment = JpegObj.getComment(); length = comment.length(); byte[] COM = new byte[length + 4]; COM[0] = (byte) 0xFF; COM[1] = (byte) 0xFE; COM[2] = (byte) ((length >> 8) & 0xFF); COM[3] = (byte) (length & 0xFF); java.lang.System.arraycopy(JpegObj.Comment.getBytes(), 0, COM, 4, JpegObj.Comment.length()); WriteArray(COM, out); // The DQT header // 0 is the luminance index and 1 is the chrominance index byte[] DQT = new byte[134]; DQT[0] = (byte) 0xFF; DQT[1] = (byte) 0xDB; DQT[2] = (byte) 0x00; DQT[3] = (byte) 0x84; offset = 4; for (i = 0; i < 2; i++) { DQT[offset++] = (byte) ((0 << 4) + i); tempArray = (int[]) dct.quantum[i]; for (j = 0; j < 64; j++) { DQT[offset++] = (byte) tempArray[jpegNaturalOrder[j]]; } } WriteArray(DQT, out); // Start of Frame Header byte[] SOF = new byte[19]; SOF[0] = (byte) 0xFF; SOF[1] = (byte) 0xC0; SOF[2] = (byte) 0x00; SOF[3] = (byte) 17; SOF[4] = (byte) JpegObj.Precision; SOF[5] = (byte) ((JpegObj.imageHeight >> 8) & 0xFF); SOF[6] = (byte) ((JpegObj.imageHeight) & 0xFF); SOF[7] = (byte) ((JpegObj.imageWidth >> 8) & 0xFF); SOF[8] = (byte) ((JpegObj.imageWidth) & 0xFF); SOF[9] = (byte) JpegObj.NumberOfComponents; index = 10; for (i = 0; i < SOF[9]; i++) { SOF[index++] = (byte) JpegObj.CompID[i]; SOF[index++] = (byte) ((JpegObj.HsampFactor[i] << 4) + JpegObj.VsampFactor[i]); SOF[index++] = (byte) JpegObj.QtableNumber[i]; } WriteArray(SOF, out); // The DHT Header byte[] DHT1; // The DHT Header byte[] DHT2; // The DHT Header byte[] DHT3; // The DHT Header byte[] DHT4; int bytes; int temp; int oldindex; int intermediateindex; length = 2; index = 4; oldindex = 4; DHT1 = new byte[17]; DHT4 = new byte[4]; DHT4[0] = (byte) 0xFF; DHT4[1] = (byte) 0xC4; for (i = 0; i < 4; i++) { bytes = 0; DHT1[index++ - oldindex] = (byte) ((int[]) Huf.bits.elementAt(i))[0]; for (j = 1; j < 17; j++) { temp = ((int[]) Huf.bits.elementAt(i))[j]; DHT1[index++ - oldindex] = (byte) temp; bytes += temp; } intermediateindex = index; DHT2 = new byte[bytes]; for (j = 0; j < bytes; j++) { DHT2[index++ - intermediateindex] = (byte) ((int[]) Huf.val.elementAt(i))[j]; } DHT3 = new byte[index]; java.lang.System.arraycopy(DHT4, 0, DHT3, 0, oldindex); java.lang.System.arraycopy(DHT1, 0, DHT3, oldindex, 17); java.lang.System.arraycopy(DHT2, 0, DHT3, oldindex + 17, bytes); DHT4 = DHT3; oldindex = index; } DHT4[2] = (byte) (((index - 2) >> 8) & 0xFF); DHT4[3] = (byte) ((index - 2) & 0xFF); WriteArray(DHT4, out); // Start of Scan Header byte[] SOS = new byte[14]; SOS[0] = (byte) 0xFF; SOS[1] = (byte) 0xDA; SOS[2] = (byte) 0x00; SOS[3] = (byte) 12; SOS[4] = (byte) JpegObj.NumberOfComponents; index = 5; for (i = 0; i < SOS[4]; i++) { SOS[index++] = (byte) JpegObj.CompID[i]; SOS[index++] = (byte) ((JpegObj.DCtableNumber[i] << 4) + JpegObj.ACtableNumber[i]); } SOS[index++] = (byte) JpegObj.Ss; SOS[index++] = (byte) JpegObj.Se; SOS[index++] = (byte) ((JpegObj.Ah << 4) + JpegObj.Al); WriteArray(SOS, out); }
public static void main(String[] args) throws Exception { boolean isInteractive = false; classUrl = MynaInstaller.class.getResource("MynaInstaller.class").toString(); isJar = (classUrl.indexOf("jar") == 0); if (!isJar) { System.err.println("Installer can only be run from inside a Myna distribution war file"); System.exit(1); } Thread.sleep(1000); Console console = System.console(); String response = null; CommandLineParser parser = new PosixParser(); // create the Options Options options = new Options(); options.addOption( "c", "context", true, "Webapp context. Must Start with \"/\" Default: " + webctx); options.addOption("h", "help", false, "Displays help."); options.addOption( "w", "webroot", true, "Webroot to use. Will be created if webroot/WEB-INF does not exist. Default: " + webroot); options.addOption( "l", "logfile", true, "Log file to use. Will be created if it does not exist. Default: ./<context>.log"); options.addOption( "s", "servername", true, "Name of this instance. Will also be the name of the init script. Defaults to either \"myna\" or the value of <context> if defined"); // options.addOption( "P", "purpose", true, "Purpose of the Server, such as DEV,PROD,TRAIN, etc. // Defaults to DEV" ); options.addOption("p", "port", true, "HTTP port. Set to 0 to disable HTTP. Default: " + port); options.addOption( "sp", "ssl-port", true, "SSL (HTTPS) port. Set to 0 to disable SSL, Default: 0"); options.addOption( "ks", "keystore", true, "keystore path. Default: <webroot>/WEB-INF/myna/myna_keystore"); options.addOption("ksp", "ks-pass", true, "keystore password. Default: " + ksPass); options.addOption("ksa", "ks-alias", true, "certificate alias. Default: " + ksAlias); modeOptions.add("upgrade"); modeOptions.add("install"); options.addOption( "m", "mode", true, "Mode: one of " + modeOptions.toString() + ". \n" + "\"upgrade\": Upgrades myna installation in webroot and exits. " + "\"install\": Unpacks to webroot, and installs startup files"); options.addOption( "u", "user", true, "User to own and run the Myna installation. Only applies to unix installs. Default: nobody"); HelpFormatter formatter = new HelpFormatter(); String cmdSyntax = "java -jar myna-X.war -m <mode> [options]"; try { CommandLine line = parser.parse(options, args); Option option; if (args.length == 0) { formatter.printHelp(cmdSyntax, options); response = console.readLine("\nContinue with Interactive Install? (y/N)"); if (response.toLowerCase().equals("y")) { isInteractive = true; } else System.exit(1); } // Help if (line.hasOption("help")) { formatter.printHelp(cmdSyntax, options); System.exit(1); } // mode if (line.hasOption("mode")) { mode = line.getOptionValue("mode"); if (!modeOptions.contains(mode)) { System.err.println( "Invalid Arguments. Reason: Mode must be in " + modeOptions.toString()); formatter.printHelp(cmdSyntax, options); System.exit(1); } } else if (isInteractive) { option = options.getOption("mode"); console.printf("\n" + option.getDescription()); do { response = console.readLine("\nEnter " + option.getLongOpt() + "(" + mode + "): "); if (!response.isEmpty()) mode = response; } while (!modeOptions.contains(mode)); } // webroot if (line.hasOption("webroot")) { webroot = line.getOptionValue("webroot"); } else if (isInteractive) { option = options.getOption("webroot"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + webroot + "): "); if (!response.isEmpty()) webroot = response; } // port if (line.hasOption("port")) { port = Integer.parseInt(line.getOptionValue("port")); } else if (isInteractive && mode.equals("install")) { option = options.getOption("port"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + port + "): "); if (!response.isEmpty()) port = Integer.parseInt(response); } // context if (line.hasOption("context")) { webctx = line.getOptionValue("context"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("context"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + webctx + "): "); if (!response.isEmpty()) webctx = response; } if (!webctx.startsWith("/")) { webctx = "/" + webctx; } // servername (depends on context) if (!webctx.equals("/")) { serverName = webctx.substring(1); } if (line.hasOption("servername")) { serverName = line.getOptionValue("servername"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("servername"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + serverName + "): "); if (!response.isEmpty()) serverName = response; } // user if (line.hasOption("user")) { user = line.getOptionValue("user"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("user"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + user + "): "); if (!response.isEmpty()) user = response; } // logfile logFile = "myna.log"; if (!webctx.equals("/")) { logFile = webctx.substring(1) + ".log"; } if (line.hasOption("logfile")) { logFile = line.getOptionValue("logfile"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("logfile"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "path(" + logFile + "): "); if (!response.isEmpty()) logFile = response; } // ssl-port if (line.hasOption("ssl-port")) { sslPort = Integer.parseInt(line.getOptionValue("ssl-port")); } else if (isInteractive && mode.equals("install")) { option = options.getOption("ssl-port"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + sslPort + "): "); if (!response.isEmpty()) sslPort = Integer.parseInt(response); } // ks-pass if (line.hasOption("ks-pass")) { ksPass = line.getOptionValue("ks-pass"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("ks-pass"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + ksPass + "): "); if (!response.isEmpty()) ksPass = response; } // ks-alias if (line.hasOption("ks-alias")) { ksAlias = line.getOptionValue("ks-alias"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("ks-alias"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + ksAlias + "): "); if (!response.isEmpty()) ksAlias = response; } // keystore String appBase = new File(webroot).getCanonicalPath(); if (keystore == null) { keystore = appBase + "/WEB-INF/myna/myna_keystore"; } if (line.hasOption("keystore")) { keystore = line.getOptionValue("keystore"); } else if (isInteractive && mode.equals("install")) { option = options.getOption("keystore"); console.printf("\n" + option.getDescription()); response = console.readLine("\nEnter " + option.getLongOpt() + "(" + keystore + "): "); if (!response.isEmpty()) keystore = response; } javaOpts = line.getArgList(); } catch (ParseException exp) { System.err.println("Invalid Arguments. Reason: " + exp.getMessage()); formatter.printHelp(cmdSyntax, options); System.exit(1); } if (isInteractive) { System.out.println("\nProceeed with the following settings?:\n"); System.out.println("mode = " + mode); System.out.println("webroot = " + webroot); if (mode.equals("install")) { System.out.println("port = " + port); System.out.println("context = " + webctx); System.out.println("servername = " + serverName); System.out.println("user = "******"logfile = " + logFile); System.out.println("ssl-port = " + sslPort); System.out.println("ks-pass = "******"ks-alias = " + ksAlias); System.out.println("keystore = " + keystore); } response = console.readLine("Continue? (Y/n)"); if (response.toLowerCase().equals("n")) System.exit(1); } File wrFile = new File(webroot); webroot = wrFile.toString(); if (mode.equals("install")) { adminPassword = console.readLine("\nCreate an Admin password for this installation: "); } // unpack myna if necessary if (!wrFile.exists() || mode.equals("upgrade") || mode.equals("install")) { upgrade(wrFile); } if (mode.equals("install")) { File propertiesFile = new File(wrFile.toURI().resolve("WEB-INF/classes/general.properties")); FileInputStream propertiesFileIS = new FileInputStream(propertiesFile); Properties generalProperties = new Properties(); generalProperties.load(propertiesFileIS); propertiesFileIS.close(); if (!adminPassword.isEmpty()) { org.jasypt.util.password.StrongPasswordEncryptor cryptTool = new org.jasypt.util.password.StrongPasswordEncryptor(); generalProperties.setProperty("admin_password", cryptTool.encryptPassword(adminPassword)); } generalProperties.setProperty("instance_id", serverName); generalProperties.store( new java.io.FileOutputStream(propertiesFile), "Myna General Properties"); String javaHome = System.getProperty("java.home"); webroot = new File(webroot).getCanonicalPath(); if (serverName.length() == 0) serverName = "myna"; if (java.lang.System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { if (!new File(logFile).isAbsolute()) { logFile = new File(wrFile.toURI().resolve("WEB-INF/" + logFile)).toString(); } File templateFile = new File( wrFile.toURI().resolve("WEB-INF/myna/install/windows/update_myna_service.cmd")); String initScript = FileUtils.readFileToString(templateFile) .replaceAll("\\{webctx\\}", webctx) .replaceAll("\\{server\\}", Matcher.quoteReplacement(serverName)) .replaceAll("\\{webroot\\}", Matcher.quoteReplacement(webroot)) .replaceAll("\\{logfile\\}", Matcher.quoteReplacement(logFile)) .replaceAll("\\{javahome\\}", Matcher.quoteReplacement(javaHome)) .replaceAll("\\{port\\}", new Integer(port).toString()) .replaceAll("\\{sslPort\\}", new Integer(sslPort).toString()) .replaceAll("\\{keystore\\}", Matcher.quoteReplacement(keystore)) .replaceAll("\\{ksPass\\}", Matcher.quoteReplacement(ksPass)) .replaceAll("\\{ksAlias\\}", Matcher.quoteReplacement(ksAlias)); File scriptFile = new File(wrFile.toURI().resolve("WEB-INF/myna/install/update_myna_service.cmd")); FileUtils.writeStringToFile(scriptFile, initScript); // Runtime.getRuntime().exec("cmd /c start " + scriptFile.toString()).waitFor(); System.out.println( "\nInstalled Service 'Myna App Server " + serverName + "' the following settings:\n"); System.out.println( "\nInit script '" + scriptFile + "' created with the following settings:\n"); System.out.println("memory=256MB"); System.out.println("serverName=" + serverName); System.out.println("javaHome=" + javaHome); System.out.println("context=" + webctx); System.out.println("port=" + port); System.out.println("myna_home=" + webroot); System.out.println("logfile=" + logFile); System.out.println("sslPort=" + sslPort); System.out.println("keyStore=" + keystore); System.out.println("ksPass="******"ksAlias=" + ksAlias); System.out.println( "\nEdit and and run the command file in " + scriptFile + " to update this service"); } else { String curUser = java.lang.System.getProperty("user.name"); if (!curUser.equals("root")) { System.out.println("Install mode must be run as root."); System.exit(1); } if (!new File(logFile).isAbsolute()) { logFile = new File(wrFile.toURI().resolve("WEB-INF/" + logFile)).toString(); } File templateFile = new File(wrFile.toURI().resolve("WEB-INF/myna/install/linux/init_script")); String initScript = FileUtils.readFileToString(templateFile) .replaceAll("\\{webctx\\}", webctx) .replaceAll("\\{server\\}", serverName) .replaceAll("\\{user\\}", user) .replaceAll("\\{webroot\\}", webroot) .replaceAll("\\{javahome\\}", javaHome) .replaceAll("\\{logfile\\}", logFile) .replaceAll("\\{port\\}", new Integer(port).toString()) .replaceAll("\\{sslPort\\}", new Integer(sslPort).toString()) .replaceAll("\\{keystore\\}", keystore) .replaceAll("\\{ksPass\\}", ksPass) .replaceAll("\\{ksAlias\\}", ksAlias); File scriptFile = new File(wrFile.toURI().resolve("WEB-INF/myna/install/" + serverName)); FileUtils.writeStringToFile(scriptFile, initScript); if (new File("/etc/init.d").exists()) { exec("chown -R " + user + " " + webroot); exec("chown root " + scriptFile.toString()); exec("chmod 700 " + scriptFile.toString()); exec("cp " + scriptFile.toString() + " /etc/init.d/"); System.out.println( "\nInit script '/etc/init.d/" + serverName + "' created with the following settings:\n"); } else { System.out.println( "\nInit script '" + scriptFile + "' created with the following settings:\n"); } System.out.println("user="******"memory=256MB"); System.out.println("server=" + serverName); System.out.println("context=" + webctx); System.out.println("port=" + port); System.out.println("myna_home=" + webroot); System.out.println("logfile=" + logFile); System.out.println("sslPort=" + sslPort); System.out.println("keyStore=" + keystore); System.out.println("ksPass="******"ksAlias=" + ksAlias); System.out.println("\nEdit this file to customize startup behavior"); } } }