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

📄 blockingclienthandler.java

📁 一个用java编写的服务器,对于学习网络编程的人来说是个很好的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				assertionSystemExit();
			}
		} catch(Exception e) {
			logger.warning("Error "+Thread.currentThread().getName()+" - Event:"+getThreadEvent()+" - Socket:"+socket+" : "+e);
			logger.fine("StackTrace: "+getName()+"\n"+MyString.getStackTrace(e));
			if(Assertion.isEnabled()) {
				assertionSystemExit();
			}
		} catch(Error e) {
			logger.warning("Error "+Thread.currentThread().getName()+" - Event:"+getThreadEvent()+" - Socket:"+socket+" : "+e);
			logger.fine("StackTrace: "+getName()+"\n"+MyString.getStackTrace(e));
			if(Assertion.isEnabled()) {
				assertionSystemExit();
			}
		}

		synchronized(this) {
			try	{				
				if(socket!=null && socket.isClosed()==false) {
					logger.finest("Closing Socket");
					socket.close();
				}	
			} catch(Exception re) {
				logger.warning("Error closing Socket/Channel: " +re);
			}
		}//end synchronized

		willClean = true;
		returnClientData();

		boolean returnClientHandler = false;
		synchronized(lockObj) {
			returnClientHandler = checkReturnClientHandler();
		}

		if(returnClientHandler) {
			returnClientHandler(); //return to pool
		}
	}

	protected boolean checkReturnClientHandler() {
		return true;
	}

	private void processRead() throws IOException, ClassNotFoundException, AppException {
		AuthStatus authStatus = null;

		String rec = null;
		Object recObject = null; //v1.2
		byte[] recByte = null; //1.4
		
		while(connection) {
			try {
				if(dataModeIN == DataMode.STRING) {
					rec = bufferedReader.readLine();
					if(rec==null) {
						lost = true;
						break;
					}
					if(getCommunicationLogging() && authorised == true) {
						appLogger.fine("Got STRING ["+getHostAddress()+"] : "+rec);
					}
					if(authorised == false)
						authStatus = clientAuthenticationHandler.handleAuthentication(this, rec);
					else
						clientCommandHandler.handleCommand(this, rec);
				} else if(dataModeIN == DataMode.OBJECT) {
					recObject = o_in.readObject();
					if(recObject==null) {
						lost = true;
						break;
					}
					if(getCommunicationLogging() && authorised == true) {
						appLogger.fine("Got OBJECT ["+getHostAddress()+"] : "+
							recObject.toString());
					}
					if(authorised == false)
						authStatus = clientAuthenticationHandler.handleAuthentication(this, recObject);
					else
						clientObjectHandler.handleObject(this, recObject);
				} else if(dataModeIN == DataMode.BYTE) {
					rec = readBytes();
					if(rec==null) {
						lost = true;
						break;
					}
					if(getCommunicationLogging() && authorised == true) {
						appLogger.fine("Got BYTE ["+getHostAddress()+"] : "+rec);
					}
					if(authorised == false)
						authStatus = clientAuthenticationHandler.handleAuthentication(this, rec);
					else
						clientCommandHandler.handleCommand(this, rec);
				} else if(dataModeIN == DataMode.BINARY) {
					recByte = readBinary();
					if(recByte==null) {
						lost = true;
						break;
					}
					if(getCommunicationLogging() && authorised == true) {
						appLogger.fine("Got BINARY ["+getHostAddress()+"] : "+MyString.getMemInfo(recByte.length));
					}
					if(authorised == false)
						authStatus = clientAuthenticationHandler.handleAuthentication(this, recByte);
					else
						clientBinaryHandler.handleBinary(this, recByte);
				} else {
					throw new IllegalStateException("Incoming DataMode is not supported: "+dataModeIN);
				}
				updateLastCommunicationTime();

				while(authStatus==AuthStatus.FAILURE)
					authStatus = processAuthorisation();

				if(authStatus==AuthStatus.SUCCESS)
					authorised = true;
			} catch(SocketTimeoutException e) {
				handleTimeout(e);
			}
		}//end of while
	}

	protected void returnClientHandler() {
		logger.finest(getName());
		super.returnClientHandler();
	}
	
	public void setDataMode(DataMode dataMode, DataType dataType) 
			throws IOException {
		if(getDataMode(dataType)==dataMode) return;

		appLogger.fine("Setting Type:"+dataType+", Mode:"+dataMode);
		super.checkDataModeSet(dataMode, dataType);

		setDataModeBlocking(dataMode, dataType);
	}

	private void setDataModeBlocking(DataMode dataMode, DataType dataType) 
			throws IOException {
		logger.finest("ENTER");
		if(dataMode == DataMode.STRING) {
			if(dataType == DataType.OUT) {
				if(dataModeOUT == DataMode.BYTE || dataModeOUT == DataMode.BINARY) {
					dataModeOUT = dataMode;
				} else if(dataModeOUT == DataMode.OBJECT) {
					dataModeOUT = dataMode;
					o_out.flush(); o_out = null;
					b_out = new BufferedOutputStream(out);
				} else {
					Assertion.affirm(false, "Unknown DataType.OUT DataMode - "+dataModeOUT);
				}
				Assertion.affirm(b_out!=null, "BufferedOutputStream is still null!");
			} else if(dataType == DataType.IN) {
				dataModeIN = dataMode;

				if(o_in!=null) {
					if(o_in.available()!=0)
						logger.warning("Data looks to be present in ObjectInputStream");
					o_in = null;
				}
				if(b_in!=null) {
					if(b_in.available()!=0)
						logger.warning("Data looks to be present in BufferedInputStream");
					b_in = null;
				}
				bufferedReader = new BufferedReader(new InputStreamReader(in));	
				Assertion.affirm(bufferedReader!=null, "BufferedReader is still null!");
			}
		} else if(dataMode == DataMode.OBJECT) {
			if(dataType == DataType.OUT) {
				dataModeOUT = dataMode;
				if(b_out!=null) {
					b_out.flush();
					b_out = null;
				}
				o_out = new ObjectOutputStream(out);
				Assertion.affirm(o_out!=null, "ObjectOutputStream is still null!");
			} else if(dataType == DataType.IN) {
				dataModeIN = dataMode;
				if(b_in!=null) {
					if(b_in.available()!=0)
						logger.warning("Data looks to be present in BufferedInputStream");
					b_in = null;
				}
				bufferedReader = null;
				o_in = new ObjectInputStream(in); //will block
				Assertion.affirm(o_in!=null, "ObjectInputStream is still null!");
			}
		} else if(dataMode == DataMode.BYTE || dataMode == DataMode.BINARY) {
			if(dataType == DataType.OUT) {
				if(dataModeOUT == DataMode.STRING || dataModeOUT == DataMode.BYTE || 
						dataModeOUT == DataMode.BINARY) {
					dataModeOUT = dataMode;
				} else if(dataModeOUT == DataMode.OBJECT) {
					dataModeOUT = dataMode;
					if(o_out!=null) {
						o_out.flush();
						o_out = null;
					}					
					b_out = new BufferedOutputStream(out);
				} else {
					Assertion.affirm(false, "Unknown DataType.OUT - DataMode: "+dataModeOUT);
				}
				Assertion.affirm(b_out!=null, "BufferedOutputStream is still null!");
			} else if(dataType == DataType.IN) {
				dataModeIN = dataMode;
				if(o_in!=null) {
					if(o_in.available()!=0)
						logger.warning("Data looks to be present in ObjectInputStream");
					o_in = null;
				}
				bufferedReader = null;
				b_in = new BufferedInputStream(in);
				Assertion.affirm(b_in!=null, "BufferedInputStream is still null!");
			} else {
				throw new IllegalArgumentException("Unknown DataType : "+dataType);
			}
		} else {
			throw new IllegalArgumentException("Unknown DataMode : "+dataMode);
		}
	}

	protected byte[] readInputStream() throws IOException {
		return readInputStream(b_in);
	}

	public void updateInputOutputStreams() throws IOException {
		setInputStream(getSocket().getInputStream());
		setOutputStream(getSocket().getOutputStream());
	}

	public void setSocketChannel(SocketChannel socketChannel) {
		if(true) throw new IllegalStateException("Can't set in blocking mode!");
	}
	public SocketChannel getSocketChannel() {
		if(true) throw new IllegalStateException("Can't get in blocking mode!");
		return null;
	}

	public void setSelectionKey(SelectionKey selectionKey) {
		if(true) throw new IllegalStateException("Can't set in blocking mode!");
	}
	public SelectionKey getSelectionKey() {
		if(true) throw new IllegalStateException("Can't get in blocking mode!");
		return null;
	}

	public void registerForRead() throws IOException, ClosedChannelException {
		if(true) throw new IllegalStateException("Can't register in blocking mode!");
	}

	public void registerForWrite() throws IOException, ClosedChannelException {
		if(true) throw new IllegalStateException("Can't register in blocking mode!");
	}

	protected void setClientWriteHandler(ClientWriteHandler handler) {
		if(true) throw new IllegalStateException("Can't register in blocking mode!");
	}
}

⌨️ 快捷键说明

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