package hh.dejsem.net;

import hh.dejsem.K;
import hh.dejsem.SendUriNG;
import hh.dejsem.fm.NetTaskRef;
import hh.lib.D;

/**
 * expose to HTTP on server (copy asynchronously)
 */
public class ExposeStream extends LongAsyncTask {

	NetConnSrv netConn;
	String uriPath;

	public ExposeStream(D d, NetTaskRef netTaskRef) throws Exception  {
		super(d, netTaskRef);
		int port = args.getInt(K.PORT_KEY, 0);
		netConn = new NetConnSrv(this.d, String.format("[%d]", port), netTaskRef);
	}

	@Override
	protected Void doInBackground(Void... params) {
		super.doInBackground();
		if(netTaskRef.go)
			try { uriPath = netConn.pushStreamExpose(); }  // expose data and get exposed path
			catch(Exception e) { if(!K.End.class.isInstance(e)) abend("EXPOSE stream to http on server", e); }
			finally { try { netConn.connClose(); } catch(Exception e) {} }
		return null;
	}

	@Override
	void completePostExecute() { SendUriNG.decideSendUri(uriPath); }
}
