tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni Local.java
Date Fri, 24 Jun 2005 08:22:58 GMT
mturk       2005/06/24 01:22:58

  Modified:    jni      build.xml
  Added:       jni/examples/org/apache/tomcat/jni Local.properties
                        LocalServer.java
               jni/java/org/apache/tomcat/jni Local.java
  Log:
  Added Local network implementation and example.
  
  Revision  Changes    Path
  1.9       +4 -4      jakarta-tomcat-connectors/jni/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jni/build.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- build.xml	10 Jun 2005 17:15:55 -0000	1.8
  +++ build.xml	24 Jun 2005 08:22:58 -0000	1.9
  @@ -309,13 +309,13 @@
               <jvmarg value="-Djava.library.path=${tc.library.path}"/>
           </java>
       </target>
  -    <target name="bioserver-example" depends="examples">
  -        <echo message="Running another Tomcat Native SSL Server example ..."/>
  -        <java dir="${examples.dir}" classname="org.apache.tomcat.jni.BIOSSLServer"
  +    <target name="locals-example" depends="examples">
  +        <echo message="Running Tomcat Native Local Server example ..."/>
  +        <java dir="${examples.dir}" classname="org.apache.tomcat.jni.LocalServer"
                fork="yes" failonerror="${test.failonerror}">
  +            <classpath refid="examples.classpath"/>
               <env key="PATH" path="${tc.library.path}:${java.library.path}"/>
               <env key="Path" path="${tc.library.path}:${java.library.path}"/>
  -            <classpath refid="examples.classpath"/>
               <jvmarg value="-Djava.library.path=${tc.library.path}"/>
           </java>
       </target>
  
  
  
  1.1                  jakarta-tomcat-connectors/jni/examples/org/apache/tomcat/jni/Local.properties
  
  Index: Local.properties
  ===================================================================
  # Local properties
  local.max=10
  
  # For NT Pipes use something like
  local.path=\\\\.\\PIPE\\test
  
  # For Unix Sockets use
  # local.path=/tmp/testsock
  
  
  
  1.1                  jakarta-tomcat-connectors/jni/examples/org/apache/tomcat/jni/LocalServer.java
  
  Index: LocalServer.java
  ===================================================================
  package org.apache.tomcat.jni;
  
  import java.util.Properties;
  
  import java.io.*;
  import java.net.*;
  import java.lang.*;
  
  /** Local Socket server example
   *
   * @author Mladen Turk
   * @version $Revision: 1.1 $, $Date: 2005/06/24 08:22:58 $
   */
  
  public class LocalServer {
  
      public static String serverAddr = null;
      public static int serverNmax    = 0;
      public static int serverNrun    = 0;
      public static long serverPool   = 0;
  
      private static Acceptor serverAcceptor = null;
  
      private static Object threadLock = new Object();
  
      static {
  
          try {
              InputStream is = LocalServer.class.getResourceAsStream
                  ("/org/apache/tomcat/jni/Local.properties");
              Properties props = new Properties();
              props.load(is);
              is.close();
              serverAddr = props.getProperty("local.path", null);
              serverNmax = Integer.decode(props.getProperty("local.max", "0")).intValue();
          }
          catch (Throwable t) {
              ; // Nothing
          }
      }
  
      public LocalServer()
      {
          int i;
          serverPool = Pool.create(0);
          try {
              serverAcceptor = new Acceptor();
              serverAcceptor.start();
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
      }
      public static void incThreads() {
          synchronized(threadLock) {
              serverNrun++;
          }
      }
  
      public static void decThreads() {
          synchronized(threadLock) {
              serverNrun--;
          }
      }
  
      /* Acceptor thread. Listens for new connections */
      private class Acceptor extends Thread {
          private long serverSock = 0;
          private long inetAddress = 0;
          private long pool = 0;
          public Acceptor() throws Exception {
              try {
  
                  pool = Pool.create(LocalServer.serverPool);
                  System.out.println("Accepting: " +  LocalServer.serverAddr);
                  serverSock = Local.create(LocalServer.serverAddr, pool);
                  int rc = Local.bind(serverSock, inetAddress);
                  if (rc != 0) {
                    throw(new Exception("Can't create Acceptor: bind: " + Error.strerror(rc)));
                  }
                  Local.listen(serverSock, LocalServer.serverNmax);
              }
              catch( Exception ex ) {
                  ex.printStackTrace();
                  throw(new Exception("Can't create Acceptor"));
              }
          }
  
          public void run() {
              int i = 0;
              try {
                  while (true) {
                      long clientSock = Local.accept(serverSock);
                      System.out.println("Accepted id: " +  i);
  
                      Socket.timeoutSet(clientSock, 10000000);
                      Worker worker = new Worker(clientSock, i++,
                                                 this.getClass().getName());
                      LocalServer.incThreads();
                      worker.start();
                  }
              }
              catch( Exception ex ) {
                  ex.printStackTrace();
              }
          }
      }
  
      private class Worker extends Thread {
          private int workerId = 0;
          private long clientSock = 0;
          private byte [] wellcomeMsg = null;
  
          public Worker(long clientSocket, int workerId, String from) {
              this.clientSock = clientSocket;
              this.workerId = workerId;
              wellcomeMsg = ("LocalServer server id: " + this.workerId + " from " +
                             from).getBytes();
          }
  
          public void run() {
              boolean doClose = false;
              try {
                  Socket.send(clientSock, wellcomeMsg, 0, wellcomeMsg.length);
                  while (!doClose) {
                      /* Do a blocking read byte at a time */
                      byte [] buf = new byte[1];
                      byte [] msg = new byte[256];
                      int p = 0;
                      while (Socket.recv(clientSock, buf, 0, 1) == 1) {
                          if (buf[0] == '\n')
                              break;
                          else if (buf[0] == '!') {
                              doClose = true;
                              break;
                          }
                          if (p > 250)
                             break;
                          msg[p++] = buf[0];
                      }
                      if (doClose) {
                          try {
                              byte [] snd = ("Bye from worker: " + workerId).getBytes();
                              Socket.send(clientSock, snd, 0, snd.length);
                          } catch(Exception e) { }
  
                          Socket.close(clientSock);
                      }
                      else
                          Socket.send(clientSock, msg, 0, p);                
                  }
              } catch (Exception e) {
                  Socket.destroy(clientSock);
                  e.printStackTrace();
              }
              LocalServer.decThreads();
              System.out.println("Worker: " +  workerId + " finished");
          }
      }
  
  
      public static void main(String [] args) {
          try {
              Library.initialize(null);
  
              LocalServer server = new LocalServer();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
   }
  
  
  
  1.1                  jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni/Local.java
  
  Index: Local.java
  ===================================================================
  /*
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  
  package org.apache.tomcat.jni;
  
  /** Local socket
   *
   * @author Mladen Turk
   * @version $Revision: 1.1 $, $Date: 2005/06/24 08:22:58 $
   */
  
  public class Local {
  
      /**
       * Create a socket.
       * @param path The address of the new socket.
       * @param cont The parent pool to use
       * @return The new socket that has been set up.
       */
      public static native long create(String path, long cont)
          throws Exception;
  
      /**
       * Bind the socket to its associated port
       * @param sock The socket to bind
       * @param sa The socket address to bind to
       * This may be where we will find out if there is any other process
       *      using the selected port.
       */
      public static native int bind(long sock, long sa);
  
      /**
       * Listen to a bound socket for connections.
       * @param sock The socket to listen on
       * @param backlog The number of outstanding connections allowed in the sockets
       *                listen queue.  If this value is less than zero, for NT pipes
       *                the number of instances is unlimite.
       *
       */
      public static native int listen(long sock, int backlog);
  
      /**
       * Accept a new connection request
       * @param sock The socket we are listening on.
       * @param pool The pool for the new socket.
       * @return  A copy of the socket that is connected to the socket that
       *          made the connection request.  This is the socket which should
       *          be used for all future communication.
       */
      public static native long accept(long sock)
          throws Exception;
  
      /**
       * Issue a connection request to a socket either on the same machine
       * or a different one.
       * @param sock The socket we wish to use for our side of the connection
       * @param sa The address of the machine we wish to connect to.
       *           Unused for NT Pipes.
       */
      public static native int connect(long sock, long sa);
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org


Mime
View raw message