|
| 1 | +package Client; |
| 2 | + |
| 3 | +import Client.Exception.ConnectionException; |
| 4 | +import Common.Request; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.net.InetSocketAddress; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.channels.SocketChannel; |
| 10 | +import java.util.Scanner; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by IntelliJ IDEA. |
| 14 | + * |
| 15 | + * @author Behruz Mansurov |
| 16 | + */ |
| 17 | +public class Client { |
| 18 | + private static final String HOST = "localhost"; |
| 19 | + private static final String ADDRESS = "127.0.0.1"; |
| 20 | + private int PORT; |
| 21 | + ByteBuffer byteBuffer = ByteBuffer.allocate(10240); |
| 22 | + byte[] bytes; |
| 23 | + |
| 24 | + public void sendRequest(Request request) { |
| 25 | + try { |
| 26 | + ByteArrayOutputStream baos; |
| 27 | + ObjectOutputStream oos = null; |
| 28 | + try (SocketChannel channel = connect(new InetSocketAddress(HOST, PORT))) { |
| 29 | + baos = new ByteArrayOutputStream(); |
| 30 | + oos = new ObjectOutputStream(baos); |
| 31 | + oos.writeObject(request); |
| 32 | + ByteBuffer buffer = ByteBuffer.allocate(baos.size()); |
| 33 | + buffer.put(baos.toByteArray()); |
| 34 | + buffer.flip(); |
| 35 | + channel.write(buffer); |
| 36 | + int read = channel.read(byteBuffer); |
| 37 | + if (read != -1) { |
| 38 | + bytes = new byte[read]; |
| 39 | +// byteBuffer.remaining(); |
| 40 | + for (int i = 0; i < read; i++) { |
| 41 | + bytes[i] = byteBuffer.get(i); |
| 42 | + } |
| 43 | + System.out.println(bytes.toString()); |
| 44 | + } |
| 45 | + } finally { |
| 46 | + oos.close(); |
| 47 | + } |
| 48 | + }catch (ConnectionException e) { |
| 49 | + System.out.println("Ошибка подключения к серверу"); |
| 50 | + } catch (IOException e) { |
| 51 | + System.out.println("Ошибка!!!"); |
| 52 | + e.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public SocketChannel connect(InetSocketAddress inetSocketAddress) throws ConnectionException { |
| 57 | + int countReconnect = 0; |
| 58 | + SocketChannel socketChannel; |
| 59 | + try { |
| 60 | + socketChannel = SocketChannel.open(inetSocketAddress); |
| 61 | + } catch (IOException e) { |
| 62 | + System.out.println("Не удается подключиться к серверу. Ожидайте ..."); |
| 63 | + while (true) { |
| 64 | + try { |
| 65 | + countReconnect++; |
| 66 | + Thread.sleep(1000); |
| 67 | + socketChannel = SocketChannel.open(inetSocketAddress); |
| 68 | + break; |
| 69 | + } catch (InterruptedException interruptedException) { |
| 70 | + interruptedException.printStackTrace(); |
| 71 | + } catch (IOException ex) { |
| 72 | + System.out.println("Не удается подключиться к серверу. Ожидайте ..."); |
| 73 | + if(countReconnect > 4) { |
| 74 | + Scanner scanner = new Scanner(System.in); |
| 75 | + String command; |
| 76 | + do { |
| 77 | + System.out.println("Хотите возобновить подключение?\n" + |
| 78 | + "Введите Y/N: "); |
| 79 | + command = scanner.nextLine().toUpperCase(); |
| 80 | + } while (!(command.equals("Y") | command.equals("N"))); |
| 81 | + if(command.equals("Y")) { |
| 82 | + countReconnect++; |
| 83 | + } else { |
| 84 | + throw new ConnectionException(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return socketChannel; |
| 91 | + } |
| 92 | +} |
0 commit comments