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

📄 peer_application.java

📁 J2ME下开发的P2P搜索程序,功能较基础,较简单,可供学习和参考使用.
💻 JAVA
字号:
/**
 *   Importing the basic packages required by various classes during 
 *	 the execution of the program.	
 */ 
  import java.io.InputStream;
  import java.util.Hashtable;
  import java.util.Enumeration;
  import com.sun.kjava.*;
  import java.lang.*;
  import javax.microedition.io.*;
  import java.io.*;
  import java.util.*;
 
 /**
  *  The packages below are included because these packages are 
  *	 required by the XML classes.
  */ 

 import org.kxml.*;
 import org.kxml.io.*;
 import org.kxml.parser.*;
 
  /**
   *  Declaration of a class peer_application. This is the first class 
   *  of peer to peer aplication project which welcomes the user to the
   *  application and on pressing the start button the user can go on to 
   *  the actual task of the application. 	
   */ 
 public class peer_application extends Spotlet 
 {
 
  /**
   *   Declaraing variables for the xmlparser and parse event(Where 
   *   the call backs after the parsing will be stored.	
   */  
  	AbstractXmlParser xmlparser;
  	ParseEvent event;

  /**
   *   Declaring the variable for the DataInputStream (for reading 
   *   the ASP, For Buttons (Start and Exit), for TextBox (message)etc.   	
   */ 
  	
  	DataInputStream din;
  	private Button start;
  	private Button exit; 
  	private TextBox message;

 /**
  *  Declaring three important variables ,ip_addresses of vector type 
  *	 to store the ip_addresses, a vector type variable users_connected
  *	 which as the name suggests consists a list of users connected and 
  *	 a String variable of the type text_for_display which consists the 
  *	 text to be displayed on to the ScrollTextBox.
  */ 
  	Vector ip_addresses;
  	Vector users_connected;
  	String text_for_display;
  	int counter = 1;

 /**
  *  This is the main program which indicates the starting of the peer 
  *	 to peer application by calling the class peer_application. 
  */ 
  	public static void main(String args[])throws Exception
      {
  		peer_application peer_to_peer = new peer_application();
  	}
  

 // Constructor for the class peer_application. 	
  	public peer_application()
  	{
 //	To make the spotlet as the current spotlet..
  		register(NO_EVENT_OPTIONS);
  		Graphics graphics = Graphics.getGraphics();	
  	    
 /**
  *  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()).
  */  
  		graphics.resetDrawRegion();
          graphics.clearScreen();	
 /**
  *  Displaying a string (Title) at a specified position by using 
  *	 the member function of the Graphics class
  */ 
  		graphics.drawString("Peer to Peer System..",25,10);
  		
 /**
  *  Initializing the button object Start, placing it on the screen 
  *	 at the appropriate position and then painting(drawing it).
  */ 
  		start = new Button("Start",30,145);
  		start.paint();
  		
 /**
  *  Initializing the button object exit, placing it on the screen 
  *	 at the appropriate position and then painting(drawing it).
  */ 	
 		exit = new Button("Exit",70,145);
 		exit.paint();
 	}	// End of constructor peer_application.
 		 
 /**
  *  The code below pertains to event handling. 
  */ 
 	public void penDown(int x, int y)    
 	{
 /**
  *  If start button is pressed..Then the current spotlet is unregistered 
  *	 and a method of the name startReading is called. This method is res-
  *  -ponsible for calling the ASP from the server when the ASP is read 
  *  an XMLParser is called which will parse the XML Data a method 
  *  parseData() is called which will is called by the XML parser to
  *	 generate callbacks from the XML parsing. These Callbacks are stored 
  *	 three variables earlier declared (ip_addresses, users_connected and
  *	 text_for_display).
  */ 
  		if(start.pressed(x,y))
  		 {
 // To show a new class fist unregister all the controls of the 
 // existing class
  			unregister();
 // Call to a method startReading which will request the server for all 
 // the users connected at a particular time using the ASP.
  			startReading();
 // The XMLParser will generate the Call-backs in the method parseData.
  			ip_addresses = parseData();
  				
 /**
  *  A new Class userlist_screen is called which displays the users 
  *  connected  at a particular instant of time, the class takes as its
  *	 parameters a Text_for_display string variable (which consists the no.
  *	 of users connected), a vector variable ip_addresses (which has the ip 
  *  addresses of the all the users connected at a particular instant of
  *  time) and a vector variable containing the users_connected.
  */  	
  			new userlist_screen(text_for_display, ip_addresses, users_connected, false,null);
 					
  		 }
 /**
  *  If exit button is pressed then the game is closed and the 
  *	 control is returned to the OS.
  */ 
  		 else
  		 {
  		    System.exit(0);
  		 }
 	} // End of event Handling method. 
 		
 	public void startReading()
 	{
 	  try
 	  {  
  // The three variables where the information is to be stored are
  // initialized.
 			text_for_display = "";
 			ip_addresses = new Vector();
 			users_connected = new Vector();
 
 // Using the Connector class of CLDC an InputStream is opened on 
 // the ASP
 			din = Connector.openDataInputStream("testhttp://harpreet/p2p/userlist.asp");
 
// The InputStream opened on the URL is passed on to the xmlparser.
 			xmlparser = new XmlParser(new InputStreamReader(din));
 	  }
 	  catch(IOException e)
 	  {
 	  System.out.println("Exception Occured while reading the XML from asp or while passing it to the XML parser");
 	  }
 	} // End of the method Startreading()..
 
// Function parseData (it will hold the callbacks generated by the 
// XMLParsing.
 	Vector parseData()
 	{
	// Move in infinite loop till the XML document ends..
 	  do
 	   {
 	    try
 	     {	
 			event = xmlparser.read ();
 	
 			if(event.getType()==Xml.START_TAG)
 			{
 // The StartTag is identified.
 				StartTag stag = (StartTag)event;
 			 	
 				String name = stag.getName();
 				
// If the Tag encountered is userinfo then the Attributes of the 
// tag are taken using the function getValue(). and are added to 
// there appropriate position.
 				if (name.equals("userinfo"))
 				{
 					
  				 text_for_display = text_for_display +counter + ". " +stag.getValue("username")+"\n"; 
  				 ip_addresses.addElement((Object)stag.getValue("ip"));
  				 users_connected.addElement((Object)stag.getValue("username"));
  				 counter++;
  				}
  					
  			}
  						
   	    }
  		catch(IOException ex)
  		{		
  			System.out.println("Exception occured");
  		  }
  	  }
    	 while (!(event instanceof EndDocument)); // end of document 
  		
  	 System.out.println("**** END OF DOCUMENT ****");

   // The vector Ip_address is returned to the calling program.       

  	return (ip_addresses);	
     }
  
 // A function  to return the TextforDisplay to the calling program
  	String returnTextForDisplay()
  	{
  	  return (text_for_display);
  	}

 // A function  to return the vector usersConnected to the calling program
  	Vector returnUsersConnected()
  	{
  	  return (users_connected);
  	}
  
  } // End of class...	 
  		  

⌨️ 快捷键说明

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