Advertisement
KillianMills

ChatServer.java

Oct 3rd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.Date;
  4. import java.text.DateFormat;
  5. import java.text.SimpleDateFormat;
  6.  
  7. class BoundedBuffer {
  8.  
  9.     private int nextIn; // will hold the value of the tail.next of the queue/  the next free slot
  10.     private int nextOut; // will hold the value of the head of the queue/ the next slot to be taken out
  11.    
  12.     private int size = 1000; // size of buffer 1000
  13.     private int occupied = 0; // will tell us if the buffer is full or not/ the current size
  14.  
  15.     private boolean dataAvailable = false; // true if data is available for the producer
  16.     private boolean roomAvailable = true; // true if data is available for the consumer
  17.    
  18.     private String[size] buffer; // array of words to be displayed
  19.    
  20.    
  21.     // Producer's Method *******************************
  22.     public synchronized void insert(){ // need to change return type to boolean for the end
  23.         try{
  24.             while(!roomAvailable){ // go to sleep while there is a full buffer
  25.                 wait();
  26.             }
  27.         }
  28.         catch (InterruptedException e) { System.out.println("Error in insert"); }
  29.        
  30.         try{
  31.             roomAvailable = true;
  32.        
  33.             nextIn = (nextIn + 1)%size;
  34.            
  35.             occupied++; // records the current size of the buffer
  36.             dataAvailable = true;
  37.            
  38.             if(size==occupied){
  39.                 roomAvailable = false;
  40.             }
  41.            
  42.             notifyAll(); // notifys all threads synchronized
  43.         }
  44.         catch (IOException e) { System.out.println("Error in insert2"); }
  45.     }
  46.    
  47.     // Consumer's Method *******************************
  48.     public synchronized void remove(){ // need to change return type to boolean for the end
  49.         try{
  50.             while(!dataAvailable){ // go to sleep while there is no data on the buffer
  51.                 wait();
  52.             }
  53.         }
  54.         catch (InterruptedException e) { System.out.println("Error in remove"); }
  55.        
  56.         dataAvailable = true;
  57.  
  58.         nextOut = (nextOut + 1)%size;
  59.        
  60.         occupied--; // records the current size of the buffer
  61.         roomAvailable = true;
  62.        
  63.         if(occupied==0){
  64.                 dataAvailable = false;
  65.         }      
  66.        
  67.         notifyAll(); // norifys all threads synchronized
  68.     }
  69. }
  70.  
  71. // One thread per connection, this is it
  72. class ServerThread extends Thread {
  73.  
  74.     //Our thread works on this
  75.     public BoundedBuffer buff;
  76.  
  77.     // The socket passed from the creator
  78.     private Socket socket = null;
  79.  
  80.     public ServerThread(Socket socket, BoundedBuffer buff) {
  81.         this.socket = socket;
  82.         this.buff = buff;
  83.     }
  84.  
  85.     // Handle the connection
  86.     public void run() {
  87.  
  88.         try {
  89.  
  90.             // Attach a printer to the socket's output stream
  91.             PrintWriter socketOut = new PrintWriter(socket.getOutputStream(), true);
  92.  
  93.             // Get the date and time
  94.             //DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
  95.             //Date date = new Date();
  96.  
  97.             // Send the date and time to the client
  98.             socketOut.println(dateFormat.format(date));
  99.  
  100.             // Close things
  101.             socketOut.close();
  102.             socket.close();
  103.  
  104.         }
  105.         catch (IOException e) {
  106.             e.printStackTrace();
  107.         }
  108.     }
  109. }
  110.  
  111. //puts messages onto buffer Producer
  112. class Producer extends Thread{
  113.  
  114.     //Our thread works on this
  115.     public BoundedBuffer buff;
  116.  
  117.     public Producer(BoundedBuffer buff) {
  118.         this.buff = buff;
  119.     }
  120.  
  121.     public void run(){
  122.        
  123.         try{
  124.             while(true){
  125.                 buff.insert();
  126.             }
  127.         }
  128.         catch (IOException e) {
  129.             e.printStackTrace();
  130.         }
  131.     }
  132. }
  133.  
  134. //takes from buffer
  135. class Consumer extends Thread{
  136.  
  137.     //Our thread works on this
  138.     public BoundedBuffer buff;
  139.  
  140.     public Consumer(BoundedBuffer buff) {
  141.         this.buff = buff;
  142.     }
  143.  
  144.     public void run(){
  145.        
  146.         try{
  147.             while(true){
  148.                 buff.remove();
  149.             }
  150.         }
  151.         catch (IOException e) {
  152.             e.printStackTrace();
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158. // The server
  159. public class ChatServer {
  160.  
  161.     public static void main(String[] args) throws IOException {
  162.  
  163.         // The server socket, connections arrive here
  164.         ServerSocket serverSocket = null;
  165.  
  166.         try {
  167.  
  168.         // Listen on on port 7777
  169.             serverSocket = new ServerSocket(7777);
  170.  
  171.         } catch (IOException e) {
  172.  
  173.             System.err.println("Could not listen on port: 7777");
  174.             System.exit(-1);
  175.  
  176.         }
  177.        
  178.         //Create our BoundedBuffer
  179.         BoundedBuffer buff = new BoundedBuffer();
  180.        
  181.         //Create our Consumer
  182.         Consumer cthread = new Consumer(buff);
  183.  
  184.  
  185.         // Loop forever
  186.         while (true) {
  187.        
  188.         //Create our Producer
  189.         Producer pthread = new Producer(buff);
  190.        
  191.         /*
  192.          * Several things going on with this line of code:
  193.          * 1. Accept a connection (returns a new socket)
  194.          * 2. Create a new thread of type ServerThread
  195.          * 3. Call start on the new thread
  196.          */
  197.         new ServerThread(serverSocket.accept(), buff).start();
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement