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