package Sight.Agents;
import Sight.io.*;
import Sight.io.GUI.*;
import Sight.Agents.util.*;
public class Request extends Thread
{
public Object result = null;
public Object q = null;
public String key = null;
public boolean err = false;
public boolean end = false;
public Agent agent;
private Request()
{
};
public Request(Agent _agent, Object _q, String _key)
{ this();
agent = _agent;
q = _q;
result = null;
err = false;
end = false;
key = _key;
result = null;
};
public String getId()
{ return key;
};
public void run()
{
String kn = key;
String nn = agent.getAgentName();
try {
if (kn == null) kn = "";
if (kn.length()>30) kn = kn.substring(0,30);
agent.p("Query "+
agent.getClass().getName()+" started in separate thread.");
Pind.setAgent(agent);
Pind.Net(nn,Pind.PROCESSING, kn);
try {
result = agent.go(q, key);
} catch (Exception exc)
{ result = null;
err = true;
end = true;
agent.p("Query "+
agent.getClass().getName()+" finished with errors. ");
Output.e(exc);
Pind.Net(nn,Pind.BUG, kn);
return;
};
agent.p("Query "+
agent.getClass().getName()+" ready.");
Pind.Net(nn,Pind.I, kn);
end = true;
}
catch (java.lang.ThreadDeath de)
{
agent.p("The task was killed by user.");
end = true;
err = true;
result = null;
Pind.Net(nn, Pind.STOP, "Stopped "+kn);
}
catch (Sight.Agents.Signals.KillTaskMessage kim)
{
agent.p("The task was killed by user.");
end = true;
err = true;
result = null;
Pind.Net(nn, Pind.STOP, "Killed "+kn);
}
catch (Throwable e)
{ end = true;
err = true;
result = null;
e.printStackTrace();
}
};
public long AgentTimeOut = 42* 60*1000;
public Object getResult()
{
return getResult(AgentTimeOut);
};
public Object getResult(long waitAtMost_msec)
{ if (err) return null;
if (result!=null && !err)
return result;
String wId = agent.getClass().getName();
if (result == null)
agent.p("Query "+ wId +" still not finished, waiting.");
Pind.Net("still not finished ",Pind.INFO_WAITING,
agent.getClass().getName());
int SleepTime = 10;
long st, ct;
st = System.currentTimeMillis();
ThreadInfoLabel info = Pind.getThreadLabel(this);
while (!end)
{
if (info!=null)
{
if (info.abandon)
{
err = true;
info.abandon = false;
info.setText2("Abandoned.");
info.setIcon(Pind.ABANDON);
Sight.io.Output.a("Thread abandoned by user command.");
return null;
}
};
if (err) return null;
try
{
Sight.util.Sleeper.Sleep(SleepTime);
SleepTime = SleepTime*2;
if (SleepTime>1000) SleepTime = 1000;
ct = System.currentTimeMillis();
if ((ct-st)>waitAtMost_msec)
{ Output.e("Request for "+agent.getAgentName()+" lasted for so long. Null returned.");
info.setText2("Abandoned, too long");
info.setIcon(Pind.ABANDON);
end = true;
return null;
};
}
catch (InterruptedException ex)
{
}
};
return result;
};
public static Request submit(Agent _agent, Object _q, String key)
{
Request bt = new Request(_agent, _q, key);
bt.start();
return bt;
};
};