use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class Agent method processOtherTask.
public void processOtherTask(final Task task) {
final Object obj = task.get();
if (obj instanceof Response) {
if (System.currentTimeMillis() - _lastPingResponseTime > _pingInterval * _shell.getPingRetries()) {
s_logger.error("Ping Interval has gone past " + _pingInterval * _shell.getPingRetries() + ". Won't reconnect to mgt server, as connection is still alive");
return;
}
final PingCommand ping = _resource.getCurrentStatus(getId());
final Request request = new Request(_id, -1, ping, false);
request.setSequence(getNextSequence());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Sending ping: " + request.toString());
}
try {
task.getLink().send(request.toBytes());
//if i can send pingcommand out, means the link is ok
setLastPingResponseTime();
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send request: " + request.toString());
}
} else if (obj instanceof Request) {
final Request req = (Request) obj;
final Command command = req.getCommand();
if (command.getContextParam("logid") != null) {
MDC.put("logcontextid", command.getContextParam("logid"));
}
Answer answer = null;
_inProgress.incrementAndGet();
try {
answer = _resource.executeRequest(command);
} finally {
_inProgress.decrementAndGet();
}
if (answer != null) {
final Response response = new Response(req, answer);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Watch Sent: " + response.toString());
}
try {
task.getLink().send(response.toBytes());
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send response: " + response.toString());
}
}
} else {
s_logger.warn("Ignoring an unknown task");
}
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class Agent method sendStartup.
public void sendStartup(final Link link) {
final StartupCommand[] startup = _resource.initialize();
if (startup != null) {
final Command[] commands = new Command[startup.length];
for (int i = 0; i < startup.length; i++) {
setupStartupCommand(startup[i]);
commands[i] = startup[i];
}
final Request request = new Request(_id != null ? _id : -1, -1, commands, false, false);
request.setSequence(getNextSequence());
if (s_logger.isDebugEnabled()) {
s_logger.debug("Sending Startup: " + request.toString());
}
lockStartupTask(link);
try {
link.send(request.toBytes());
} catch (final ClosedChannelException e) {
s_logger.warn("Unable to send reques: " + request.toString());
}
}
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class Agent method sendRequest.
@Override
public AgentControlAnswer sendRequest(final AgentControlCommand cmd, final int timeoutInMilliseconds) throws AgentControlChannelException {
final Request request = new Request(getId(), -1, new Command[] { cmd }, true, false);
request.setSequence(getNextSequence());
final AgentControlListener listener = new AgentControlListener(request);
registerControlListener(listener);
try {
postRequest(request);
synchronized (listener) {
try {
listener.wait(timeoutInMilliseconds);
} catch (final InterruptedException e) {
s_logger.warn("sendRequest is interrupted, exit waiting");
}
}
return listener.getAnswer();
} finally {
unregisterControlListener(listener);
}
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class AgentAttache method sendNext.
protected synchronized void sendNext(final long seq) {
_currentSequence = null;
if (_requests.isEmpty()) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(seq, "No more commands found"));
}
return;
}
Request req = _requests.pop();
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(req.getSequence(), "Sending now. is current sequence."));
}
try {
send(req);
} catch (AgentUnavailableException e) {
if (s_logger.isDebugEnabled()) {
s_logger.debug(log(req.getSequence(), "Unable to send the next sequence"));
}
cancel(req.getSequence());
}
_currentSequence = req.getSequence();
}
use of com.cloud.agent.transport.Request in project cloudstack by apache.
the class AgentManagerImpl method send.
@Override
public long send(final Long hostId, final Commands commands, final Listener listener) throws AgentUnavailableException {
final AgentAttache agent = getAttache(hostId);
if (agent.isClosed()) {
throw new AgentUnavailableException("Agent " + agent.getId() + " is closed", agent.getId());
}
final Command[] cmds = checkForCommandsAndTag(commands);
final Request req = new Request(hostId, agent.getName(), _nodeId, cmds, commands.stopOnError(), true);
req.setSequence(agent.getNextSequence());
agent.send(req, listener);
return req.getSequence();
}
Aggregations