/*
* File: Conexion.java
*
*
* This file is a part of MICE, a program designed for
* people with severe motor disabilities to whom it is impossible
* to use a traditional mouse. This application gives these people
* the control of the physical mouse via another type of device.
*
* Authors: Isabel Gonzalez
* Date: 2008/ 2009
*
* Company: Colegio Publico de Educacion Especial Alborada, Zaragoza
* DIIS, Universidad de Zaragoza
*
* License: Copyright (C) 2008
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*
*
*/
package DriverTeclado;
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;
public class Conexion {
static SocketChannel sc;
static ByteBuffer buffer;
static Eventos eC;
public Conexion(Eventos e) {
eC=e;
}
public static int enviar(int tecla){
ByteBuffer enviar=null;
ByteBuffer buffer = ByteBuffer.allocate(8);
IntBuffer intBuffer = buffer.asIntBuffer();
ByteBuffer mensaje = ByteBuffer.allocate(8);
IntBuffer intBufferMensaje = mensaje.asIntBuffer();
int capturar=0; // no capturo
if (tecla==-1){
intBuffer.put(0, -1);
intBuffer.put(1, -1);
}
else if (tecla==1){ // envio velicidad
intBuffer.put(0, Eventos.VELOCIDAD);
intBuffer.put(1, Eventos.speedCursor);
}
else if (tecla==2){ // envio tipo
int tipo=0;
if (Eventos.relacionVel == 0) tipo= 0; // simple
else tipo=1;
intBuffer.put(0, Eventos.TIPO);
intBuffer.put(1, tipo);
}
else if (tecla==3){ // envio velicidad
intBuffer.put(0, Eventos.RELACION);
intBuffer.put(1, Eventos.relacionVel);
}
else{
intBufferMensaje= eC.crearEvento(tecla);
intBuffer.put(0, intBufferMensaje.get(0));
intBuffer.put(1, intBufferMensaje.get(1));
}
try {
if (buffer !=null){
sc.write(buffer);
}
} catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
capturar=1;
return(capturar);
}
public static void enviarNombreDriver(){
ByteBuffer buffer2 = ByteBuffer.allocate(8);
IntBuffer intBuffer2 = buffer2.asIntBuffer();
try {
intBuffer2.put(0, 8);
intBuffer2.put(1, 1);
sc.write(buffer2);
buffer2.clear();
} catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void establecerConexion(){
try{
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);
}
}