/*
* 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.response;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.opencloudb.MycatServer;
import org.opencloudb.manager.ManagerConnection;
import org.opencloudb.net.FrontendConnection;
import org.opencloudb.net.NIOConnection;
import org.opencloudb.net.NIOProcessor;
import org.opencloudb.net.mysql.OkPacket;
import org.opencloudb.util.SplitUtil;
/**
* @author mycat
*/
public final class KillConnection {
private static final Logger logger = Logger.getLogger(KillConnection.class);
public static void response(String stmt, int offset, ManagerConnection mc) {
int count = 0;
List<FrontendConnection> list = getList(stmt, offset, mc);
if (list != null)
for (NIOConnection c : list) {
StringBuilder s = new StringBuilder();
logger.warn(s.append(c).append("killed by manager").toString());
c.close("kill by manager");
count++;
}
OkPacket packet = new OkPacket();
packet.packetId = 1;
packet.affectedRows = count;
packet.serverStatus = 2;
packet.write(mc);
}
private static List<FrontendConnection> getList(String stmt, int offset, ManagerConnection mc) {
String ids = stmt.substring(offset).trim();
if (ids.length() > 0) {
String[] idList = SplitUtil.split(ids, ',', true);
List<FrontendConnection> fcList = new ArrayList<FrontendConnection>(idList.length);
NIOProcessor[] processors = MycatServer.getInstance().getProcessors();
for (String id : idList) {
long value = 0;
try {
value = Long.parseLong(id);
} catch (NumberFormatException e) {
continue;
}
FrontendConnection fc = null;
for (NIOProcessor p : processors) {
if ((fc = p.getFrontends().get(value)) != null) {
fcList.add(fc);
break;
}
}
}
return fcList;
}
return null;
}
}