Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorpersistencia;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.InetAddress;
- import java.net.MulticastSocket;
- import java.net.SocketTimeoutException;
- import java.util.Scanner;
- import static servidorpersistencia.ServidorPersistencia.IDProcesso;
- import static servidorpersistencia.ServidorPersistencia.grupo;
- import static servidorpersistencia.ServidorPersistencia.recv;
- /**
- *
- * @author giovanne.gaspareto
- */
- public class Cliente {
- static InetAddress grupo;
- static MulticastSocket s;
- static DatagramPacket msg;
- static DatagramPacket recv;
- private static Scanner keyboard;
- static final String GROUP_IP = "224.0.0.1";
- static final int MULTICAST_PORT = 2000;
- static int IDProcesso = 0;
- static String mensagem = "";
- static int numeroProcessosGrupo = 0;
- static int tempoRandom = 0;
- static int idProcessoNaRegiaoCritica = 0;
- static int idRecebido = 0;
- static int proximo = 0;
- static int escolha = 0;
- static String mensagemTeclado = "";
- static int idBusca = 0;
- static boolean continuarExecucao = true;
- static String mensagemTecladoEntrada = "";
- static final String CLIENTE_M = "M";
- static final String DIVISOR_STRING = ";";
- static final String BUSCA = "b";
- static final String INSERCAO = "i";
- static final String RESPOSTA = "resposta";
- public static void setUp(){
- try {
- //System.setProperty("java.net.preferIPv4Stack", "true"); // somente pra mac no wifi
- grupo=InetAddress.getByName(GROUP_IP);
- s=new MulticastSocket(MULTICAST_PORT);
- System.err.println("Entrando no grupo ...");
- s.joinGroup(grupo);
- System.err.println("Ok.");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void defineIDProcesso(){
- keyboard = new Scanner(System.in);
- System.out.println("Informe o ID(i) deste cliente Mi: ");
- IDProcesso = keyboard.nextInt();
- }
- public static String receiveMessageGroup(){
- byte[] buf=new byte[1000];
- recv=new DatagramPacket(buf, buf.length);
- try {
- s.setSoTimeout(10);
- s.setLoopbackMode(false);
- // System.out.println("timeout no recevei sem nada: " + s.getSoTimeout());
- s.receive(recv);
- byte[] dest=new byte[recv.getLength()];
- System.arraycopy(recv.getData(), 0, dest, 0, dest.length);
- String mensagemRecebida = new String(recv.getData(),recv.getOffset(),recv.getLength());
- // System.err.println("Mensagen recebida: "+ mensagemRecebida);
- return mensagemRecebida;
- } catch (SocketTimeoutException ex) {
- // Logger.getLogger(ProcessoSender.class.getName()).log(Level.SEVERE, null, ex);
- return "x;0;0";
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return "x;0;0";
- }
- }
- public static void sendMessageGroup(String msgStr) {
- msg=new DatagramPacket(msgStr.getBytes(), msgStr.length(),grupo, 2000);
- // System.out.println("Enviando mensagem para o grupo ...");
- try {
- s.send(msg);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static int lerEscolha(){
- keyboard = new Scanner(System.in);
- System.out.println("[Cliente " + IDProcesso+ "]Menu:\n1: Inserir texto \n2: Buscar texto por ID \n3: Desconectar cliente");
- escolha = keyboard.nextInt();
- return escolha;
- }
- public static String lerTeclado(){
- keyboard = new Scanner(System.in);
- System.out.println("Informe o texto a ser enviado: ");
- mensagemTeclado = keyboard.nextLine();
- return mensagemTeclado;
- }
- public static int lerIDBusca(){
- keyboard = new Scanner(System.in);
- System.out.println("Informe o K que sera buscado dentro da base de dados: ");
- escolha = keyboard.nextInt();
- return escolha;
- }
- public static void main(String[] args) throws IOException{
- setUp();
- defineIDProcesso();
- // mensagem = CLIENTE_M+DIVISOR_STRING+IDProcesso;
- // sendMessageGroup(mensagem);
- boolean aguardandoResposta = true;
- String opcao;
- String[] partes = new String[3];
- while(continuarExecucao){
- switch(lerEscolha()){
- case 1:
- mensagemTecladoEntrada = lerTeclado();
- mensagem = CLIENTE_M + DIVISOR_STRING + IDProcesso + DIVISOR_STRING + INSERCAO + DIVISOR_STRING +mensagemTecladoEntrada; // M;1;textoaqui
- sendMessageGroup(mensagem);
- aguardandoResposta = true;
- while(aguardandoResposta){
- mensagem = receiveMessageGroup();
- partes = mensagem.split(DIVISOR_STRING);
- opcao = partes[0];
- if(opcao.equals(RESPOSTA)){
- System.out.println(partes[2]);
- aguardandoResposta = false;
- }
- }
- break;
- case 2:
- idBusca = lerIDBusca();
- mensagem = CLIENTE_M + DIVISOR_STRING + IDProcesso + DIVISOR_STRING + BUSCA + DIVISOR_STRING + idBusca;
- sendMessageGroup(mensagem);
- System.out.println(receiveMessageGroup());
- aguardandoResposta = true;
- while(aguardandoResposta){
- mensagem = receiveMessageGroup();
- partes = mensagem.split(DIVISOR_STRING);
- opcao = partes[0];
- if(opcao.equals(RESPOSTA)){
- System.out.println(partes[2]);
- aguardandoResposta = false;
- }
- }
- break;
- case 3:
- System.err.println("Cliente "+IDProcesso+ " encerrando conexao...");
- s.leaveGroup(grupo);
- continuarExecucao = false;
- default:
- System.err.println("Case Default leitura do teclado");
- break;
- }
- }
- System.out.println("Cliente desconectado");
- System.exit(0);
- }
- }
Advertisement