+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

jspspikeFixed list printf30c5bf

master
4.3 KiB124 linesraw
1import java.io.*;
2import java.util.*;
3import java.util.Scanner;
4
5import java.net.*;
6
7public class BookClient {
8    static boolean tcp = false;
9    static DatagramSocket udpSocket;
10    static Socket tcpSocket;
11    static int clientId;
12
13    final static String hostAddress = "localhost";
14
15    final static int tcpPort = 7000;
16    final static int udpPort = 8000;
17
18    static void sendData(String data) throws IOException {
19        if (tcp) {
20            PrintStream out = new PrintStream(tcpSocket.getOutputStream());
21            out.println(data);
22            System.out.println("TCP-> " + data);
23        } else {
24            InetAddress address = InetAddress.getByName(hostAddress);
25            DatagramPacket packet = new DatagramPacket(data.getBytes("UTF-8"), data.getBytes("UTF-8").length, address, udpPort);
26            udpSocket.send(packet);
27            System.out.println("UDP-> " + data);
28        }
29    }
30
31    static String getData() throws IOException {
32        if (tcp) {
33            BufferedReader in = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
34            String data = in.readLine();
35            System.out.println("TCP<- " + data);
36            return data;
37        } else {
38            byte[] data = new byte[1<<15];
39            DatagramPacket packet = new DatagramPacket(data, data.length);
40            udpSocket.receive(packet);
41            String str = new String(data, "UTF-8");
42            System.out.println("UDP<- " + str);
43            return str;
44        }
45    }
46
47    public static void main(String[] args) throws SocketException, FileNotFoundException, IOException {
48        if (args.length != 2) {
49            System.out.println(
50                "ERROR: Provide 2 arguments: commandFile, clientId");
51            System.out.println(
52                "\t(1) <command-file>: file with commands to the server");
53            System.out.println("\t(2) client id: an integer between 1..9");
54            System.exit(-1);
55        }
56
57        String commandFile = args[0];
58        clientId = Integer.parseInt(args[1]);
59
60        udpSocket = new DatagramSocket();
61
62        Scanner sc = new Scanner(new FileReader(commandFile));
63        PrintWriter writer = new PrintWriter("out_" + clientId + ".txt", "UTF-8");
64
65        while (sc.hasNextLine()) {
66            String cmd = sc.nextLine();
67            String[] tokens = cmd.split(" ");
68
69            if (tokens[0].equals("setmode")) {
70                char mode = tokens[1].charAt(0);
71                //System.out.println("SetMode: " + mode);
72
73                if (mode == 'T') {
74                    sendData("s|T");
75                    writer.println(getData());
76                    tcpSocket = new Socket(hostAddress, tcpPort);
77                    tcp = true;
78                } else if (mode == 'U') {
79                    sendData("s|U");
80                    writer.println(getData());
81                    tcpSocket.close();
82                    tcp = false;
83                }
84            } else if (tokens[0].equals("borrow")) {
85                String student = tokens[1];
86                String book = cmd.substring(cmd.indexOf('"'));
87                //System.out.println("Borrow: Student=" + student + " Book=" + book);
88
89                sendData("b|" + student + "|" + book);
90                writer.println(getData());
91            } else if (tokens[0].equals("return")) {
92                int id = Integer.parseInt(tokens[1]);
93                //System.out.println("Return: " + id);
94
95                sendData("r|" + id);
96                writer.println(getData());
97            } else if (tokens[0].equals("inventory")) {
98                //System.out.println("Inventory");
99
100                sendData("i");
101                String list = getData();
102                for (String s : list.split("\\|")) {
103                    writer.println(s);
104                }
105            } else if (tokens[0].equals("list")) {
106                String student = tokens[1];
107                //System.out.println("List: " + student);
108
109                sendData("l|" + student);
110                String list = getData();
111                for (String s : list.split("\\|")) {
112                    writer.println(s);
113                }
114            } else if (tokens[0].equals("exit")) {
115                sendData("e"); 
116                break;
117            } else {
118                System.out.println("ERROR: No such command");
119            }
120        }
121
122        writer.close();
123    }
124}