Package Manejo

Source Code of Manejo.esperarEventos

/*
* Conexion.java
*
* Created on 2 de julio de 2009, 18:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package Manejo;

import Tipos.Tablero;
import java.nio.IntBuffer;
import java.nio.channels.SocketChannel;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.io.*;
import java.nio.ByteBuffer;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.nio.charset.*;
import java.lang.StringBuffer;

//import org.apache.log4j.*;

public class Conexion {
    //static Logger log;
    static SocketChannel sc;
    static ByteBuffer buffer;
    public static esperarEventos espEv;
   
   
    public Conexion() {
    
        buffer = ByteBuffer.allocate(100);
    }
     public static void enviar(int cod, int param){
        System.out.println("Enviar: cod: "+ cod + " param: " + param);
       
        ByteBuffer buffer = ByteBuffer.allocate(8);
        IntBuffer intBuffer = buffer.asIntBuffer();
       
        intBuffer.put(0, cod);
        intBuffer.put(1, param);
        try {
                if (buffer !=null){
                    sc.write(buffer);
            
                }
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
    }
    public static void enviarNombreDriver(){
     ByteBuffer buffer2 = ByteBuffer.allocate(8);
     IntBuffer intBuffer2 = buffer2.asIntBuffer();
      
        try {
            intBuffer2.put(0, 8);
            intBuffer2.put(1, 0);
            //buffer.put(nombre.getBytes());
            //buffer2.flip();
            sc.write(buffer2);
            buffer2.clear();
        } catch(Exception e) {
            e.printStackTrace();
            System.exit(0);
        }
    
    }
   
    public static void establecerConexion(){
        try{
            buffer = ByteBuffer.allocate(100);
            sc = SocketChannel.open();          
            System.err.println("making conection ...");
            sc.socket().connect(new InetSocketAddress("localhost",17223));
            System.err.println("connection established");
           
        } catch(Exception e) {
            e.printStackTrace();
        }    
    }
   
     public static void finalizarConexion(){
      enviar(-1,-1);      
       
    }

    public static void esperarEventos() {
        espEv = new esperarEventos(sc);
        espEv.start();
    }

  
   
}

class esperarEventos extends Thread {
    SocketChannel sc;
    public static Tablero t = Tablero.getInstance();
    ByteBuffer buffer = ByteBuffer.allocate(8);
    IntBuffer intBuffer = buffer.asIntBuffer();
   public esperarEventos (SocketChannel socket) {
      sc=socket;
   }
    public void run(){
        String result;
        int tipoCod= t.MOVIMIENTO;
        int parametro= 0;
        try {
            while(true){
                System.out.println("-----> RV Espera evento........");
                buffer.clear();
                int numBytesRead =sc.read(buffer);
                if (numBytesRead == -1) {
                    // No more bytes can be read from the channel
                    sc.close();
                } else {
                    tipoCod = intBuffer.get(0);
                    parametro = intBuffer.get(1);
                    System.out.println("-----> RV recibe: "+ intBuffer.get(0) +" y " + intBuffer.get(1));
                    //boton izquierdo
                    if ((tipoCod==t.MENSAJERATON)&& (parametro==00)){ // inicio
                        System.out.println("Izquierdo parar");
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                           
                            t.ppal.manejoclic.parar();
                        }
                    }
                    else if ((tipoCod==t.MENSAJERATON)&& (parametro==10)){ // fin
//                        t.ocupadoIzda=false;
                        System.out.println("Izquierdo iniciar");
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                            t.ppal.manejoclic.iniciar();
                        }
                    }
                    // boton medio
                    else if ((tipoCod==t.MENSAJERATON)&& (parametro==01)){ // inicio
//                        t.ocupadoMedio= true;
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                            System.out.println("Medio parar");
                            t.ppal.manejoclic.parar();
                        }
                    }
                    else if ((tipoCod==t.MENSAJERATON)&& (parametro==11)){ // fin
//                        t.ocupadoMedio= false;
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                            System.out.println("Medio iniciar");
                            t.ppal.manejoclic.iniciar();
                        }
                    }
                    // boton derecho
                    else if ((tipoCod==t.MENSAJERATON)&& (parametro==02)){ // inicio
//                        t.ocupadoDcha= true;
                        System.out.println("Derecho parar");
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                            t.ppal.manejoclic.parar();
                        }
                    }
                    else if ((tipoCod==t.MENSAJERATON)&& (parametro==12)){ // fin
//                        t.ocupadoDcha= false;
                        System.out.println("Derecho iniciar");
                        if (t.ppal.manejoclic!=null){
                            //t.ocupadoIzda=true;
                            // ya no envia a java
                           
                            t.ppal.manejoclic.iniciar();
                        }
                    }
                   
                    buffer.flip();
                    buffer.clear();
               
              
                }
            }
        }
        catch (IOException e) {
            // Connection may have been closed
        }
    }
}



TOP

Related Classes of Manejo.esperarEventos

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.