|
| 1 | +package dp.module2.lab3.client; |
| 2 | + |
| 3 | +import dp.module2.lab1.store.Film; |
| 4 | +import dp.module2.lab1.store.Genre; |
| 5 | +import dp.module2.lab3.common.QueryType; |
| 6 | + |
| 7 | +import java.io.DataInputStream; |
| 8 | +import java.io.DataOutputStream; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.Socket; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Scanner; |
| 14 | + |
| 15 | +public class Client { |
| 16 | + private Socket socket; |
| 17 | + private DataOutputStream out; |
| 18 | + private DataInputStream in; |
| 19 | + |
| 20 | + public Client(String ip, int port) throws IOException { |
| 21 | + socket = null; |
| 22 | + |
| 23 | + while (true) { |
| 24 | + try { |
| 25 | + System.out.println("Connecting to server..."); |
| 26 | + socket = new Socket(ip, port); |
| 27 | + System.out.println("Connected"); |
| 28 | + break; |
| 29 | + } catch (IOException e) { |
| 30 | + int timeout = 5000; |
| 31 | + System.out.printf("Failed. Waiting %d ms...%n", timeout); |
| 32 | + try { |
| 33 | + Thread.sleep(timeout); |
| 34 | + } catch (InterruptedException ex) { |
| 35 | + ex.printStackTrace(); |
| 36 | + Thread.currentThread().interrupt(); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + in = new DataInputStream(socket.getInputStream()); |
| 42 | + out = new DataOutputStream(socket.getOutputStream()); |
| 43 | + } |
| 44 | + |
| 45 | + public boolean addGenre(Genre genre) throws IOException { |
| 46 | + sendQuery(QueryType.ADD_GENRE, List.of(genre.getName())); |
| 47 | + return in.readInt() == 0; |
| 48 | + } |
| 49 | + |
| 50 | + public boolean deleteGenre(Genre genre) throws IOException { |
| 51 | + sendQuery(QueryType.DELETE_GENRE, List.of(genre.getName())); |
| 52 | + return in.readInt() == 0; |
| 53 | + } |
| 54 | + |
| 55 | + public boolean addFilm(Film film) throws IOException { |
| 56 | + sendQuery(QueryType.ADD_FILM, film.toList()); |
| 57 | + return in.readInt() == 0; |
| 58 | + } |
| 59 | + |
| 60 | + public boolean deleteFilm(Film film) throws IOException { |
| 61 | + sendQuery(QueryType.ADD_FILM, film.toList()); |
| 62 | + return in.readInt() == 0; |
| 63 | + } |
| 64 | + |
| 65 | + public boolean updateFilm(Film oldFilm, Film newFilm) throws IOException { |
| 66 | + List<String> arguments = oldFilm.toList(); |
| 67 | + arguments.addAll(newFilm.toList()); |
| 68 | + sendQuery(QueryType.UPDATE_FILM, arguments); |
| 69 | + |
| 70 | + return in.readInt() == 0; |
| 71 | + } |
| 72 | + |
| 73 | + public int countFilmsByGenre(Genre genre) throws IOException { |
| 74 | + sendQuery(QueryType.COUNT_FILMS_BY_GENRE, List.of(genre.getName())); |
| 75 | + int count = in.readInt(); |
| 76 | + int status = in.readInt(); |
| 77 | + |
| 78 | + return count; |
| 79 | + } |
| 80 | + |
| 81 | + public Film getFilmByName(Film film) throws IOException { |
| 82 | + sendQuery(QueryType.GET_FILM_BY_NAME, film.toList()); |
| 83 | + Film resultFilm = Film.parseFilm(in); |
| 84 | + int status = in.readInt(); |
| 85 | + return resultFilm; |
| 86 | + } |
| 87 | + |
| 88 | + public List<Film> getFilmsByGenre(Genre genre) throws IOException { |
| 89 | + sendQuery(QueryType.GET_FILMS_BY_GENRE, genre.toList()); |
| 90 | + int count = in.readInt(); |
| 91 | + |
| 92 | + List<Film> films = new ArrayList<>(); |
| 93 | + |
| 94 | + for (int i = 0; i < count; i++) { |
| 95 | + films.add(Film.parseFilm(in)); |
| 96 | + } |
| 97 | + |
| 98 | + int status = in.readInt(); |
| 99 | + |
| 100 | + return films; |
| 101 | + } |
| 102 | + |
| 103 | + public List<Genre> getGenres() throws IOException { |
| 104 | + sendQuery(QueryType.GET_GENRES, List.of()); |
| 105 | + int count = in.readInt(); |
| 106 | + |
| 107 | + List<Genre> genres = new ArrayList<>(); |
| 108 | + |
| 109 | + for (int i = 0; i < count; i++) { |
| 110 | + genres.add(Genre.parseGenre(in)); |
| 111 | + } |
| 112 | + |
| 113 | + int status = in.readInt(); |
| 114 | + |
| 115 | + return genres; |
| 116 | + } |
| 117 | + |
| 118 | + public void disconnect() throws IOException { |
| 119 | + System.out.println("Disconnected from server"); |
| 120 | + socket.close(); |
| 121 | + } |
| 122 | + |
| 123 | + private boolean sendQuery(QueryType type, List<String> arguments) throws IOException { |
| 124 | + out.writeUTF(type.name()); |
| 125 | + |
| 126 | + for (String argument : arguments) { |
| 127 | + out.writeUTF(argument); |
| 128 | + } |
| 129 | + |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) throws IOException { |
| 134 | + Client client = null; |
| 135 | + try { |
| 136 | + client = new Client("localhost", 2710); |
| 137 | + } catch (IOException e) { |
| 138 | + e.printStackTrace(); |
| 139 | + } |
| 140 | + |
| 141 | + Scanner sc = new Scanner(System.in); |
| 142 | + String genreName; |
| 143 | + String filmName; |
| 144 | + float duration; |
| 145 | + |
| 146 | + |
| 147 | + while (true) { |
| 148 | +// System.out.print("Enter a genre: "); |
| 149 | +// genreName = sc.nextLine(); |
| 150 | +// |
| 151 | +// if (Objects.equals(genreName, "stop")) { |
| 152 | +// break; |
| 153 | +// } |
| 154 | +// |
| 155 | +// client.addGenre(new Genre(genreName)); |
| 156 | + |
| 157 | + for(Genre genre : client.getGenres()) { |
| 158 | + System.out.println(genre); |
| 159 | + } |
| 160 | + |
| 161 | + System.out.print("Enter a film name: "); |
| 162 | + filmName = sc.nextLine(); |
| 163 | +// |
| 164 | + if (filmName == "stop") |
| 165 | + break; |
| 166 | +// |
| 167 | +// System.out.print("Enter a film duration: "); |
| 168 | +// duration = Float.parseFloat(sc.nextLine()); |
| 169 | +// System.out.print("Enter a film genre: "); |
| 170 | +// genreName = sc.nextLine(); |
| 171 | +// |
| 172 | +// client.addFilm(new Film(filmName, duration, genreName)); |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + try { |
| 179 | + client.disconnect(); |
| 180 | + } catch (IOException e) { |
| 181 | + e.printStackTrace(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments