/*
* File: Conexion.java
*
* This file is a part of driver Voz for 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 Gonz�lez
* Date: 2008/2009
*
* Company: Colegio P�blico de Educaci�n Especial Alborada, Zaragoza
* DIIS, Universidad de Zaragoza
*
* License: Copyright (C) 2009
*
* 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 drivercontrolvoz;
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 Logger log;
static SocketChannel sc;
static ByteBuffer buffer;
public Conexion() {
buffer = ByteBuffer.allocate(100);
}
public static void enviar(int cod, int param){
System.out.println("****VOZ Envia: cod: "+ cod + " param: " + param);
ByteBuffer buffer = ByteBuffer.allocate(8);
IntBuffer intBuffer = buffer.asIntBuffer();
intBuffer.put(0, cod);
intBuffer.put(1, param);
// System.out.println("Dev crear evento: "+ intBufferMensaje.get(0)+ " "+ intBufferMensaje.get());
// System.out.println("Dev luego: "+ intBuffer.get(0)+ " "+ intBuffer.get(1));
try {
//buffer.put(mensaje.getBytes());
if (buffer !=null){
//buffer.flip();
sc.write(buffer);
//buffer.clear();
}
} 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, 2);
//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");
// enviarNombreDriver();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void finalizarConexion(){
enviar(-1,-1);
}
}