package com.tronacademy.phantom.messaging;

import java.io.ByteArrayOutputStream;
import java.util.Queue;

/**
 * <p>
 * Handler for an outgoing transmission process.
 * </p>
 * 
 * @author George Xian
 * @since 2015-05-04
 */
public class OutgoingTransaction {
	
	private final Queue<ByteArrayOutputStream> mOutgoingStream;
	private final int mJobSize;
	
	public final int mPriority;
	
	/**
	 * The queue of byte streams to transmit over the network.
	 * 
	 * @param name     String name of the transaction process.
	 * @param stream   Byte stream to transmit.
	 * @param priority Priority of the transmission (lower is higher)
	 */
	public OutgoingTransaction(String name, Queue<ByteArrayOutputStream> stream, int priority) {
		mOutgoingStream = stream;
		mJobSize = mOutgoingStream.size();
		
		mPriority = priority;
	}
	
	/**
	 * @return Flag indicating whether transaction is complete.
	 */
	public boolean hasComplete() {
		return mOutgoingStream.size() > 0;
	}
	
	/**
	 * @return Next byte stream of this transaction to transmit.
	 */
	public ByteArrayOutputStream getNext() {
		return mOutgoingStream.remove();
	}
	
	/**
	 * @return Completion of this transaction in percent.
	 */
	public int getProgress() {
		return 100*mOutgoingStream.size() / mJobSize;
	}

}

@@ -65,39 +65,204 @@
 rray
-OutputStream;%0Aimport java.util.
+InputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.InputStream;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Queue;%0Aimport java.util.concurrent.LinkedBlocking
 Queu
@@ -516,21 +516,22 @@
 ze;%0A%09%0A%09p
-ublic
+rivate
  final i
@@ -544,16 +544,45 @@
 iority;%0A
+%09private final String mName;%0A
 %09%0A%09/**%0A%09
@@ -1008,16 +1008,585 @@
 iority;%0A
+%09%09mName = name;%0A%09%7D%0A%09%0A%09public OutgoingTransaction(OutgoingTransaction trans) %7B%0A%09%09mJobSize = trans.mJobSize;%0A%09%09// TODO: clone() is not working%0A%09%09mOutgoingStream = new LinkedBlockingQueue%3CByteArrayOutputStream%3E(trans.showContents());%0A%09%09%0A%09%09mName = trans.mName;%0A%09%09mPriority = trans.mPriority;%0A%09%09%0A%09%7D%0A%09%0A%09/**%0A%09 * @return String name for this transaction.%0A%09 */%0A%09public String getName() %7B%0A%09%09return mName;%0A%09%7D%0A%09%0A%09/**%0A%09 * @return Priority for this transaction.%0A%09 */%0A%09public int getPriority() %7B%0A%09%09return mPriority;%0A%09%7D%0A%09%0A%09public int getNumPackets() %7B%0A%09%09return mOutgoingStream.size();%0A
 %09%7D%0A%09%0A%09/*
@@ -1788,24 +1788,42 @@
  to transmit
+, null if complete
 .%0A%09 */%0A%09publ
@@ -1825,28 +1825,18 @@
 %09public 
-ByteArrayOut
+In
 putStrea
@@ -1862,31 +1862,69 @@
 urn 
-mOutgoingStream.remove(
+new ByteArrayInputStream(mOutgoingStream.poll().toByteArray()
 );%0A%09
@@ -2072,10 +2072,197 @@
 ze;%0A%09%7D%0A%0A
+%09/**%0A%09 * @return A copy of the contents of this transaction.%0A%09 */%0A%09public List%3CByteArrayOutputStream%3E showContents() %7B%0A%09%09 return new ArrayList%3CByteArrayOutputStream%3E(mOutgoingStream);%0A%09%7D%0A
 %7D%0A

Change visibility of OutgoingTransaction members