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

📄 serversearch.java

📁 J2ME下开发的P2P搜索程序,功能较基础,较简单,可供学习和参考使用.
💻 JAVA
字号:
 /**
  *  The packages below are included because these packages are 
  *	 required by the XML classes.
  */	
  import com.sun.kjava.*;
  import org.kxml.*;
  import org.kxml.parser.*;
/**
 *   Importing the basic packages required by various classes during 
 *	 the execution of the program.	
 */ 
 import java.lang.*;
 import javax.microedition.io.*;
 import javax.microedition.io.Connector;
 import java.io.*;
 import java.util.*;
 
//  Declaration of a class serversearch used for server level 
//	searching.
  public class serversearch extends Spotlet
  {
  		Vector record_of_users;
  /**
   *   Declaraing variables for the xmlparser and parse event(Where 
   *   the call backs after the parsing will be stored.	
   */  	
 		AbstractXmlParser xmlparser;
 		ParseEvent event;
 	
 		DataInputStream din;
         String tt= "";
	// Declaring and initializing the buttons..
 		private Button search = new Button("Search", 25,120);
         private Button cancel = new Button("Cancel", 65,120);
	// Declaring and initializing the TextField..
         private TextField t_file_name_search = new TextField("File Names ", 10, 35,130,10);
		// Declaring and initializing the TextField..	
      private TextField t_computer_name_search = new TextField("On Computer/s ", 10, 70,130,10);                
       
	// The current graphics is obtained...	
         Graphics gr = Graphics.getGraphics();
 		Vector ip_address;
 		Vector users_connected;
 		
 
	// serversearch constructor	
   public serversearch(String user_data, Vector ip_address, Vector users_connected)
        {
         
/**
 *  Initializing the variable of the graphics class and clearing   
 *	the drawing region by calling the appropriate functions of 
 *	the Graphics class namely (resetDrawRegion() and clearScreen()).
 */  
 	    gr.resetDrawRegion();
         gr.clearScreen();	
//	To make the spotlet as the current spotlet..
 		register(NO_EVENT_OPTIONS);
 		
/**
 *  Displaying a string (Title) at a specified position by using 
 *	the member function of the Graphics class
 */ 
         gr.drawString("Server Level Search",35,10);
// Painting the textfield on the screen and makeing it the current
// textfield.
         t_file_name_search.paint();
         t_file_name_search.setFocus();
// Painting the textfield on the screen 
         t_computer_name_search.paint();
// Painting the buttons on the screen 
         search.paint();
         cancel.paint();
 		
// initializing the parameters with the varaiables declared 
// earlier.
 		this.ip_address = ip_address;
 		this.users_connected =users_connected;
         }	// end constructor
 
 
// Event Handling function. (KeyDown)..
         public void keyDown( int x)
         {
		// If TAB button is pressed then the focus shifts from one
		// textfield to the other..
                 if(t_file_name_search.hasFocus())
                 {
                   t_file_name_search.handleKeyDown(x);
 				  String tt = t_file_name_search.getText();
 				  if (x == 9)
 				  {
 					t_file_name_search.loseFocus();	
 					t_computer_name_search.setFocus();
 				  }
                 }                                  
 
 				else if(t_computer_name_search.hasFocus())
                 {
                   t_computer_name_search.handleKeyDown(x);       
   				  if (x == 9)
 				  {
 					t_computer_name_search.loseFocus();
 					t_file_name_search.setFocus();	
 
 				  }
 
 				}
         } 
      
 
 
	// Event Handling function. (penDown)..
         public void penDown( int x, int y)
         {
         
 // if search button is pressed... 
                if(search.pressed(x,y))
                 {
 					
 					record_of_users = new Vector();
 
 					boolean b_file = false;
 					boolean b_computer = false;
 					int pointfind = 0;
 					String file_name = t_file_name_search.getText();
	// Various checks to ensure thast no field is left empty..
 					if(file_name.equals(""))
 					{
 					file_name = "*";
 					b_file = true;
 					}
 					else
 					{
 					pointfind = file_name.indexOf(".");
 					if( pointfind > 0)
 					{
 					b_file = true;
 					}
  					}
  					String computer_name = t_computer_name_search.getText();
  					if(computer_name.equals(""))
  					{
  					computer_name = "*";
  					b_computer = true;
  					}
  					else
  					{
  					if( pointfind > 0)
  					{
  					b_computer = true;
  					}
  					}
  
  
  				if (b_computer == true && b_file == true)
  					{
  					}
  					else
  					{
  					String text_for_display = "";	
  					int number = 1;
  					
  					try
  					{
 // Using the Connector class of CLDC an InputStream is opened on 
 // the ASP
  					din = Connector.openDataInputStream("testhttp://harpreet/p2p/search.asp?us=" + computer_name + "&fs=" + file_name);			
	// The stream of response by the server is then passed on to 
	// the xmlparser for parsing..

  					 xmlparser = new XmlParser(new InputStreamReader(din));
  					 }
  					 catch(IOException e)
  					 {
  					 System.out.println("Server Search Exception: "+e);
  					 
  					 }
			// Function used for parsing is called...
  					 parseData();
  					 
  					
		// An enumeration is generated from the vector users_connected..
  					Enumeration enumeration = users_connected.elements();
		// A loop is moved till enumeration has more elements
  					while (enumeration.hasMoreElements())
  					{
  						int increment = 0;
  						Object temp = enumeration.nextElement();
  						
  						Enumeration enum = record_of_users.elements();
  			            while (enum.hasMoreElements())
  			            {
  				          Object temp1 = enum.nextElement();
  						  
		// If matchess then count is increased by 1. 
  						  if (temp1.equals(temp))
  						  {
  						   increment++;
  						  }
  				        }
  					
		// The count is shown with the text_for_display.
  					  text_for_display = text_for_display+number+". "+(String)temp+"("+increment+")"+"\n";
  					  increment = 0;
  					  number++;
  					}				
  				
		// the caret on the textField is killed.
  					t_file_name_search.loseFocus();
  					t_file_name_search.killCaret();
  				
		// the caret on the textField is killed.
  					t_computer_name_search.loseFocus();
  					t_computer_name_search.killCaret();

		// The Spotlet is unregistered..
  					unregister();
  
// Call to the userlist_screen is made with appropriate parameters
  					new userlist_screen(text_for_display, ip_address,users_connected,true, record_of_users); 	
  
  					}
  
                  }                         
// If cancel is pressed..
                  else if(cancel.pressed(x,y))
                  {
	// textfield caret's are killed..
  				t_file_name_search.loseFocus();
  				t_file_name_search.killCaret();
  				
  				t_computer_name_search.loseFocus();
  				t_computer_name_search.killCaret();
  
	// Spotlet is unregistered.
  				unregister();
// Create an object of the class peer_application and call the 
// function startReading() to send a request to the server
  				peer_application refresh_application = new peer_application();
  				refresh_application.startReading();
	// Then call the parser function it will return a vector.
  				Vector ip_addresses =  refresh_application.parseData();
  				String text_for_display = refresh_application.returnTextForDisplay();
  				
  				
  				Vector uers_connected = refresh_application.returnUsersConnected();
 	// class userlist_screen is called with appropriate parameters..
  				new userlist_screen(text_for_display,ip_addresses,users_connected, false,null);
  
  
  
                  }                         
          }
 
// Function parseData (it will hold the callbacks generated by the 
// XMLParsing.
  	void parseData()
  		{
  		  do
	// Move in loop till the XML document ends..
  		   {
  		    try
  		     {	
  				event = xmlparser.read ();
  											
  				if(event.getType()==Xml.START_TAG)
  				{
  					StartTag stag = (StartTag)event;
  			 	
  					String name = stag.getName();
				// The Start Tag is identified
  					
	/**
	 * If the Tag encountered is result then the Attributes of the   
	 * tag are taken using the function getValue(). and are added to 
	 * there appropriate position.	
	 */ 
  					if (name.equals("result"))
  					{
  
  					String folder_identity = "";		
  						
  					String ip = stag.getValue("ip").trim();
  					String username = stag.getValue("username").trim();
  					String filename = stag.getValue("filename");
  
 
	// For files and folders appropriate flags are attached..
  					if (!(filename.length() == 0))
  					{
  						filename.trim();
  					}
  					
  		if (!(filename.length() == 0))
  			{
  			
  
  			if (!(filename.charAt(filename.length()-1)  == '\\'))
  			{
  			folder_identity = "1";
  			}
  			else
  			{
  			folder_identity = "0";
  			}
  			
  
		// The username , ip address, the file /foldername and the 
		// file /folder flag is added to the vector.	
  		record_of_users.addElement((Object)username);
  		record_of_users.addElement((Object)ip);
  		record_of_users.addElement((Object)filename);
  		record_of_users.addElement((Object)folder_identity);
  				}		
  			}
  		}
  							
       }
      catch(IOException ex)
  	   {		
  			System.out.println("Exception occured");
  	   }
  	}
  	while (!(event instanceof EndDocument));
  		System.out.println("**** END OF DOCUMENT ****");
		// End of parsing...
  	}
  }		// End of serversearch...

⌨️ 快捷键说明

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