+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
73968d3
master
1import java .io .*; 2import java .net .*; 3import java .util .*; 4import java .util .Map .Entry ; 5import java .util .concurrent .atomic .*; 6import java .util .concurrent .locks .*; 7 8public class BookServer { 9static LinkedHashMap <String ,AtomicInteger >books ; 10static HashMap <Integer ,String >checkedOut ; 11static HashMap <String ,HashSet <Integer >>students ; 12static ServerSocket tcpSocket ; 13static DatagramSocket udpSocket ; 14static ReentrantLock lock ; 15static int bookID =1 ; 16 17public static void main (String []args )throws Exception { 18final int tcpPort =7000 ; 19final int udpPort =8000 ; 20if (args .length !=1 ) { 21System .out .println ( 22"ERROR: Provide 1 argument: input file containing initial inventory" ); 23System .exit (-1 ); 24 } 25books =new LinkedHashMap <>(); 26checkedOut =new LinkedHashMap <>(); 27students =new LinkedHashMap <>(); 28lock =new ReentrantLock (); 29 30try { 31BufferedReader reader =new BufferedReader (new FileReader (args [0 ])); 32String line =reader .readLine (); 33while (line !=null ) { 34int quote =line .lastIndexOf ('"' ); 35books .put (line .substring (0 ,quote +1 ), 36new AtomicInteger ( 37Integer .parseInt (line .substring (quote +2 )))); 38line =reader .readLine (); 39 } 40reader .close (); 41 }catch (IOException e ) {e .printStackTrace (); } 42 43System .out .println ("Initial book count:" ); 44for (Entry e :books .entrySet ()) {System .out .println (" " +e ); } 45 46tcpSocket =new ServerSocket (tcpPort ); 47udpSocket =new DatagramSocket (udpPort ); 48 49new Thread (() -> { 50while (true ) { 51try { 52new Thread (new TcpHandler (tcpSocket .accept ())).start (); 53 }catch (Exception e ) {} 54 } 55 }).start (); 56 57new Thread (() -> { 58byte []buf =new byte [1 <<15 ]; 59DatagramPacket packet ; 60while (true ) { 61try { 62packet =new DatagramPacket (buf ,buf .length ); 63udpSocket .receive (packet ); 64new Thread (new UdpHandler (udpSocket ,packet )).start (); 65 }catch (Exception e ) {} 66 } 67 }).start (); 68 } 69 70static String handleCommand (String s ) { 71String []parts =s .split ("\\|" ); 72switch (parts [0 ].charAt (0 )) { 73case 's' : 74return "The communication mode is set to " + 75 (parts [1 ].charAt (0 ) =='T' ?"TCP" :"UDP" ); 76case 'b' : 77String student =parts [1 ]; 78String book =parts [2 ]; 79AtomicInteger count =books .get (book ); 80if (count ==null ) { 81return "Request Failed - We do not have this book" ; 82 } 83lock .lock (); 84if (count .get () ==0 ) { 85lock .unlock (); 86return "Request Failed - Book not available" ; 87 } 88count .getAndDecrement (); 89HashSet <Integer >ids =students .get (student ); 90if (ids ==null ) { 91ids =new HashSet <>(); 92students .put (student ,ids ); 93 } 94ids .add (bookID ); 95checkedOut .put (bookID ,book ); 96lock .unlock (); 97return "Your request has been approved, " +bookID ++ +" " + 98student +" " +book ; 99case 'r' : 100int id =Integer .parseInt (parts [1 ]); 101lock .lock (); 102book =checkedOut .remove (id ); 103if (book ==null ) { 104lock .unlock (); 105return "" +id +" not found, no such borrow record" ; 106 } 107for (HashSet <Integer >temp :students .values ()) {temp .remove (id ); } 108books .get (book ).getAndIncrement (); 109lock .unlock (); 110return "" +id +" is returned" ; 111case 'l' : 112student =parts [1 ]; 113lock .lock (); 114ids =students .get (student ); 115if (ids ==null ) { 116lock .unlock (); 117return "No record found for " +student ; 118 } 119String result ="" ; 120for (Integer i :ids ) { 121if (result .length () >0 ) {result +="|" ; } 122result +=i +" " +checkedOut .get (i ); 123 } 124lock .unlock (); 125return result ; 126case 'i' : 127result ="" ; 128lock .lock (); 129for (Entry e :books .entrySet ()) { 130if (result .length () >0 ) {result +="|" ; } 131result +=e .getKey () +" " +e .getValue (); 132 } 133lock .unlock (); 134return result ; 135case 'e' : 136PrintWriter out =null ; 137try {out =new PrintWriter ("inventory.txt" ); 138 }catch (Exception e ) {e .printStackTrace ();} 139for (Entry e :books .entrySet ()) { 140out .println (e .getKey () +" " +e .getValue ()); 141 } 142out .close (); 143 } 144return "RIP" ; 145 } 146 147static class TcpHandler implements Runnable { 148Socket socket ; 149public TcpHandler (Socket socket ) {this .socket =socket ; } 150public void run () { 151try { 152BufferedReader in =new BufferedReader ( 153new InputStreamReader (socket .getInputStream ())); 154PrintWriter out = 155new PrintWriter (socket .getOutputStream (),true ); 156String command =in .readLine (); 157while (command !=null ) { 158out .println (handleCommand (command )); 159System .out .println (); 160System .out .println ("Books: " +books ); 161System .out .println ("Students: " +students ); 162System .out .println ("Checkedout: " +checkedOut ); 163command =in .readLine (); 164 } 165 }catch (Exception e ) {// e.printStackTrace(); 166 } 167 } 168 } 169 170static class UdpHandler implements Runnable { 171DatagramSocket socket ; 172DatagramPacket packet ; 173public UdpHandler (DatagramSocket socket ,DatagramPacket packet ) { 174this .socket =socket ; 175this .packet =packet ; 176 } 177public void run () { 178try { 179packet .getData (); 180String command = 181new String (packet .getData (),0 ,packet .getLength ()); 182packet .setData (handleCommand (command ).getBytes ("UTF-8" )); 183udpSocket .send (packet ); 184 }catch (Exception e ) {e .printStackTrace (); } 185 } 186 } 187}