networkservercontrolimpl.java

来自「derby database source code.good for you.」· Java 代码 · 共 2,300 行 · 第 1/5 页

JAVA
2,300
字号
			consolePropertyMessage("DRDA_Usage"+i+".I");		} catch (Exception e) {}	// ignore exceptions - there shouldn't be any	}	/**	 * Set connection pool parameters for a database	 *	 * @param database	database parameters applied to	 * @param min		minimum number of connections, if 0, pooled connections not used	 *						if -1, use default							 * @param max		maximum number of connections, if 0, pooled connections 	 *						created when no free connection available, if -1, 	 *						use default	 * @param host		machine network server is running on, if null, localhost is used	 * @param portNumber	port number server is to use, if <= 0, default port number	 *			is used	 *	 * @exception Exception	throws an exception if an error occurs	 */	protected void setConnectionPoolParameters(String database, int min, int max,		String host, int portNumber)		throws Exception	{		consolePropertyMessage("DRDA_NotImplemented.S", "conpool");	}	/**	 * Set default connection pool parameters 	 *	 * @param min		minimum number of connections, if 0, pooled connections not used	 *						if -1, use default	 * @param max		maximum number of connections, if 0, pooled connections 	 *						created when no free connection available	 *						if -1, use default	 * @param host		machine network server is running on, if null, localhost is used	 * @param portNumber	port number server is to use, if <= 0, default port number	 *			is used	 *	 * @exception Exception	throws an exception if an error occurs	 */	protected void setConnectionPoolParameters(int min, int max, String host, 			int portNumber) throws Exception	{		consolePropertyMessage("DRDA_NotImplemented.S", "conpool");	}	/**	 * Connect to  network server and set connection maxthread parameter	 *	 * @param max		maximum number of connections, if 0, connections 	 *						created when no free connection available	 *						if -1, use default	 *	 * @exception Exception	throws an exception if an error occurs	 */	public void netSetMaxThreads(int max) throws Exception	{		setUpSocket();		writeCommandHeader(COMMAND_MAXTHREADS);		commandOs.writeInt(max);		send();		readResult();		int newval = readInt();		consolePropertyMessage("DRDA_MaxThreadsChange.I",  					new Integer(newval).toString());	}	/**	 * Set network server connection timeslice parameter	 *	 * @param timeslice	amount of time given to each session before yielding to 	 *						another session, if 0, never yield. if -1, use default.	 *	 * @exception Exception	throws an exception if an error occurs	 */	public void netSetTimeSlice(int timeslice)		throws Exception	{		setUpSocket();		writeCommandHeader(COMMAND_TIMESLICE);		commandOs.writeInt(timeslice);		send();		readResult();		int newval = readInt();		consolePropertyMessage("DRDA_TimeSliceChange.I", 									   new Integer(newval).toString());	}	/**	 * Get current properties	 *	 * @return Properties object containing properties	 * @exception Exception	throws an exception if an error occurs	 */	public Properties getCurrentProperties() 		throws Exception	{		setUpSocket();		writeCommandHeader(COMMAND_PROPERTIES);		send();		byte [] val = readBytesReply("DRDA_PropertyError.S");		Properties p = new Properties();		try {			ByteArrayInputStream bs = new ByteArrayInputStream(val);			p.load(bs);		} catch (IOException io) {			consolePropertyMessage("DRDA_IOException.S", 						io.getMessage());		}		return p;	}	/**	 * Set a thread name to be something that is both meaningful and unique (primarily	 * for debugging purposes).	 *	 * @param thrd An instance of a Thread object that still has its default	 *  thread name (as generated by the jvm Thread constructor).  This should	 *  always be of the form "Thread-N", where N is a unique thread id	 *  generated by the jvm.  Ex. "Thread-0", "Thread-1", etc.	 *	 * @return The received thread's name has been set to a new string of the form	 *  [newName + "_n"], where 'n' is a unique thread id originally generated	 *  by the jvm Thread constructor.  If the default name of the thread has	 *  been changed before getting here, then nothing is done.	 *	 **/	public static void setUniqueThreadName(Thread thrd, String newName) {		// First, pull off the unique thread id already found in thrd's default name;		// we do so by searching for the '-' character, and then counting everything		// after it as a N.		if (thrd.getName().indexOf("Thread-") == -1) {		// default name has been changed; don't do anything.			return;		}		else {			String oldName = thrd.getName();			thrd.setName(newName + "_" +			  oldName.substring(oldName.indexOf("-")+1, oldName.length()));		} // end else.		return;	}	/*******************************************************************************/	/*        Protected methods                                                    */	/*******************************************************************************/	/**	 * Remove session from session table	 *	 * @param sessionid 	id of session to be removed	 */	protected void removeFromSessionTable(int sessionid)	{		sessionTable.remove(new Integer(sessionid));	}	/**	 * 	processCommands reads and processes NetworkServerControlImpl commands sent	 * 	to the network server over the socket.  The protocol used is	 * 		4 bytes 	- String CMD:	 * 		2 bytes		- Protocol version	 *		1 byte		- length of locale (0 for default)	 *			n bytes - locale	 *		1 byte		- length of codeset (0 for default)	 *			n bytes - codeset	 * 		1 byte		- command	 * 		n bytes		- parameters for the command	 * 	The server returns	 *		4 bytes		- String RPY:	 *	for most commands	 *		1 byte		- command result, 0 - OK, 1 - warning, 2 - error	 *	 if warning or error	 *		1 bytes		- length of message key	 *		n bytes		- message key	 *		1 byte		- number of parameters to message	 *		{2 bytes		- length of parameter	 *		n bytes		- parameter} for each parameter	 *  for sysinfo	 *		1 byte		- command result, 0 - OK, 1 - warning, 2 - error	 *   if OK 	 *		2 bytes		- length of sysinfo	 *		n bytes		- sysinfo	 *			 * 			 * 	Note, the 3rd byte of the command must not be 'D0' to distinquish it 	 *	from DSS structures.	 * 	The protocol for the parameters for each command follows:	 *	 * 	Command: trace <connection id> {on | off}	 * 	Protocol:	 * 		4 bytes		- connection id - connection id of 0 means all sessions	 * 		1 byte		- 0 off, 1 on	 * 	 * 	Command: logConnections {on | off}	 * 	Protocol:	 * 		1 byte		- 0 off, 1 on	 * 	 * 	Command: shutdown	 * 	No parameters	 * 	 * 	Command: sysinfo	 * 	No parameters	 * 	 * 	Command: dbstart	 * 	Protocol:	 * 		2 bytes		- length of database name	 * 		n bytes		- database name	 * 		2 bytes		- length of boot password	 * 		n bytes		- boot password 	 * 		2 bytes		- length of encryption algorithm	 * 		n bytes		- encryption algorithm	 * 		2 bytes		- length of encryption provider	 * 		n bytes		- encryption provider	 * 		2 bytes		- length of user name	 * 		n bytes		- user name	 * 		2 bytes		- length of password	 * 		n bytes		- password	 *	 * 	Command: dbshutdown	 * 	Protocol:	 * 		2 bytes		- length of database name	 * 		n bytes		- database name	 * 		2 bytes		- length of user name	 * 		n bytes		- user name	 * 		2 bytes		- length of password	 * 		n bytes		- password	 *	 * 	Command: connpool	 * 	Protocol:	 * 		2 bytes		- length of database name, if 0, default for all databases	 *						is set	 * 		n bytes		- database name	 *		2 bytes		- minimum number of connections, if 0, connection pool not used	 *						if value is -1 use default	 *		2 bytes		- maximum number of connections, if 0, connections are created	 *						as needed, if value is -1 use default	 *	 * 	Command: maxthreads	 * 	Protocol:	 *		2 bytes		- maximum number of threads	 *	 * 	Command: timeslice 	 * 	Protocol:	 *		4 bytes		- timeslice value	 *	 * 	Command: tracedirectory	 * 	Protocol:	 * 		2 bytes		- length of directory name	 * 		n bytes		- directory name	 *	 *	Command: test connection	 * 	Protocol:	 * 		2 bytes		- length of database name if 0, just the connection	 *						to the network server is tested and user name and 	 *						password aren't sent	 * 		n bytes		- database name	 * 		2 bytes		- length of user name (optional)	 * 		n bytes		- user name	 * 		2 bytes		- length of password  (optional)	 * 		n bytes		- password	 *	 *	The calling routine is synchronized so that multiple threads don't clobber each	 * 	other. This means that configuration commands will be serialized.	 * 	This shouldn't be a problem since they should be fairly rare.	 * 			 * @param reader	input reader for command	 * @param writer output writer for command	 * @param session	session information	 *	 * @exception Throwable	throws an exception if an error occurs	 */	protected synchronized void processCommands(DDMReader reader, DDMWriter writer, 		Session session) throws Throwable	{		try {			String protocolStr = reader.readCmdString(4);			String locale = DEFAULT_LOCALE;			String codeset = null;			// get the version			int version = reader.readNetworkShort();			if (version <= 0 || version > PROTOCOL_VERSION)				throw new Throwable(langUtil.getTextMessage("DRDA_UnknownProtocol.S",  new Integer(version).toString()));			int localeLen = reader.readByte();			if (localeLen > 0)			{				currentSession = session;				locale = reader.readCmdString(localeLen);				session.langUtil = new LocalizedResource(codeset,locale,DRDA_PROP_MESSAGES);			}			String notLocalMessage = null;			// for now codesetLen is always 0			int codesetLen = reader.readByte();			int command = reader.readByte();			if (command !=  COMMAND_TESTCONNECTION)			{				try {					checkAddressIsLocal(session.clientSocket.getInetAddress());				}catch (Exception e)				{					notLocalMessage = e.getMessage();				}			}			if (notLocalMessage != null)			{				sendMessage(writer, ERROR,notLocalMessage);				session.langUtil = null;				currentSession = null;				return;			}			switch(command)			{				case COMMAND_SHUTDOWN:					sendOK(writer);					directShutdown();					break;				case COMMAND_TRACE:					sessionArg = reader.readNetworkInt();					boolean on = (reader.readByte() == 1);					if (setTrace(on))					{						sendOK(writer);					}					else					{						sendMessage(writer, ERROR,  							localizeMessage("DRDA_SessionNotFound.U", 							(session.langUtil == null) ? langUtil : session.langUtil,							new String [] {new Integer(sessionArg).toString()}));					}					break;				case COMMAND_TRACEDIRECTORY:					setTraceDirectory(reader.readCmdString());					sendOK(writer);					consolePropertyMessage("DRDA_TraceDirectoryChange.I", traceDirectory);					break;				case COMMAND_TESTCONNECTION:					databaseArg = reader.readCmdString();					userArg = reader.readCmdString();					passwordArg = reader.readCmdString();					if (databaseArg != null)						connectToDatabase(writer, databaseArg, userArg, passwordArg);					else						sendOK(writer);					break;				case COMMAND_LOGCONNECTIONS:					boolean log = (reader.readByte() == 1);					setLogConnections(log);					sendOK(writer);					consolePropertyMessage("DRDA_LogConnectionsChange.I",						(log ? "DRDA_ON.I" : "DRDA_OFF.I"));					break;				case COMMAND_SYSINFO:					sendSysInfo(writer);					break;				case COMMAND_PROPERTIES:					sendPropInfo(writer);					break;				case COMMAND_RUNTIME_INFO:					sendRuntimeInfo(writer);					break;				case COMMAND_MAXTHREADS:					int max = reader.readNetworkInt();					try {						setMaxThreads(max);					}catch (Exception e) {						sendMessage(writer, ERROR, e.getMessage());						return;					}					int newval = getMaxThreads();					sendOKInt(writer, newval);					consolePropertyMessage("DRDA_MaxThreadsChange.I", 						new Integer(newval).toString());					break;				case COMMAND_TIMESLICE:					int timeslice = reader.readNetworkInt();					try {						setTimeSlice(timeslice);					}catch (Exception e) {						sendMessage(writer, ERROR, e.getMessage());						return;					}					newval = getTimeSlice();					sendOKInt(writer, newval);					consolePropertyMessage("DRDA_TimeSliceChange.I", 						new Integer(newval).toString());					break;			}		} catch (DRDAProtocolException e) {			//we need to handle this since we aren't in DRDA land here			consoleExceptionPrintTrace(e);		} catch (Exception e) {			consoleExceptionPrintTrace(e);		}		finally {			session.langUtil = null;			currentSession = null;		}	}	/**	 * Get the next session for the thread to work on	 * Called from DRDAConnThread after session completes or timeslice	 * exceeded.   	 *	 * If there is a waiting session, pick it up and put currentSession 	 * at the back of the queue if there is one.	 * @param currentSession	session thread is currently working on	 *	 * @return  next session to work on, could be same as current session	 */	protected Session getNextSession(Session currentSession)	{		Session retval = null;		if (shutdown == true)			return retval;		synchronized (runQueue)		{			try {				// nobody waiting - go on with current session				if (runQueue.size() == 0)				{					// no current session - wait for some work					if (currentSession == null)					{						while (runQueue.size() == 0)						{							// This thread has nothing to do now so 							// we will add it to freeThreads							freeThreads++;							runQueue.wait();							if (shutdown == true)								return null;							freeThreads--;						}					}					else						return currentSession;				}				retval = (Session) runQueue.elementAt(0);				runQueue.removeElementAt(0);				if (currentSession != null)					runQueueAdd(currentSession);			} catch (InterruptedException e) {			// If for whatever reason (ex. database shutdown) a waiting thread is			// interrupted while in this method, that thread is going to be			// closed down, so we need to decrement the number of threads			// that will be available for use.				freeThreads--;			}		}		return retval;	}	/**	 * Get the stored application requester or store if we haven't seen it yet	 *	 * @param appRequester Application Requester to look for

⌨️ 快捷键说明

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