package rcs.nml; import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.Socket; import java.util.Hashtable; import java.util.*; import rcs.utils.*; import rcs.nml.NMLFormatConverter; import rcs.nml.NMLFormatConverterBase; import rcs.nml.XDRFormatConverter; import rcs.nml.DISPFormatConverter; import rcs.utils.jcrypt; import rcs.utils.URL_and_FileLoader; import rcs.nml.NMLException; class NMLConfigInfo { String file_name = null; Hashtable buffer_lines = new Hashtable(); //Vector lines_vector; URL_and_FileLoader loader = null; // int lines_read = 0; } /** * Class for connecting to NML buffers, ussually via network sockets.
*
* Related Documentation: 
* RCS Library, NML Programmers Guide, Java Interface to NML
*
* Source Code:
* NMLConnection.java
* 
* 
* * @author Will Shackleford -- shackle@cme.nist.gov * */ public class NMLConnection { static protected Hashtable previously_read_nml_configurations = null; /** * This variable can be set to true by some other thread to * stop the login. * * @see rcs.nml.NMLConnection#login(java.lang.String,java.lang.String) */ public static boolean interrupt_login = false; protected NMLFormatConverterBase format_converter = null; protected NMLMessageDictionary message_dictionary = null; // Protocol Options protected static final int NML_STCP_PROTOCOL_TYPE = 1; protected static final int NML_TCP_PROTOCOL_TYPE = 2; protected static final int NML_UDP_PROTOCOL_TYPE = 3; private int protocol_option = NML_STCP_PROTOCOL_TYPE; // Data format Options protected static final int NML_ASCII_ENCODING_TYPE = 4; protected static final int NML_DISP_ENCODING_TYPE = 5; protected static final int NML_XDR_ENCODING_TYPE = 6; private int data_format_option =NML_DISP_ENCODING_TYPE; private int serial_number = 0; private int last_id_read = 0; // REMOTE_CMS_REQUEST_TYPE's protected static final int REMOTE_CMS_READ_REQUEST_TYPE = 1; protected static final int REMOTE_CMS_WRITE_REQUEST_TYPE = 2; protected static final int REMOTE_CMS_GET_KEYS_REQUEST_TYPE = 7; protected static final int REMOTE_CMS_LOGIN_REQUEST_TYPE = 8; private int request_type = REMOTE_CMS_READ_REQUEST_TYPE; // CMS_INTERNAL_ACCESS_TYPE's protected static final int CMS_READ_ACCESS = 1; protected static final int CMS_CHECK_IF_READ_ACCESS = 2; protected static final int CMS_PEEK_ACCESS = 3; protected static final int CMS_WRITE_ACCESS = 4; protected static final int CMS_WRITE_IF_READ_ACCESS = 5; private int access_type = CMS_READ_ACCESS; private int remote_status = 0; private int message_size = 0; private int was_read = 0; private int input_bytes_read = 0; private boolean reply_header_recieved = false; private boolean write_reply_recieved = true; private boolean input_buffer_ready = false; protected int output_data_size = 0; protected NMLmsg last_msg_read = null; /** * Set config_debug_on to true to print out additional information * while reading the NML Congiguration file. */ public boolean config_debug_on = false; /** * Set read_debug_on to true to print out additional information * while reading. */ public boolean read_debug_on = false; /** * Set write_debug_on to true to print out additional information * while writing. */ public boolean write_debug_on = false; private String input_string = ""; /** * Name of buffer to connect to. (used in configuration file) */ public String buffer_name = null; /** * Name of this process as used in configuration file. */ public String process_name = null; /** * Name/URL the configuration file to read. */ public String configuration_file = null; private int input_buffer_offset = 0; private int input_bytes_ready = 0; private byte input_buffer[]; private byte output_buffer[]; private static final int NML_DEFAULT_BUFFER_SIZE=2048; private int buffer_size; /** * TCP port of NML server. */ public int port = 0; /** * buffer_number from NML configuration file. */ public int buffer_number = 0; /** * name of the computer the NML server runs on. */ public String host = ""; protected boolean read_request_sent = false; private boolean null_error_reported = false; private DataInputStream m_InputStream = null; private DataOutputStream m_OutputStream = null; private Socket m_Socket = null; /** * True when this object is connected to the NML server. * All reads and writes will fail when this is false. */ public boolean connected = false; /** * This constructs an NMLConnection which will not work until * the host and port are set manually or * by reading the NML configuration file with ReadNMLConfigurationFile(). * Most users should use the other constructor with several parameters. * * @see rcs.nml.NMLConnection#NMLConnection(rcs.nml.NMLMessageDictionary, java.lang.String, java.lang.String, java.lang.String) * @see rcs.nml.NMLConnection#ReadNMLConfigurationFile(java.lang.String, java.lang.String, java.lang.String) * */ public NMLConnection() { // System.out.print("\r\nconstructing NMLConnection\r\n"); input_buffer = new byte[NML_DEFAULT_BUFFER_SIZE]; output_buffer = new byte[NML_DEFAULT_BUFFER_SIZE]; buffer_size = NML_DEFAULT_BUFFER_SIZE; interrupt_login = false; SetFormatConverter(new XDRFormatConverter()); } /** * This functions sets the NMLMessageDictionary for this connection. * The purpose of the NMLMessageDictionary is to provide a means * for the NMLConnection to determine the structure of messages being * sent or recieved using a message type. (An integer passed within * all NML messages.) * * @param new_dict the NMLMessageDictionary for this NMLConnection to use. * @see rcs.nml.NMLMessageDictionary */ public void SetMessageDictionary(NMLMessageDictionary new_dict) { message_dictionary = new_dict; if(null != format_converter) { format_converter.SetMessageDictionary(new_dict); } } /** * This functions gets the NMLMessageDictionary for this connection. * * @return the implementation of NMLMessageDictionary used by this * NMLConnection * * @see rcs.nml.NMLConnection#SetMessageDictionary(rcs.nml.NMLMessageDictionary) * @see rcs.nml.NMLMessageDictionary */ public NMLMessageDictionary GetMessageDictionary() { if(null != format_converter) { message_dictionary = format_converter.GetMessageDictionary(); } return message_dictionary; } /** * This functions sets the NMLFormatConverter for this connection. * The NMLFormatConverter is resposible for converting each of * the basic data types to some neutral format that can be * used on many different platforms. Most users should either accept * the default (XDRFormatConverter) or allow the format to be specified * in the NML configuration file rather than calling this function * directly. * * @param new_fc the NMLFormatConverter for this Connection to use. * * @see rcs.nml.NMLFormatConverter */ public void SetFormatConverter(NMLFormatConverter new_fc) { try { format_converter = (NMLFormatConverterBase) new_fc; } catch(ClassCastException e) { System.err.println("This NMLFormatConverter is not a subclass of NMLFormatConverterBase."); e.printStackTrace(); } if(null != message_dictionary && null != format_converter) { format_converter.SetMessageDictionary(message_dictionary); } } /** * This functions gets the NMLFormatConverter for this connection. * * @return the NMLFormatConverter for this connection * * @see rcs.nml.NMLConnection#SetFormatConverter(rcs.nml.NMLFormatConverter) */ public NMLFormatConverter GetFormatConverter() { return format_converter; } /** * This constuctor creates a fully functional NML Connection * using the application defined NMLMessageDictionary, reads * the NML configuration file, and connects immediately. * This is the constructor most users should use. * * @param msg_dict the NMLMessageDictionary for this connection to use (the NMLMessageDictionary allows this Connection to determine the message structure from the message type) * @param BufferName the name of the buffer to connect to, (must match one of the buffers in the configuration file.) * @param ProcessName the name of the process that will use this connection (must match one of the process names in the configuration file) * @param ConfigurationFile the file name or URL of an NML configuration file (URL's should either be complete, or they can be relative to rcs.utils.URL_and_FileLoader.current_directory) * @exception rcs.nml.NMLException * If the configuration file can not be read, the file is improperly formatted or the buffer or process can * can not be found in it. * */ public NMLConnection(NMLMessageDictionary msg_dict, String BufferName, String ProcessName, String ConfigurationFile) throws NMLException { this(); ReadNMLConfigurationFile(BufferName, ProcessName, ConfigurationFile); SetMessageDictionary(msg_dict); } /** * This function reads configuration information from the NML configuration file. * * @param BufferName the name of the buffer to connect to, (must match one of the buffers in the configuration file.) * @param ProcessName the name of the process that will use this connection (must match one of the process names in the configuration file) * @param ConfigurationFile the file name or URL of an NML configuration file (URL's should either be complete, or they can be relative to rcs.utils.URL_and_FileLoader.current_directory) * @exception rcs.nml.NMLException * If the configuration file can not be read, the file is improperly formatted or the buffer or process can * can not be found in it. * */ public void ReadNMLConfigurationFile(String BufferName, String ProcessName, String ConfigurationFile) throws NMLException { buffer_name = BufferName; process_name = ProcessName; configuration_file = ConfigurationFile; ReadNMLConfigurationFile(); connect(); } /** * Clear any data saved from previously read configuration files. */ public static void ClearStaticData() { previously_read_nml_configurations = null; } /** * Read preset configuration file. * @exception rcs.nml.NMLException * If the configuration file can not be read, the file is improperly formatted or the buffer or process can * can not be found in it. */ public void ReadNMLConfigurationFile() throws NMLException { URL_and_FileLoader loader = null; String current_line = null; boolean BufferLineFound = false; NMLConfigInfo config_info = null; int lines_parsed = 0; try { if(config_debug_on) { System.out.println("ReadNMLConfigurationFile(): buffer_name = "+buffer_name+", process_name = "+process_name+", configuration_file = "+configuration_file+","); } if(null == previously_read_nml_configurations) { previously_read_nml_configurations = new Hashtable(); } else { config_info = (NMLConfigInfo) previously_read_nml_configurations.get(configuration_file); } boolean buffer_line_in_hashtable = false; if(null == config_info) { loader = new URL_and_FileLoader(configuration_file); config_info = new NMLConfigInfo(); config_info.file_name = configuration_file; config_info.loader = loader; } else { buffer_line_in_hashtable = config_info.buffer_lines.containsKey(buffer_name); loader = config_info.loader; } data_format_option =NML_XDR_ENCODING_TYPE; while(true) { if(!buffer_line_in_hashtable) { current_line = loader.readLine(); } else { current_line = (String) config_info.buffer_lines.get(buffer_name); } if(null == current_line) { break; } if(config_debug_on) { System.out.println(current_line); } if(BufferLineFound) { break; } if(!current_line.startsWith("B")) { continue; } StringTokenizer tokenizer = new StringTokenizer(current_line," \t"); int token_number = 0; while(tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if(config_debug_on) { System.out.println("token "+token_number+" = "+token); } if(token_number == 1) { config_info.buffer_lines.put(token, current_line); if(token.equals(buffer_name)) { BufferLineFound = true; } else { break; } } if(token_number == 3) { host = token; if(config_debug_on) { System.out.println("host = "+host); } } if(token_number == 4) { try { buffer_size = (((new Integer(0)).valueOf(token)).intValue())*4; } catch(Exception e) { System.err.println("Invalid buffer size token ("+token+") in "+configuration_file+" for "+buffer_name); System.err.println("Using default buffer size."); e.printStackTrace(); buffer_size = NML_DEFAULT_BUFFER_SIZE; } if(buffer_size <= 0) { buffer_size = NML_DEFAULT_BUFFER_SIZE; } if(config_debug_on) { System.out.println("buffer_size = "+buffer_size); } } if(token_number == 7) { try { buffer_number = (((new Integer(0)).valueOf(token)).intValue()); } catch(Exception e) { System.err.println("Invalid buffer number token ("+token+") in "+configuration_file+" for "+buffer_name); e.printStackTrace(); } if(config_debug_on) { System.out.println("buffer_number = "+buffer_number); } } int port_index = -1; if(-1 != (port_index = token.indexOf("TCP="))) { protocol_option = NML_TCP_PROTOCOL_TYPE; String port_string = token.substring(port_index+4); if(config_debug_on) { System.out.println("Setting port number: port_string = "+port_string); } try { port = (((new Integer(0)).valueOf(port_string)).intValue()); } catch(Exception e) { System.err.println("Invalid port token ("+token+") in "+configuration_file+" for "+buffer_name); e.printStackTrace(); } if(config_debug_on) { System.out.println("port = "+port); } } if(-1 != (port_index = token.indexOf("STCP="))) { protocol_option = NML_STCP_PROTOCOL_TYPE; } if(-1 != (port_index = token.indexOf("ascii"))) { data_format_option = NML_ASCII_ENCODING_TYPE; } if(-1 != (port_index = token.indexOf("disp"))) { data_format_option = NML_DISP_ENCODING_TYPE; } token_number++; } // end while loop parsing tokens on on line if(buffer_line_in_hashtable) { break; } if(BufferLineFound) { break; } } // end while loop reading file line by line if(BufferLineFound) { if(buffer_size > 0) { input_buffer = new byte[buffer_size]; output_buffer = new byte[buffer_size]; } switch(data_format_option) { case NML_XDR_ENCODING_TYPE: SetFormatConverter(new XDRFormatConverter()); break; case NML_DISP_ENCODING_TYPE: SetFormatConverter(new DISPFormatConverter()); break; default: System.err.println("Invalid data_format_option."); SetFormatConverter(new XDRFormatConverter()); break; } } else { System.err.println("Can not find line for "+buffer_name+" in "+configuration_file); } previously_read_nml_configurations.put(configuration_file,config_info); } catch(Exception e) { System.err.println("Error reading NML configuration file "+configuration_file); System.err.println("BufferName = "+buffer_name); System.err.println("ProcessName = "+process_name); if(null != loader) { System.err.println("lines_read = "+loader.lines_read); } if(null != current_line) { System.err.println("last line read = "+current_line); } e.printStackTrace(); throw new NMLException("Misc. Error",buffer_name,configuration_file,e); } if(!BufferLineFound) { throw new NMLException("Can not find buffer line.", buffer_name,configuration_file); } } protected void finalize() { disconnect(); } /** * This function connects this NMLConnection object to a server. * All attempts to read or write will fail while the object is * disconnected. * * Since the NMLConnection(NMLMessageDictionary, String, String,String) * */ public int connect() { try { read_request_sent = false; if(port <= 0) { return -1; } if(connected) { disconnect(); } /* ****************************************************************** */ // javac now gives a warning if this Socket constructor is used, // saying the method has been deprecated. //m_Socket = new Socket(host,port,true); /* ******************************************************************* */ m_Socket = new Socket(host,port); m_OutputStream = new DataOutputStream(m_Socket.getOutputStream()); m_InputStream = new DataInputStream(m_Socket.getInputStream()); write_reply_recieved = true; read_request_sent = false; input_buffer_ready = false; input_bytes_read = 0; last_msg_read = null; interrupt_login = false; message_size = 0; /*set = new SocketSet(); set.port = port; set.m_Socket = m_Socket; set.m_OutputStream = m_OutputStream; set.m_InputStream = m_InputStream; set.count = 1; m_socketSetHashtable.put(portInteger, set); */ connected = true; System.out.print("Socket "+port+" opened. \r\n"); } catch(Throwable e) { System.err.println("\r\nCan't connect to port "+port+" on host "+host+"\r\n"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); return -1; } return 0; } /** * Disconnect this object from the NML server. This * allows the NML server to shutdown and come back up * while preserving the configuration information in this * object. * * All reads and writes will fail while the object is disconnected. */ public void disconnect() { try{ read_request_sent = false; /* SocketSet set; Integer portInteger = new Integer(port); set = (SocketSet) m_socketSetHashtable.get(portInteger); if(null != set) { set.count--; if(set.count <= 0) { if(set.m_OutputStream != null) { set.m_OutputStream.close(); set.m_OutputStream = null; } if(set.m_InputStream != null); { set.m_InputStream.close(); set.m_InputStream = null; } if(set.m_Socket != null) { set.m_Socket.close(); set.m_Socket = null; } m_socketSetHashtable.remove(portInteger); m_OutputStream = null; m_InputStream = null; m_Socket = null; System.out.print("\r\n Socket "+set.port+" closed. \r\n"); return; } System.out.print("\r\n Socket "+port+" closed.\r\n"); return; } */ if(connected) { if(m_OutputStream != null) { m_OutputStream.close(); m_OutputStream = null; } if(m_InputStream != null); { m_InputStream.close(); m_InputStream = null; } if(m_Socket != null) { m_Socket.close(); m_Socket = null; } System.out.print("Socket "+port+" closed.\r\n"); } connected = false; } catch(Exception e) { if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); } } /** * This function allows the application to gain access to * NML servers with security enabled. * * @param name the login name of the user running the client program * @param passwd the passwd of the user to login as. * @exception rcs.nml.NMLException * If the login could not be attempted because the connection to the * NML server failed. (If the passwd is wrong it returns false.) * * @return true if login was successful */ public boolean login(String name, String passwd) throws NMLException { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: return loginTCP(name, passwd); case NML_UDP_PROTOCOL_TYPE: return true; case NML_STCP_PROTOCOL_TYPE: return true; default: System.err.println("NMLConnection.read(): Invalid protocol_option = "+protocol_option+" -- buffer_name = "+buffer_name); return false; } } /** * Read a NMLmsg. * * @return null if the message in the * buffer has already been read by this NMLConnection, otherwise it * returns the NMLmsg read. * * @exception rcs.nml.NMLException * The read failed (ussually because of some network error). * */ public NMLmsg read() throws NMLException { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: return readTCP(); case NML_UDP_PROTOCOL_TYPE: return readUDP(); case NML_STCP_PROTOCOL_TYPE: if(null == format_converter) { return null; } return format_converter.convertStringToMsg(readSTCPDataString()); default: System.err.println("NMLConnection.read(): Invalid protocol_option = "+protocol_option+" -- buffer_name = "+buffer_name); return null; } // return null; } protected void read_raw_data() throws NMLException { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: readTCP_raw_data(); case NML_UDP_PROTOCOL_TYPE: readUDP_raw_data(); case NML_STCP_PROTOCOL_TYPE: if(null == format_converter) { return ; } readSTCPDataString(); default: System.err.println("NMLConnection.read(): Invalid protocol_option = "+protocol_option+" -- buffer_name = "+buffer_name); return ; } // return null; } protected void readTCP_raw_data() throws NMLException { if(port < 1) { throw new NMLException("Invalid Port.", buffer_name,configuration_file); } if(null == format_converter) { throw new NMLException("No Format Converter.", buffer_name,configuration_file); } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); System.err.println("Stream is null."); } null_error_reported = true; throw new NMLException("No input or output stream.", buffer_name,configuration_file); } try { if(!write_reply_recieved) { input_buffer_ready = false; if(write_debug_on || read_debug_on) { System.out.println("NMLConnection.readTCP() -- Still have not recieved write reply for buffer "+buffer_name); } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 12) { return ; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"+" -- buffer_name = "+buffer_name); } remote_status = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(write_debug_on || read_debug_on) { System.out.println("NMLConnection.readTCP() -- Write reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status); } write_reply_recieved = true; } if(!read_request_sent) { input_buffer_ready = false; m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_READ_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); access_type = CMS_READ_ACCESS; m_OutputStream.writeInt(access_type); m_OutputStream.writeInt(last_id_read); read_request_sent = true; reply_header_recieved = false; if(read_debug_on) { System.out.println("TCP read request sent."); System.out.println("serial_number = "+serial_number+", request_type = "+request_type+", buffer_number = "+buffer_number); System.out.println("access_type = "+access_type+", last_id_read = "+last_id_read+" -- buffer_name = "+buffer_name); } serial_number++; return ; } else { input_bytes_ready = m_InputStream.available(); if(!reply_header_recieved) { input_buffer_ready = false; if(input_bytes_ready < 20) { return ; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"+" -- buffer_name = "+buffer_name); } remote_status = m_InputStream.readInt(); message_size = m_InputStream.readInt(); last_id_read = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(read_debug_on) { System.out.println("TCP read reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status+", buffer_number = "+buffer_number); System.out.println("message_size = "+message_size+", was_read = "+was_read+" -- buffer_name = "+buffer_name); } if(message_size == 0) { read_request_sent = false; return ; } reply_header_recieved = true; } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < message_size) { input_buffer_ready = false; return ; } input_bytes_read = m_InputStream.read(input_buffer,0,message_size); if(input_bytes_read < message_size) { System.err.println("NML tried to read "+message_size+" bytes from input stream but only got "+input_bytes_read+" bytes."+" -- buffer_name = "+buffer_name); input_buffer_ready = false; return ; } reply_header_recieved = false; read_request_sent = false; if(read_debug_on) { System.out.println("TCP read converting Raw Data(message_size = "+message_size+") to NMLmsg."+" -- buffer_name = "+buffer_name); } input_buffer_ready = true; } } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); input_buffer_ready = false; throw new NMLException("Misc. Error.", buffer_name,configuration_file,e); } } protected NMLmsg readTCP() throws NMLException { readTCP_raw_data(); if(!input_buffer_ready) { return null; } return format_converter.convertRawDataToMsg(input_buffer, message_size); } protected NMLmsg readUDP() throws NMLException { return last_msg_read; } protected void readUDP_raw_data() throws NMLException { } /** * Read an NMLmsg but do not change the was_read flag. * * @return null if the message in the * buffer has already been read by this NMLConnection or no message * has yet been written to the buffer, otherwise it * returns the NMLmsg read. * * @exception rcs.nml.NMLException * The peek failed. */ public NMLmsg peek() throws NMLException { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: return peekTCP(); case NML_UDP_PROTOCOL_TYPE: return peekUDP(); case NML_STCP_PROTOCOL_TYPE: if(null == format_converter) { return null; } return format_converter.convertStringToMsg(peekSTCPDataString()); default: System.err.println("NMLConnection.peek(): Invalid protocol_option = "+protocol_option+" -- buffer_name = "+buffer_name); return null; } // return null; } protected void peek_raw_data() throws NMLException { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: peekTCP_raw_data(); break; case NML_UDP_PROTOCOL_TYPE: peekUDP_raw_data(); break; case NML_STCP_PROTOCOL_TYPE: if(null == format_converter) { return ; } peekSTCPDataString(); break; default: System.err.println("NMLConnection.peek_raw_data(): Invalid protocol_option = "+protocol_option+" -- buffer_name = "+buffer_name); return ; } // return null; } protected void peekTCP_raw_data() throws NMLException { if(port < 1) { throw new NMLException("Invalid Port.", buffer_name,configuration_file); } if(null == format_converter) { throw new NMLException("No Format Converter.", buffer_name,configuration_file); } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); System.err.println("Stream is null."); } null_error_reported = true; throw new NMLException("No input or output stream.", buffer_name,configuration_file); } try { if(!write_reply_recieved) { input_buffer_ready = false; if(write_debug_on || read_debug_on) { System.out.println("NMLConnection.peekTCP() -- Still have not recieved write reply for buffer "+buffer_name); } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 12) { return ; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"+" -- buffer_name = "+buffer_name); } remote_status = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(write_debug_on || read_debug_on) { System.out.println("NMLConnection.peekTCP() -- Write reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status); } write_reply_recieved = true; } if(!read_request_sent) { input_buffer_ready = false; m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_READ_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); access_type = CMS_PEEK_ACCESS; m_OutputStream.writeInt(access_type); m_OutputStream.writeInt(last_id_read); read_request_sent = true; reply_header_recieved = false; if(read_debug_on) { System.out.println("TCP peek request sent."); System.out.println("serial_number = "+serial_number+", request_type = "+request_type+", buffer_number = "+buffer_number); System.out.println("access_type = "+access_type+", last_id_read = "+last_id_read+" -- buffer_name = "+buffer_name); } serial_number++; return ; } else { input_bytes_ready = m_InputStream.available(); if(!reply_header_recieved) { input_buffer_ready = false; if(input_bytes_ready < 20) { return ; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"+" -- buffer_name = "+buffer_name); } remote_status = m_InputStream.readInt(); message_size = m_InputStream.readInt(); last_id_read = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(read_debug_on) { System.out.println("TCP peek reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status+", buffer_number = "+buffer_number); System.out.println("message_size = "+message_size+", was_read = "+was_read+" -- buffer_name = "+buffer_name); } if(message_size == 0) { read_request_sent = false; return ; } reply_header_recieved = true; } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < message_size) { input_buffer_ready = false; return ; } input_bytes_read = m_InputStream.read(input_buffer,0,message_size); if(input_bytes_read < message_size) { System.err.println("NML tried to peek "+message_size+" bytes from input stream but only got "+input_bytes_read+" bytes."+" -- buffer_name = "+buffer_name); input_buffer_ready = false; return ; } reply_header_recieved = false; read_request_sent = false; input_buffer_ready = true; } } catch(Exception e) { System.err.println("\r\nCan't peek NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); input_buffer_ready = false; throw new NMLException("Misc. Error.", buffer_name,configuration_file,e); } } protected NMLmsg peekTCP() throws NMLException { peekTCP_raw_data(); if(!input_buffer_ready) { return null; } if(read_debug_on) { System.out.println("TCP peek converting Raw Data(message_size = "+message_size+") to NMLmsg."+" -- buffer_name = "+buffer_name); } return format_converter.convertRawDataToMsg(input_buffer, message_size); } protected NMLmsg peekUDP() { return last_msg_read; } protected void peekUDP_raw_data() { } /** * Reads an NMLmsg and converts it to a String. * * @return null if the message in the * buffer has already been read by this NMLConnection or no message * has yet been written to the buffer, otherwise it * returns the NMLmsg read converted to a string. * The string is a comma separated list of the parameters * in the NMLmsg in the order they are updated, starting with * the type and size. * * @exception rcs.nml.NMLException * The read failed. * * @see rcs.nml.NMLConnection#read() */ public String readDataString() throws NMLException { if(protocol_option == NML_STCP_PROTOCOL_TYPE && data_format_option == NML_DISP_ENCODING_TYPE) { input_string = readSTCPDataString(); if(read_debug_on) { System.out.println("input_string = "+input_string); } return input_string; } else { if(data_format_option != NML_DISP_ENCODING_TYPE) { if(null == format_converter) { return null; } input_string = format_converter.convertMsgToString(read()); if(read_debug_on) { System.out.println("input_string = "+input_string); } } else { read_raw_data(); if(input_buffer_ready) { input_string = new String(input_buffer,0,0,message_size); } else { input_string = null; } } return input_string; } } protected String readSTCPDataString() throws NMLException { String request_string; String reply_string = ""; int input_bytes_ready = 0; int input_bytes_read = 0; int bytes_to_read = 0; int input_end_of_line = 0; if(port < 1) { throw new NMLException("Invalid Port.", buffer_name,configuration_file); } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); System.err.println("Stream is null."); } null_error_reported = true; throw new NMLException("No input or output stream.", buffer_name,configuration_file); } try { if(!read_request_sent) { request_string = "read("+buffer_number+"):\n"; m_OutputStream.writeBytes(request_string); read_request_sent = true; input_string = ""; if(read_debug_on) { System.out.println("readSTCPDataString(): Read request sent. request_string = "+request_string+", (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } return null; } else { input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 1) { return null; } if(input_bytes_ready < buffer_size) { bytes_to_read = input_bytes_ready; } else { bytes_to_read = buffer_size; } input_bytes_read = m_InputStream.read(input_buffer,0,bytes_to_read); if(input_bytes_read < 1) { return null; } String string_to_add = new String(input_buffer,0,0,input_bytes_read); if(null == input_string) { input_string = ""; } if(null != string_to_add) { input_string += string_to_add; } input_end_of_line = input_string.indexOf('\n'); if(input_end_of_line < 1) { return null; } reply_string = input_string.substring(0,input_end_of_line); if(reply_string.charAt(input_end_of_line-1) == '\r') { reply_string = reply_string.substring(0,input_end_of_line-1); } if(reply_string.charAt(0) == '\r') { reply_string = reply_string.substring(1); } input_string = input_string.substring(input_end_of_line+1); } if(reply_string.startsWith("ERR") || reply_string.startsWith("null")) { Integer i; read_request_sent = false; if(reply_string.length() > 5) { if(reply_string.charAt(5) == '0') { return null; } } if(-1 != reply_string.indexOf("Shutdown")) { disconnect(); } throw new Exception(); } } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+" reply_string= "+reply_string+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); throw new NMLException("Misc. Error.", buffer_name,configuration_file,e); } read_request_sent = false; return reply_string; } /** * Reads an NMLmsg using peek() and converts it to a String. * * @return null if the message in the * buffer has already been read by this NMLConnection or no message * has yet been written to the buffer, otherwise it * returns the NMLmsg read converted to a string. * The string is a comma separated list of the parameters * in the NMLmsg in the order they are updated, starting with * the type and size. * * @exception rcs.nml.NMLException * The peek failed. * * @see rcs.nml.NMLConnection#peek() */ public String peekDataString() throws NMLException { if(protocol_option == NML_STCP_PROTOCOL_TYPE && data_format_option == NML_DISP_ENCODING_TYPE) { input_string = peekSTCPDataString(); if(read_debug_on) { System.out.println("input_string = "+input_string); } return input_string; } else { if(data_format_option != NML_DISP_ENCODING_TYPE) { if(null == format_converter) { return null; } input_string = format_converter.convertMsgToString(peek()); if(read_debug_on) { System.out.println("input_string = "+input_string); } } else { peek_raw_data(); if(input_buffer_ready) { input_string = new String(input_buffer,0,0,message_size); } else { input_string = null; } } return input_string; } } protected String peekSTCPDataString() throws NMLException { String request_string; String reply_string = ""; int input_bytes_ready = 0; int input_bytes_read = 0; int bytes_to_read = 0; int input_end_of_line = 0; if(port < 1) { throw new NMLException("Invalid Port.", buffer_name,configuration_file); } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); System.err.println("Stream is null."); } null_error_reported = true; throw new NMLException("No input or output stream.", buffer_name,configuration_file); } try { if(!read_request_sent) { request_string = "peek("+buffer_number+"):\n"; m_OutputStream.writeBytes(request_string); read_request_sent = true; input_string = ""; if(read_debug_on) { System.out.println("peekSTCPDataString(): Read request sent. request_string = "+request_string+", (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } return null; } else { input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 1) { return null; } if(read_debug_on) { System.out.println("peekSTCPDataString(): "+input_bytes_ready+" bytes ready. , (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } if(input_bytes_ready < buffer_size) { bytes_to_read = input_bytes_ready; } else { bytes_to_read = buffer_size; } input_bytes_read = m_InputStream.read(input_buffer,0,bytes_to_read); if(input_bytes_read < 1) { return null; } if(read_debug_on) { System.out.println("peekSTCPDataString(): "+input_bytes_read+" bytes received. , (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } String string_to_add = new String(input_buffer,0,0,input_bytes_read); if(null == input_string) { input_string = ""; } if(null == string_to_add) { return null; } if(string_to_add.length() < 1) { return null; } if(read_debug_on) { System.out.println("peekSTCPDataString(): Data received. string_to_add = <"+string_to_add+">, (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } input_string += string_to_add; input_end_of_line = input_string.indexOf('\n'); if(input_end_of_line < 0) { return null; } reply_string = input_string.substring(0,input_end_of_line); if(read_debug_on) { System.out.println("peekSTCPDataString(): reply received. reply_string = "+reply_string+", (port="+port+" buffer_number="+buffer_number+" host="+host+")"); } if(null != reply_string) { if(reply_string.length() > 0 && reply_string.length() > input_end_of_line-1) { if(reply_string.charAt(input_end_of_line-1) == '\r') { reply_string = reply_string.substring(0,input_end_of_line-1); } if(reply_string.charAt(0) == '\r') { reply_string = reply_string.substring(1); } } } input_string = input_string.substring(input_end_of_line+1); } read_request_sent = false; while(true) { if(reply_string.length() < 1) { return null; } if(reply_string.charAt(0) != ' ') { break; } } if(reply_string.startsWith("E") || reply_string.startsWith("null")) { Integer i; if(reply_string.length() > 5) { if(reply_string.charAt(5) == '0') { return null; } } if(-1 != reply_string.indexOf("Shutdown")) { disconnect(); } throw new Exception(); } } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+" reply_string = "+reply_string+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); throw new NMLException("Misc. Error.", buffer_name,configuration_file,e); } read_request_sent = false; return reply_string; } protected boolean loginTCP(String name, String passwd) throws NMLException { if(port < 1) { throw new NMLException("Invalid Port.", buffer_name,configuration_file); } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"+" -- buffer_name = "+buffer_name); System.err.println("Stream is null."); } null_error_reported = true; throw new NMLException("No input or output stream.", buffer_name,configuration_file); } try { if(interrupt_login) { System.err.println("NMLConnection.login("+name+",********) interrupted."); System.err.println("Can't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } long start_millis = System.currentTimeMillis(); while(read_request_sent) { peekTCP(); if(read_request_sent && ((System.currentTimeMillis()) -start_millis) > 2000) { System.err.println("NMLConnection.loginTCP() -- timed out waiting for !read_request_sent."); System.err.println("\r\nCan't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } } m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_GET_KEYS_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); m_OutputStream.writeInt(0); m_OutputStream.writeInt(0); byte namebytes[] = new byte[16]; int bytes_namelen = name.length(); if(bytes_namelen > 16) { bytes_namelen = 16; } name.getBytes(0,bytes_namelen,namebytes,0); m_OutputStream.write(namebytes, 0, 16); serial_number++; for(int i = 0; i < 300; i++) { if(interrupt_login) { System.err.println("NMLConnection.login("+name+",********) interrupted."); System.err.println("Can't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } if(m_InputStream.available() >= 20) { break; } Thread.sleep(100); } if(m_InputStream.available() < 20) { System.err.println("NMLConnection.login("+name+",********) timed out."); System.err.println("Can't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+") -- buffer_name = "+buffer_name); } byte key1_bytes[] = new byte[8]; m_InputStream.read(key1_bytes, 0,8); String key1 = new String(key1_bytes,0,0,2); String passwd_pass1 = jcrypt.crypt(passwd, key1); byte key2_bytes[] = new byte[8]; m_InputStream.read(key2_bytes, 0,8); String key2 = new String(key2_bytes,0,0,2); String passwd_pass2 = jcrypt.crypt(passwd_pass1, key2); m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_LOGIN_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); m_OutputStream.writeInt(0); m_OutputStream.writeInt(0); m_OutputStream.write(namebytes, 0, 16); byte passwdbytes[] = new byte[16]; int bytes_passwdlen = passwd_pass2.length(); if(bytes_passwdlen > 16) { bytes_passwdlen = 16; } passwd_pass2.getBytes(0,bytes_passwdlen,passwdbytes,0); m_OutputStream.write(passwdbytes, 0, 16); serial_number++; for(int i = 0; i < 300; i++) { if(interrupt_login) { System.err.println("NMLConnection.login("+name+",********) interrupted."); System.err.println("Can't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } if(m_InputStream.available() >= 8) { break; } Thread.sleep(100); } if(m_InputStream.available() < 8) { System.err.println("NMLConnection.login("+name+",********) timed out."); System.err.println("Can't login NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); return false; } returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+") -- buffer_name = "+buffer_name); } int login_succeeded = m_InputStream.readInt(); System.out.println("login_succeeded = "+login_succeeded); return (boolean) (login_succeeded == 1); } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); throw new NMLException("Misc. Error.", buffer_name,configuration_file,e); } } protected int writeTCP(NMLmsg msg) { try { if(null == msg) { return -1; } if(port < 1) { return -1; } if(null == format_converter) { return -1; } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); System.err.println("Stream is null."); } null_error_reported = true; return -1; } long start_millis = System.currentTimeMillis(); while(read_request_sent) { peekTCP(); if(read_request_sent && ((System.currentTimeMillis()) -start_millis) > 2000) { System.err.println("NMLConnection.writeTCP() -- timed out waiting for !read_request_sent."); if(read_request_sent) { return -1; } } } if(!write_reply_recieved) { if(write_debug_on) { System.out.println("NMLConnection.writeTCP() -- Still have not recieved write reply for buffer "+buffer_name); } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 12) { return -1; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"); } remote_status = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(write_debug_on) { System.out.println("NMLConnection.writeTCP() -- Write reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status); } write_reply_recieved = true; } if(format_converter.convertMsgToRawData(output_buffer,buffer_size,msg) < 0) { return -1; } m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_WRITE_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); access_type = CMS_WRITE_ACCESS; m_OutputStream.writeInt(access_type); m_OutputStream.writeInt(format_converter.raw_data_size); m_OutputStream.write(output_buffer,0, format_converter.raw_data_size); if(write_debug_on) { System.out.println("TCP write request sent."); System.out.println("serial_number = "+serial_number+", request_type = "+request_type+", buffer_number = "+buffer_number); System.out.println("access_type = "+access_type+", last_id_read = "+last_id_read+", raw_data_size = "+format_converter.raw_data_size); } write_reply_recieved = false; serial_number++; return 0; } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); return -1; } } protected int writeTCP_raw_data() { try { if(port < 1) { return -1; } if(null == format_converter) { return -1; } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); System.err.println("Stream is null."); } null_error_reported = true; return -1; } long start_millis = System.currentTimeMillis(); while(read_request_sent) { peekTCP(); if(read_request_sent && ((System.currentTimeMillis()) -start_millis) > 2000) { System.err.println("NMLConnection.writeTCP() -- timed out waiting for !read_request_sent."); if(read_request_sent) { return -1; } } } if(!write_reply_recieved) { if(write_debug_on) { System.out.println("NMLConnection.writeTCP() -- Still have not recieved write reply for buffer "+buffer_name); } input_bytes_ready = m_InputStream.available(); if(input_bytes_ready < 12) { return -1; } int returned_serial_number = m_InputStream.readInt(); if(returned_serial_number != serial_number) { System.err.println("returned_serial_number("+returned_serial_number+") != serial_number("+serial_number+")"); } remote_status = m_InputStream.readInt(); was_read = m_InputStream.readInt(); if(write_debug_on) { System.out.println("NMLConnection.writeTCP() -- Write reply recieved."); System.out.println("returned_serial_number = "+returned_serial_number+", remote_status = "+remote_status); } write_reply_recieved = true; } m_OutputStream.writeInt(serial_number); request_type = REMOTE_CMS_WRITE_REQUEST_TYPE; m_OutputStream.writeInt(request_type); m_OutputStream.writeInt(buffer_number); access_type = CMS_WRITE_ACCESS; m_OutputStream.writeInt(access_type); m_OutputStream.writeInt(output_data_size); m_OutputStream.write(output_buffer,0, output_data_size); if(write_debug_on) { System.out.println("TCP write request sent."); System.out.println("serial_number = "+serial_number+", request_type = "+request_type+", buffer_number = "+buffer_number); System.out.println("access_type = "+access_type+", last_id_read = "+last_id_read+", raw_data_size = "+format_converter.raw_data_size); } write_reply_recieved = false; serial_number++; return 0; } catch(Exception e) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); return -1; } } protected int writeUDP(NMLmsg msg) { if(null == msg) { return -1; } return 0; } /** * Writes an NMLmsg. * * @param msg the NMLmsg to write. * @return 0 if the write was successful, -1 if there was an error */ public int write(NMLmsg msg) { if(null == msg) { return -1; } switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: return writeTCP(msg); case NML_UDP_PROTOCOL_TYPE: return writeUDP(msg); case NML_STCP_PROTOCOL_TYPE: if(null == format_converter) { return -1; } return writeSTCPDataString(format_converter.convertMsgToString(msg)); default: System.err.println("NMLConnection.write(): Invalid protocol_option = "+protocol_option); return -1; } //return null; } public int write_raw_data() { switch(protocol_option) { case NML_TCP_PROTOCOL_TYPE: return writeTCP_raw_data(); case NML_UDP_PROTOCOL_TYPE: return writeUDP_raw_data(); default: System.err.println("NMLConnection.write(): Invalid protocol_option = "+protocol_option); return -1; } //return null; } private int writeUDP_raw_data() { return 0; } /** * Convert a String to an NMLmsg and then write it to the buffer. * * @param dataString string to write * @return 0 if the write was successful, -1 otherwise */ public int writeDataString(String dataString) { if(null == dataString) { return -1; } if(protocol_option == NML_STCP_PROTOCOL_TYPE && data_format_option == NML_DISP_ENCODING_TYPE) { return writeSTCPDataString(dataString); } else { if(data_format_option != NML_DISP_ENCODING_TYPE) { if(null == format_converter) { return -1; } return write(format_converter.convertStringToMsg(dataString)); } else { dataString.getBytes(0, dataString.length(), output_buffer, 0); output_data_size = dataString.length(); return write_raw_data(); } } } protected int writeSTCPDataString(String dataString) { String request_string; try { if(null == dataString) { return -1; } if(port < 1) { return -1; } if(null == m_OutputStream || null == m_InputStream) { if(!null_error_reported) { System.err.println("\r\nCan't read NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); System.err.println("Stream is null."); } null_error_reported = true; return -1; } if(null == dataString) { System.err.println("NMLConnnection.write() -- attempt to send null dataString."); return -1; } if(dataString.length() < 1) { System.err.println("NMLConnnection.write() -- attempt to send dataString with length less than 1."); return -1; } request_string = "write("+buffer_number+"): "+dataString+"\n"; m_OutputStream.writeBytes(request_string); } catch(Exception e) { System.err.println("\r\nCan't write NML (port="+port+" buffer_number="+buffer_number+" host="+host+")"); if(null != e.getMessage()) { System.err.print("\r\n"+e.getMessage()+"\r\n"); } e.printStackTrace(); return -1; } return 0; } }