⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverpool.java

📁 线程通信
💻 JAVA
字号:

import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class ServerPool{
	
	
	static final int POOL_MIN=3;		//min total size of pool(default pool size)	
	static final int POOL_MAX=7;		//max total size of pool
	static final int POOL_MIN_LEFT=0;	//min ready size of pool
	static final int POOL_MAX_LEFT=3;	//max ready size of pool
	static final int GROW=3;			//default change size of pool
	
	private Counter counter=new Counter();
	private ServerSocket serverSocket;
	private int localPort;
	
	private JProgressBar active=null;
	private JProgressBar total=null;
	
	private Library lib;
	

	
	public ServerPool(Library lib,JProgressBar active,JProgressBar total){
		try{
			serverSocket=new ServerSocket(0);
			localPort=serverSocket.getLocalPort();
			System.out.println("server running on"+localPort);
		}catch(IOException e){
			e.printStackTrace();
			throw new RuntimeException();
		}
		this.active=active;
		this.total=total;
		this.lib=lib;
		
		
		//initialise the pool size to POOL_MIN
		synchronized(counter){
			grow(POOL_MIN);
			counter.incTotal(POOL_MIN);
		}
	}
	
	public void grow(int amt){
		for(int i=0;i<amt;i++){
			new Handler(this);
		}
	}
	
	public int getLocalPort(){
		return localPort;
	}
	
	class Counter{
		
		private int numActive=0; //total size of pool so far
		private int numTotal=0;	 //active size of pool so far 
		
		
		public void incActive(){
			try{numActive++;}
			finally{resetActive();}
			//although synchronized counter has no deadlock danger
			//need to keep the display and the actual figure in pace
		}
		
		public void incTotal(int amt){
			try{numTotal+=amt;}
			finally{resetTotal();}
		}
		
		public void decActive(){
			try{numActive--;}
			finally{resetActive();}
		}
		
		public void decTotal(){
			try{numTotal--;}
			finally{resetTotal();}
		}
		
		private void resetActive(){
			active.setValue(numActive);
			active.setString(Integer.toString(numActive));
		}
		
		private void resetTotal(){
			total.setValue(numTotal);
			total.setString(Integer.toString(numTotal));
			
		}
		
		public boolean needGrow(){
			return((numTotal-numActive)<=POOL_MIN_LEFT)
			&&(numTotal<=POOL_MAX);
		}
		
		public int growBy(){
			int diff=POOL_MAX-numTotal;
			return diff>=GROW?GROW:diff;
		}
		
		public boolean needShrink(){
			return ((numTotal-numActive)>=POOL_MAX_LEFT);
			
		}
	}
	
	
	class Handler extends Thread{
		
		private ServerPool pool;

		
		
		public Handler(ServerPool pool){
			this.pool=pool;
			start();
		}
		
		
		
		public void run(){
			
			while(true){
				try{
					Socket s=serverSocket.accept();
					
					synchronized(counter){
						counter.incActive();
						if(counter.needGrow()){
							int amt=counter.growBy();
							pool.grow(amt);
							counter.incTotal(amt);
						}
					}
					
					ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
					ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
				
					
					System.out.println("reading request");
					Request req=(Request)ois.readObject();
			
					try{
						sleep(1000);//time delay
					}catch(Exception e){}
					System.out.println("processing request");
					Response r=new Response("");
					int type=req.getType();
					List l=req.getReq();
			
					
		
					if(type==Request.AUTHEN){
						boolean pass=lib.authenicate(req.getBorrowerId());
						r=new Response("authenication"+(pass?"pass":"fail"));
						r.setPass(pass);
					}
							
					else if(type==Request.BORROW||type==Request.RETUN||type==Request.SEARCH)
						processBook(type,req,r,l);
					else if(type==Request.CHECKLOAN){
						r=new Response("checkloan request\n");
						Borrower b=lib.getBorrower(req.getBorrowerId());
						r.addMess(b.checkLoan());
					}
		
					System.out.println("sending response");
					oos.writeObject(r);
					oos.flush();
					
					
					synchronized(counter){
						if(counter.needShrink()){
							counter.decTotal();
							counter.decActive();
							return;
						}
						counter.decActive();
						System.out.println("Done");
					}
					
				}catch(Exception e){
					System.out.println("connection exception");
					e.printStackTrace();
					throw new RuntimeException();
				}
			}
		}
					
					
		//Request.BORROW||Request.RETUN||Request.SEARCH
		private void processBook(int type,Request req,Response r,List l){
			boolean isBorrow=(type==Request.BORROW);
			boolean isRetun=(type==Request.RETUN);
			boolean isSearch=(type==Request.SEARCH);
			
			Borrower b=lib.getBorrower(req.getBorrowerId());	
			for(int i=0;i<l.size();i++){
				try{
					String title=(String)l.get(i);
					Book book=lib.getBook(title);
					if(book==null)
						throw new LibraryException(title+" not exist");
					else if(isBorrow){
						r.addMess("borrow request for "+title);
						b.borrow(book);
						r.addMess(" successful\n");
					}
					else if(isRetun){
						r.addMess("retun request for "+title);
						b.retun(book);
						r.addMess(" successful\n");
					}
					else if(isSearch)
						r.addMess("search request for "+title+"\n"+book.toString()+"\n");
					r.addBook(book.getId());
					
				}catch(LibraryException e){
					r.addMess(e.getMessage()+"\n");
				}
			}
		}
		
	
		
		
	}
	
}
	
	
			
			
						
						
						
							
							
							
							
							
						
							
				
			
			
		
		
		
		
		

			
			
			





				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -