package tst.print;
import java.util.Calendar;
import tcg.print.ILinePrinter;
import tcg.print.IPrintable;
import tcg.print.PrintableFactory;
import tcg.print.TcpLinePrinter;
public class TestTcpPrinterThreaded
{
/**
* @param args
*/
public static void main(String[] args)
{
String printer_host = "192.168.10.200";
int printer_port = 9100;
//initialize the calendar instance. in Unix it often throws exception because of
//unknown locale
try
{
Calendar.getInstance();
}
catch(Exception ex)
{
//ignore
}
PrintingThread[] threadArr = new PrintingThread[10];
for (int i = 0; i<threadArr.length; i++)
{
threadArr[i] = new PrintingThread(printer_host, printer_port);
threadArr[i].start();
}
try
{
Thread.sleep(5000);
}
catch(Exception ex)
{
//ignore
}
for (int i = 0; i<threadArr.length; i++)
{
threadArr[i].stop(0);
}
}
}
class PrintingThread implements Runnable
{
protected static long DEF_PRECISION_MILLIS = 1000;
private Thread thread_ = null;
private boolean isRunning_ = false;
private String printerHost_ = "";
private int printerPort_ = 0;
ILinePrinter printer = null;
IPrintable printable = null;
private int counter_ = 1;
public PrintingThread(String host, int port)
{
if (host != null) printerHost_ = host;
printerPort_ = port;
printer = new TcpLinePrinter(printerHost_, printerPort_);
if (!printer.ping())
{
System.out.println("Remote printer " + printerHost_ + ":" + printerPort_
+ " is not available.");
printer = null;
}
printable = PrintableFactory.createPrintable("OKI");
}
/**
* How often the thread execution is repeated.
*/
protected long precisionMillis = DEF_PRECISION_MILLIS;
/**
* Called before the thread is started.
*/
protected void _onStart()
{
//nothing
}
/**
* Called before the thread is stopped.
*/
protected void _onStop()
{
//nothing
}
/**
* Called in the beginning of the thread execution
*/
protected void _initial()
{
//nothing
}
/**
* Called at the end of the thread execution
*/
protected void _final()
{
//nothing
}
/**
* Periodic execution inside the trhead
*/
protected void _doWork()
{
if (printer == null) return;
printable.clear();
printable.append(Calendar.getInstance().getTime().toString() + ": Thread "
+ this.getId() + " say Hello World (" + counter_++ + ")!\n");
printer._print(printable);
}
/**
* Start the detached thread. A new execution thread is created.
*/
public synchronized void start()
{
_onStart();
if (thread_ != null && (!thread_.isAlive() || !isRunning_))
{
//has terminated. reset the thread
thread_ = null;
}
//start a new thread
if (thread_ == null)
{
thread_ = new Thread(this);
thread_.start();
}
}
/**
* Stop the detached thread.
* @param millis - how long to wait until the thread is terminated
*/
public synchronized void stop(long millis)
{
_onStop();
if (thread_ != null)
{
isRunning_ = false;
//interrupt the thread in case it is sleeping
thread_.interrupt();
try
{
thread_.join(millis);
}
catch (InterruptedException inte)
{
//ignore
}
//reset the thread
thread_ = null;
}
}
/**
* Check if the thread is already running
* @return - true is the thread is running, false otherwise
*/
public boolean isRunning()
{
return isRunning_;
}
/**
* Main thread execution
*/
public void run()
{
_initial();
long starttime = 0;
long idletime = 0; //must not be unsigned!!!
isRunning_ = true;
while (isRunning_)
{
starttime = Calendar.getInstance().getTimeInMillis();
//actual work
_doWork();
//check for idle timeout
idletime = precisionMillis - (Calendar.getInstance().getTimeInMillis() - starttime);
if (idletime > 0)
{
try
{
Thread.sleep(idletime);
}
catch (InterruptedException ie)
{
//ignore
}
}
}
//reset flag.
isRunning_ = false;
_final();
}
/**
* Get the thread id of current running thread.
* @return - the thread id if the thread is running, 0 otherwise
*/
public long getId()
{
if (thread_ == null) return 0;
return thread_.getId();
}
}