package cc;

import cc.idl.*;
import cc.monitors.*;

import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class Client{
    static UDPSvr UDPSvrImpl;
	static int id = 0;
	static int size = 0;
	static int delay = 0;
        
	static void setParameters(int auxSize, int auxDelay){			
		//Setup size limits					
		if (auxSize > Parameters.getMaxSize())
			auxSize = Parameters.getMaxSize();
		else if (auxSize < Parameters.getMinSize())
			auxSize = Parameters.getMinSize();
				
		//Setup delay limits					
		if (auxDelay > Parameters.getMaxDelay())
			auxDelay = Parameters.getMaxDelay();
		else if (auxDelay < Parameters.getMinDelay())
			auxDelay = Parameters.getMinDelay();
		
		//Request flow adjustment, if need					
		if (auxSize != size){
			UDPSvrImpl.setSize(id, auxSize);
			size = auxSize;			
		}
		if (auxDelay != delay){
			UDPSvrImpl.setDelay(id, auxDelay);
			delay  = auxDelay;			
		}
	}

	static int getSize(){
		return size;
	}
	
	static int getDelay(){
		return delay;
	}
	
	public static void requestPackets(String bucketPackets){
		UDPSvrImpl.requestPackets(id, bucketPackets);
	}

	public static void alive(){
		UDPSvrImpl.alive(id);
	}

    public static void main(String args[]) {
     
        Parameters.parseArgs(args);
		Receiver receiver;
        
        try{
            // create and initialize the ORB
            ORB orb = ORB.init(args, null);
            
            // get the root naming context
            org.omg.CORBA.Object objRef = 
            orb.resolve_initial_references("NameService");
            
            // Use NamingContextExt instead of NamingContext. This is 
            // part of the Interoperable naming Service.  
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
            
            // resolve the Object Reference in Naming
            String name = "UDP_STREAM";
            UDPSvrImpl = UDPSvrHelper.narrow(ncRef.resolve_str(name));
            
			//Starts receiver
			receiver = new Receiver();
			receiver.start();
			
			String host = receiver.getIP();
			int port = receiver.getPort();
			
			size = Parameters.getSize();
			delay = Parameters.getDelay(); 

			//Requests a flow
			id = UDPSvrImpl.request (host, port, size, delay);
			if (id == -1) return;
			
			
			System.out.println("Ready to receive at: " + host + ":" + port);
            
            //Attach monitors
			receiver.attach(new LogMonitor());
			if (Parameters.getResend().equals("on"))
				receiver.attach(new BufferMonitor());
//			if (Parameters.getProfile().equals("opt"))
//				receiver.attach(new StreamingMonitor());

			//Configure execution duration			
			if (Parameters.getDuration() == 0){
				while (System.in.read() == -1);
			}
			else{
				Thread.sleep(Parameters.getDuration() * 1000);            
			}

			//Detach all monitors
			receiver.detachAll();
			
			//Stop receiver
			receiver.stopStreaming();
			
			//Stop sender
			UDPSvrImpl.stop(id);
		} catch (Exception e) {
			System.out.println("ERROR : " + e) ;
			e.printStackTrace(System.out);
		}
    }
   
}

