You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Francesc Oller <fr...@upc.edu> on 2010/10/21 01:39:25 UTC

org.apache.tomcat.jni.Poll programming question

Again, this code doesn't work:

import org.apache.tomcat.jni.*;

import java.util.*;

public class ExampleAPR {
  public static void main(String[] args) {
    int err = 0;
    try {
    // Initialize APR library
      Library.initialize(null);
    		
      // Create pool
      long pool = Pool.create(0);
    		
      // Create socket
      long s = Socket.create(Socket.APR_INET,
        Socket.SOCK_STREAM,
        Socket.APR_PROTO_TCP,
        pool);
    		
      // Bind to port
      long addr = Address.info(null,
        Socket.APR_INET,
        Integer.parseInt(args[0]),
        0, pool);
      Socket.bind(s, addr);
      Socket.listen(s, 5);
      long set = Poll.create(10, pool, 0, 10);
      if ((err = Poll.add(set, s, Poll.APR_POLLIN)) != 0)
        System.out.println("Poll.add: "
          + org.apache.tomcat.jni.Error.strerror(err));
      long[] desc = new long[2];
      if ((err = Poll.poll(set, -1, desc, false)) != 0)
        System.out.println("Poll.poll: "
          + org.apache.tomcat.jni.Error.strerror(err));
      System.out.println("after poll");
			
      long ns = Socket.accept(s);
    		
      byte[] recchars = new byte[80];
      Socket.recv(ns, recchars, 0, 80);
    			
      System.out.println(new String(recchars));
    } catch(org.apache.tomcat.jni.Error e) {
      System.out.println("Error");
      e.printStackTrace();
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Three things:

1.-

call Poll.poll fails with:

Poll.poll: Unknown error 4294847295

Should block at it, right?

2.-

It is unclear to me which value to put in 4th parameter
(time to live) of:

long set = Poll.create(10, pool, 0, 10);

3.-

Socket.recv(ns, recchars, 0, -1) doesn't work. I'd to put full byte[]
length (80)

Couldn't find any sample code that could enlighten these questions.

Regards, francesc



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


Re: org.apache.tomcat.jni.Poll programming question

Posted by Francesc Oller <fr...@upc.edu>.
El ds 23 de 10 de 2010 a les 03:17 +0400, en/na Konstantin Kolinko va
escriure:

> From Poll.java of Tomcat trunk:
> 
>      * @param descriptors Array of signaled descriptors (output parameter)
>      *        The descriptor array must be two times the size of pollset.
>      *        and are populated as follows:
> 
> You create a pollset with size of 10, but pass in an array of 2 ?

You are right, I was just trying things here, but 2 should be ok since
pollset length is 1 (listening socket only)

> AprEndpoint.java:
>             return Poll.create(size, pool, 0, timeout * 1000);
> 
> See also [1]
> 
> The ttl value is apr_interval_time_t, which is measured in microseconds.
> 
> The value for "timeout" may come from
> org.apache.tomcat.util.net.SocketProperties.soTimeout, which defaults
> to 20000 (20 seconds).
> 
> Thus 20000 * 1000;

Thanks for this, I'll pass same value

> From the source code (it is network.c) it looks like -1 wouldn't be a
> valid value for nbytes argument,  so the javadoc in Socket.java is
> wrong here.

agreed

> Tomcat sources?

Of course. Key point is that it seems as if there is a severe bug in
tomcat-native-1.1.20-src/jni/native/src/poll.c:

TCN_IMPLEMENT_CALL(jint, Poll, poll)(TCN_STDARGS, jlong pollset,
                                     jlong timeout, jlongArray set,
                                     jboolean remove)
{
    ....
    if (ptime > 0 && p->max_ttl >= 0) {
        now = apr_time_now();

        /* Find the minimum timeout */
        for (i = 0; i < p->nelts; i++) {
            apr_interval_time_t t = now - p->socket_ttl[i];
            if (t >= p->max_ttl) {
                ptime = 0;
                break;
            }
            else {
                ptime = TCN_MIN(p->max_ttl - t, ptime);
            }
        }
    }
    else if (ptime < 0)
        ptime = 0;
    for (;;) {
        rv = apr_pollset_poll(p->pollset, ptime, &num, &fd);
    ....

There is no way for this code that ptime can be made negative.
apr_pollset_poll never blocks! As a short workaround I commented:

    /*else if (ptime < 0)
        ptime = 0;*/

I'm waiting for Mladen Turk to say something about this.

Best regards,
Francesc Oller
> 
> > Regards, francesc
> >
> 
> [1] http://svn.apache.org/viewvc/tomcat/native/branches/1.1.x/native/src/poll.c?view=markup
> 
> 
> Best regards,
> Konstantin Kolinko
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 



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


Re: org.apache.tomcat.jni.Poll programming question

Posted by Konstantin Kolinko <kn...@gmail.com>.
2010/10/21 Francesc Oller <fr...@upc.edu>:

I cannot say that I throughly know this. Most part of the below
answers is just from reading the sources:

>
> 1.-
>
> call Poll.poll fails with:
>
> Poll.poll: Unknown error 4294847295
>

>From Poll.java of Tomcat trunk:

     * @param descriptors Array of signaled descriptors (output parameter)
     *        The descriptor array must be two times the size of pollset.
     *        and are populated as follows:

You create a pollset with size of 10, but pass in an array of 2 ?

> Should block at it, right?
>
> 2.-
>
> It is unclear to me which value to put in 4th parameter
> (time to live) of:
>
> long set = Poll.create(10, pool, 0, 10);
>

AprEndpoint.java:
            return Poll.create(size, pool, 0, timeout * 1000);

See also [1]

The ttl value is apr_interval_time_t, which is measured in microseconds.

The value for "timeout" may come from
org.apache.tomcat.util.net.SocketProperties.soTimeout, which defaults
to 20000 (20 seconds).

Thus 20000 * 1000;


> 3.-
>
> Socket.recv(ns, recchars, 0, -1) doesn't work. I'd to put full byte[]
> length (80)
>

>From the source code (it is network.c) it looks like -1 wouldn't be a
valid value for nbytes argument,  so the javadoc in Socket.java is
wrong here.

> Couldn't find any sample code that could enlighten these questions.
>

Tomcat sources?

> Regards, francesc
>

[1] http://svn.apache.org/viewvc/tomcat/native/branches/1.1.x/native/src/poll.c?view=markup


Best regards,
Konstantin Kolinko

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