Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.Date;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- class BoundedBuffer {
- private int nextIn; // will hold the value of the tail.next of the queue/ the next free slot
- private int nextOut; // will hold the value of the head of the queue/ the next slot to be taken out
- private int size = 1000; // size of buffer 1000
- private int occupied = 0; // will tell us if the buffer is full or not/ the current size
- private boolean dataAvailable = false; // true if data is available for the producer
- private boolean roomAvailable = true; // true if data is available for the consumer
- private String[size] buffer; // array of words to be displayed
- // Producer's Method *******************************
- public synchronized void insert(){ // need to change return type to boolean for the end
- try{
- while(!roomAvailable){ // go to sleep while there is a full buffer
- wait();
- }
- }
- catch (InterruptedException e) { System.out.println("Error in insert"); }
- try{
- roomAvailable = true;
- nextIn = (nextIn + 1)%size;
- occupied++; // records the current size of the buffer
- dataAvailable = true;
- if(size==occupied){
- roomAvailable = false;
- }
- notifyAll(); // notifys all threads synchronized
- }
- catch (IOException e) { System.out.println("Error in insert2"); }
- }
- // Consumer's Method *******************************
- public synchronized void remove(){ // need to change return type to boolean for the end
- try{
- while(!dataAvailable){ // go to sleep while there is no data on the buffer
- wait();
- }
- }
- catch (InterruptedException e) { System.out.println("Error in remove"); }
- dataAvailable = true;
- nextOut = (nextOut + 1)%size;
- occupied--; // records the current size of the buffer
- roomAvailable = true;
- if(occupied==0){
- dataAvailable = false;
- }
- notifyAll(); // norifys all threads synchronized
- }
- }
- // One thread per connection, this is it
- class ServerThread extends Thread {
- //Our thread works on this
- public BoundedBuffer buff;
- // The socket passed from the creator
- private Socket socket = null;
- public ServerThread(Socket socket, BoundedBuffer buff) {
- this.socket = socket;
- this.buff = buff;
- }
- // Handle the connection
- public void run() {
- try {
- // Attach a printer to the socket's output stream
- PrintWriter socketOut = new PrintWriter(socket.getOutputStream(), true);
- // Get the date and time
- //DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
- //Date date = new Date();
- // Send the date and time to the client
- socketOut.println(dateFormat.format(date));
- // Close things
- socketOut.close();
- socket.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //puts messages onto buffer Producer
- class Producer extends Thread{
- //Our thread works on this
- public BoundedBuffer buff;
- public Producer(BoundedBuffer buff) {
- this.buff = buff;
- }
- public void run(){
- try{
- while(true){
- buff.insert();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //takes from buffer
- class Consumer extends Thread{
- //Our thread works on this
- public BoundedBuffer buff;
- public Consumer(BoundedBuffer buff) {
- this.buff = buff;
- }
- public void run(){
- try{
- while(true){
- buff.remove();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- // The server
- public class ChatServer {
- public static void main(String[] args) throws IOException {
- // The server socket, connections arrive here
- ServerSocket serverSocket = null;
- try {
- // Listen on on port 7777
- serverSocket = new ServerSocket(7777);
- } catch (IOException e) {
- System.err.println("Could not listen on port: 7777");
- System.exit(-1);
- }
- //Create our BoundedBuffer
- BoundedBuffer buff = new BoundedBuffer();
- //Create our Consumer
- Consumer cthread = new Consumer(buff);
- // Loop forever
- while (true) {
- //Create our Producer
- Producer pthread = new Producer(buff);
- /*
- * Several things going on with this line of code:
- * 1. Accept a connection (returns a new socket)
- * 2. Create a new thread of type ServerThread
- * 3. Call start on the new thread
- */
- new ServerThread(serverSocket.accept(), buff).start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement