  // written by: Asim Alvi
  // tested by: Asim Alvi
  // debugged by: Asim Alvi

package com.example.stockprophit;
import java.io.*;
import java.net.*;

import android.os.AsyncTask;
import android.util.Log;


public class Client extends AsyncTask {

	public Integer localport; 
	
	public InetAddress RPublicIPB=null;
	
	private String stockname;
	
	
	public Client(String name){
		stockname = name;
	}
	
	public void sendInfo(Socket socket){
		
		ObjectInputStream ois=null;
		    try {
				ois = new ObjectInputStream(socket.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e(Client.class.toString(), "IOException1");
				cancel(true); 
			}
			ObjectOutputStream oos=null;
		
			try {
				oos = new ObjectOutputStream(socket.getOutputStream());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				Log.e(Client.class.toString(), "IOException2");
				cancel(true); 
			}
	
		
		try {
			oos.writeObject(stockname);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(Client.class.toString(), "IOException3");
			cancel(true);
		} 
	
		try {
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(Client.class.toString(), "IOException4");
			cancel(true);
		}
		
		System.out.println("Success");
	
	}
	/*
	public void getEP(Socket socket){
		 ObjectOutputStream oos=null;
			
			try {
				oos = new ObjectOutputStream(socket.getOutputStream());
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				System.exit(-1); 
			}
		
		ObjectInputStream ois=null;
		    try {
				ois = new ObjectInputStream(socket.getInputStream());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(-1); 
			}
			try{
			RLocalIPB= (InetAddress) ois.readObject();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err.println("I/O Exception");
				System.exit(-1);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err.println("Class NOt Found");
				System.exit(-1);
			}
			
			try {
				RLocalPortB= (Integer) ois.readObject();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err.println("I/O Exception");
				System.exit(-1);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.err.println("Class NOt Found");
				System.exit(-1);
			}
			
			try{
				RPublicIPB= (InetAddress) ois.readObject();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.err.println("I/O Exception");
					System.exit(-1);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.err.println("Class NOt Found");
					System.exit(-1);
				}
				try {
					RPublicPortB= (Integer) ois.readObject();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.err.println("I/O Exception");
					System.exit(-1);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.err.println("Class NOt Found");
					System.exit(-1);
				}
			
			
	}
	
	*/
		
	public static void waittime (int n){
		long t0,t1;
		t0=System.currentTimeMillis();
		do{
			t1=System.currentTimeMillis();
		}
		while (t1-t0<n);
	}

	@Override
	protected Object doInBackground(Object... arg0) {
		Socket socket=null;
		int port=62000;
		InetAddress IP=null;	
		try {
			IP= InetAddress.getByName("172.16.69.11");
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			Log.e(Client.class.toString(), "Failed to connect unknown host 1");
			cancel(true);
		}
		
		
		try {
			socket= new Socket(IP,port);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			Log.e(Client.class.toString(), "Failed to connect unknown host 2");
			//System.err.println("Failed to connect unknown host"); 
		
			cancel(true);
		} catch (IOException e) {
			
			e.printStackTrace();
			Log.e(Client.class.toString(), "Failed to connect to port"); 
			cancel(true);
		} 
	
		// System.out.println(LocalIPA.toString()); 
	 	localport=socket.getLocalPort();
	 	//System.out.println(localport); 
				
	 	sendInfo(socket); 
	 	/**getEP(socket);**/
	 	//System.out.println(RLocalIPB.toString()+" "+RLocalPortB+" "+RPublicIPB.toString()+" "+RPublicPortB); 
	 	try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(Client.class.toString(), "IOException 0"); 
			cancel(true);
		}
	 	return -1;
	}
	

}