/*
* Copyright 2012-2015 org.opencloudb.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opencloudb.server.response;
import org.apache.log4j.Logger;
import org.opencloudb.MycatServer;
import org.opencloudb.config.ErrorCode;
import org.opencloudb.net.mysql.ErrorPacket;
import org.opencloudb.net.mysql.HeartbeatPacket;
import org.opencloudb.net.mysql.OkPacket;
import org.opencloudb.server.ServerConnection;
import org.opencloudb.util.TimeUtil;
/**
* @author mycat
*/
public class Heartbeat {
private static final Logger HEARTBEAT = Logger.getLogger("heartbeat");
public static void response(ServerConnection c, byte[] data) {
HeartbeatPacket hp = new HeartbeatPacket();
hp.read(data);
if (MycatServer.getInstance().isOnline()) {
OkPacket ok = new OkPacket();
ok.packetId = 1;
ok.affectedRows = hp.id;
ok.serverStatus = 2;
ok.write(c);
if (HEARTBEAT.isInfoEnabled()) {
HEARTBEAT.info(responseMessage("OK", c, hp.id));
}
} else {
ErrorPacket error = new ErrorPacket();
error.packetId = 1;
error.errno = ErrorCode.ER_SERVER_SHUTDOWN;
error.message = String.valueOf(hp.id).getBytes();
error.write(c);
if (HEARTBEAT.isInfoEnabled()) {
HEARTBEAT.info(responseMessage("ERROR", c, hp.id));
}
}
}
private static String responseMessage(String action, ServerConnection c, long id) {
return new StringBuilder("RESPONSE:").append(action).append(", id=").append(id).append(", host=")
.append(c.getHost()).append(", port=").append(c.getPort()).append(", time=")
.append(TimeUtil.currentTimeMillis()).toString();
}
}