import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;

public class ListenAgent {

//			private String Id;
//			private String Passwd;
//			private String Ip;
//			private Boolean IsLogin;

	public String ListenLoginIp(String Id, String Passwd, String Ip)

	{
				
			Connection con=null;
			Statement stmt1=null;
			Statement stmt2=null;
			ResultSet rs1=null;

			try{
			//Loading driver class into Application JVM
			Class.forName("sun.jdbc.driver.OracleDriver");
			//When the Driver is loaded in Memory, register this driver into usable driver 
			//     With using java.sql.DriverManager class.
			con = DriverManager.getConnection("jdbc:oracle:thin:@203.248.127.241:oracle", "scott", "tiger");
			//for program to get Object of Statement, call the createStament() Method.
			stmt1 = con.createStatement();
			stmt2 = con.createStatement();

			rs1 = stmt1.executeQuery("select ip from member_regist where id='" + Id + "' and password='" + Passwd + "'");
//			while(rs1.next()){
//					if(String(tmp=rs1.getString("ip")) = "Ip");
//					else
//					{
							stmt2.executeQuery("update table member_regist set ip='" + Ip + "' where id='" + Id + "' and password='" + Passwd + "'");
//					}
//			}
			return "<Result>True</Result>";
			
			}	catch(ClassNotFoundException e){
			//Error in loading Driver
				return "<result>False</result>";
			}	catch(SQLException e){
			System.out.println("SQLException caught: " + e.getMessage()); //Error in Getting connection 
				return "<result>False</result>";
			}
			finally{
					// Always Close the connection of database. 
					try{
							if(con != null) con.close();
							}
					catch(SQLException ignored){;}
			}
		}

		
		public String ListenNewMail(String Id, String NewMail)
		{
				StringBuffer buffer = new StringBuffer();
				StringBufferInputStream sbi = new StringBufferInputStream(NewMail);
				InputStreamReader isr = new InputStreamReader(sbi,"UTF-8");
				BufferedReader br = new BufferedReader(isr);

				int tmp;
				while((tmp=br.read())>-1)
				{
						buffer.append((char)tmp);
				}
				br.close();
				String result = buffer.toString();
				try{
						String fileName = "c:/server/"+Id+"_NewMail.xml";
						File file = new File(fileName);
						BufferedWriter bw = new BufferedWriter(new FileWriter(file.getPath(),true));
						bw.write(result);
				}catch(IOException e){
						System.out.println("IO Exception happens!!" + e);
				}

				return "<Result>True</Result>" ;
		};


};         

