Foros del Web » Programación para mayores de 30 ;) » Java »

Selector Sockets java

Estas en el tema de Selector Sockets java en el foro de Java en Foros del Web. Hola a todos, estoy programando una aplicacion cliente/servidor en java 1.5 con selectors. En mi programa servidor, lo que hago es registrar 2 canales. Por ...
  #1 (permalink)  
Antiguo 11/02/2009, 06:34
 
Fecha de Ingreso: febrero-2008
Mensajes: 43
Antigüedad: 16 años, 2 meses
Puntos: 0
Selector Sockets java

Hola a todos,

estoy programando una aplicacion cliente/servidor en java 1.5 con selectors.
En mi programa servidor, lo que hago es registrar 2 canales.
Por uno quiero leer lo que me mandan de ese canal, y reenviarlo al otro canal.
Cuando recibo una conexion, me guardo el ServerSocket en una tabla hash, de manera que al leer algo (isReadable), recorro la tabla, y compruebo mi SelectableChannel con los que tengo en la tabla, porque quiero coger el otro canal para escribir en él. Esta comparación es lo que me da problemas.
¿Es esta una forma acertada de escribir por el otro canal? ¿Cómo podría escribir de otra forma?
Si está bien, cómo podria obtener el otro canal?


Código:
import java.io.*;
import java.lang.StringBuffer;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
import java.util.HashMap;

import java.util.Iterator;

public class PServer {

    int port = 17223; //4001;
    Selector selector = null;
    ServerSocketChannel selectableChannel = null;
    int keysAdded = 0;
    HashMap hashMap;
    static String QUIT_SERVER = "quit";
    static String SHUTDOWN = "shutdown";

    public PServer() {
    }

    public PServer( int port ) {
        this.port = port;
    }

    public void initialize()

    throws IOException {
        this.selector = SelectorProvider.provider().openSelector();
        this.selectableChannel = ServerSocketChannel.open();
	this.selectableChannel.configureBlocking(false);
	InetSocketAddress isa = new InetSocketAddress(this.port);
	this.selectableChannel.socket().bind(isa);
        
        
        
        System.out.println("Inicializacion completada");
        
        hashMap = new HashMap();
        
        
          }

    
    
    public void finalize()

    throws IOException {
        this.selectableChannel.close();
        this.selector.close();
    }

    public void acceptConnections() throws IOException, InterruptedException {
      System.out.println("Accept connection");
      SelectionKey acceptKey = this.selectableChannel.register( this.selector, SelectionKey.OP_ACCEPT );
      System.out.println("Despues de registrar");
      System.err.println("Acceptor loop..." );
      
        while (( this.keysAdded = acceptKey.selector().select()) > 0 ) {
            Set readyKeys = this.selector.selectedKeys();
            Iterator i = readyKeys.iterator();
 
            while (i.hasNext()) {
                SelectionKey key = (SelectionKey)i.next();
                i.remove();
                if ( key.isAcceptable() ) {
                    ServerSocketChannel nextReady = (ServerSocketChannel)key.channel();
                    System.err.println("Acceptable");
                    SocketChannel channel = nextReady.accept();
                    channel.configureBlocking( false );
                    SelectionKey readKey = channel.register( this.selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE  );
                    readKey.attach( new ChannelCallback( channel ) );
                    System.err.println("acceptable: "+ channel.toString());
                    SocketChannel ch=channel.socket().getChannel();
                    hashMap.put(ch,1);
                    
                }

                else if ( key.isReadable() ) {
                    SelectableChannel nextReady = (SelectableChannel) key.channel();
                    System.err.println("Readable");
                    this.readMessage( (ChannelCallback)  key.attachment() );
                                      
                    Iterator iterator = hashMap.keySet().iterator();
                    System.err.println(nextReady.toString());
                    while( iterator. hasNext() ){
                        System.err.println( iterator.next() );
                        if (iterator.next()==(SocketChannel)nextReady){
                            System.err.println("iguales los canales");
                        }
                       
                    }   
                    
                    
                    
//                    ChannelCallback callback = (ChannelCallback) key.attachment();
//                    String message = nextReady.toString();
//                    System.err.println("mensaje: "+ message);
//                    System.err.println("cap message:" + message.length());
//                    ByteBuffer buf = ByteBuffer.wrap( message.getBytes() );
//                    System.err.println("cap buffer:" + buf.capacity());
//                    int nbytes = callback.getChannel().write( buf );
//                    System.err.println("fin de Writable");                   
          }
        }

    }
    }
 

    public void writeMessage( SocketChannel channel, String message) throws IOException {
        ByteBuffer buf = ByteBuffer.wrap( message.getBytes()  );
        int nbytes = channel.write( buf );
        System.err.println( "Wrote " + nbytes + " to channel." );
    }

 

    static final int BUFSIZE = 8;

    public String decode( ByteBuffer byteBuffer ) throws CharacterCodingException {
        Charset charset = Charset.forName( "us-ascii" );
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = decoder.decode( byteBuffer );
        String result = charBuffer.toString();
        return result;
    }

    public void readMessage( ChannelCallback callback) throws IOException, InterruptedException {
        ByteBuffer byteBuffer = ByteBuffer.allocate( BUFSIZE );
        int nbytes = callback.getChannel().read( byteBuffer );
        byteBuffer.flip();
        String result = this.decode( byteBuffer );
        System.err.println(result);
        if ( result.indexOf( "exit" ) >= 0 ) callback.getChannel().close();
        else if ( result.indexOf( "shutdown" ) >= 0 ) {
            callback.getChannel().close();
            throw new InterruptedException();
        }
        else {
            callback.append( result.toString() );
            //If we are done with the line then we execute the callback.
            if ( result.indexOf( "\n" ) >= 0 )
                callback.execute();
        }
    }

    public class ChannelCallback {
        private SocketChannel channel;
        private StringBuffer buffer;

        public ChannelCallback( SocketChannel channel ) {
            this.channel = channel;
            this.buffer = new StringBuffer();
        }

        public void execute() throws IOException {
            System.err.println("Execute");
            System.err.println(this.buffer.toString() );
            writeMessage( this.channel, this.buffer.toString() );
            buffer = new StringBuffer();
        }

        public SocketChannel getChannel() {
            return this.channel;
        }

        public void append( String values ) {
            buffer.append( values );
        }

    }

 

    public static void main( String[] args ) {

       PServer nbServer = new PServer();

        try {
            nbServer.initialize();
        } catch ( IOException e ) {
            e.printStackTrace();
            System.exit( -1 );
        }

        try {
            nbServer.acceptConnections();
        }

        catch ( IOException e ) {
            e.printStackTrace();
        }

        catch ( InterruptedException e ) {
        }

    }
Gracias :)
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:43.