add load agents from pool

This commit is contained in:
Tienan Chen 2013-09-06 20:32:21 +08:00
parent 635ef7606c
commit 144b3e13df
6 changed files with 226 additions and 131 deletions

View File

@ -1,100 +1,158 @@
package org.bench4q.web.controller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.model.agent.AgentResponseModel;
import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.entity.db.Agent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("accessToken")
public class AgentActionController extends BaseActionController {
private AgentResponseModel extractAgentResponseModel(String content)
throws JAXBException {
AgentResponseModel resultModel = new AgentResponseModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (AgentResponseModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
private String _marshalAgentToString(Agent agent) throws JAXBException {
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = JAXBContext.newInstance(agent.getClass())
.createMarshaller();
marshaller.marshal(agent, stringWriter);
return stringWriter.toString();
}
@RequestMapping("addAgenttoPool.do")
public @ResponseBody String addAgenttoPool (HttpServletRequest request,@ModelAttribute("accessToken") String accessToken) throws IOException, JAXBException {
System.out.println("enter add port");
String urlString = masterIP + "agentManage/addAgentToPool";
HttpRequester httpRequester = new HttpRequester();
String hostname = request.getParameter("hostname");
String maxLoad = request.getParameter("maxLoad");
int maxLoadnum = Integer.parseInt(maxLoad);
Agent agent = new Agent();
agent.setHostName(hostname);
agent.setMaxLoad(maxLoadnum);
agent.setRemainLoad(maxLoadnum);
agent.setPort(6565);
String content = _marshalAgentToString(agent);
Map<String, String> properties = new HashMap<String, String>();
properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
HttpResponse httpResponse = httpRequester.sendPostXml(urlString, content, properties);
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println("success");
return "success";
} else {
System.out.println("failure");
return "failure";
}
}
@RequestMapping("removeAgentfromPool.do")
public @ResponseBody String removeAgentfromPool (HttpServletRequest request,@ModelAttribute("accessToken") String accessToken) throws IOException, JAXBException {
System.out.println("enter remove port");
String urlString = masterIP + "agentManage/removeAgentFromPool";
HttpRequester httpRequester = new HttpRequester();
String hostName = request.getParameter("hostName");
Map<String, String> properties = new HashMap<String, String>();
properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
Map<String, String> params = new HashMap<String, String>();
params.put("hostName", hostName);
HttpResponse httpResponse = httpRequester.sendGet(urlString,
params, properties);
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println("success");
return "success";
} else {
System.out.println("failure");
return "failure";
}
}
}
package org.bench4q.web.controller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.bench4q.master.api.model.agent.AgentResponseModel;
import org.bench4q.master.communication.HttpRequester;
import org.bench4q.master.communication.HttpRequester.HttpResponse;
import org.bench4q.master.entity.db.Agent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("accessToken")
public class AgentActionController extends BaseActionController {
private final String baseUrl = this.getMasterIP() + "/agentManage";
private String getBaseUrl() {
return baseUrl;
}
@RequestMapping("addAgenttoPool.do")
public @ResponseBody
String addAgenttoPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken)
throws IOException, JAXBException {
System.out.println("enter add port");
String urlString = masterIP + "agentManage/addAgentToPool";
HttpRequester httpRequester = new HttpRequester();
String hostname = request.getParameter("hostname");
String maxLoad = request.getParameter("maxLoad");
int maxLoadnum = Integer.parseInt(maxLoad);
Agent agent = new Agent();
agent.setHostName(hostname);
agent.setMaxLoad(maxLoadnum);
agent.setRemainLoad(maxLoadnum);
agent.setPort(6565);
String content = _marshalAgentToString(agent);
Map<String, String> properties = new HashMap<String, String>();
properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
HttpResponse httpResponse = httpRequester.sendPostXml(urlString,
content, properties);
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println("success");
return "success";
} else {
System.out.println("failure");
return "failure";
}
}
@RequestMapping("removeAgentfromPool.do")
public @ResponseBody
String removeAgentfromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken)
throws IOException, JAXBException {
System.out.println("enter remove agent");
String urlString = masterIP + "agentManage/removeAgentFromPool";
HttpRequester httpRequester = new HttpRequester();
String hostName = request.getParameter("hostName");
Map<String, String> properties = new HashMap<String, String>();
properties.put(AUTH_HEADER_PROPERTY, this.ACCES_TOCKEN_STARTER
+ accessToken);
Map<String, String> params = new HashMap<String, String>();
params.put("hostName", hostName);
HttpResponse httpResponse = httpRequester.sendGet(urlString, params,
properties);
AgentResponseModel agentResponseModel = extractAgentResponseModel(httpResponse
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println("success");
return "success";
} else {
System.out.println("failure");
return "failure";
}
}
public @ResponseBody
String loadAgentsFromPool(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken)
throws IOException, JAXBException {
System.out.println("enter load Agent");
AgentResponseModel agentResponseModel = this
.extractAgentResponseModel(this
.getHttpRequester()
.sendPost(this.getBaseUrl() + "/queryAgentList", null,
this.makeAccessTockenMap(accessToken))
.getContent());
if (agentResponseModel.isSuccess()) {
System.out.println("success");
return listToJsonString(agentResponseModel.getAgents());
} else {
System.out.println("failer");
return "failure";
}
}
private String listToJsonString(List<Agent> agentList) {
String result = "";
if (agentList == null) {
return result;
}
for (Agent agent : agentList) {
if (result.isEmpty()) {
result = result + "[" + objectToJsonString(agent);
} else {
result = result + "," + objectToJsonString(agent);
}
}
return result + "]";
}
private String objectToJsonString(Agent agent) {
return "{" + "hostName:\"" + agent.getHostName() + "\",port:\""
+ agent.getPort() + "\",ID:\"" + agent.getId()
+ "\",currentStatus:\"" + agent.getCurrentStatus()
+ "\",maxLoad:\"" + agent.getMaxLoad() + "\",remainLoad:\""
+ agent.getRemainLoad() + "\"}";
}
private AgentResponseModel extractAgentResponseModel(String content)
throws JAXBException {
AgentResponseModel resultModel = new AgentResponseModel();
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (AgentResponseModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
private String _marshalAgentToString(Agent agent) throws JAXBException {
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = JAXBContext.newInstance(agent.getClass())
.createMarshaller();
marshaller.marshal(agent, stringWriter);
return stringWriter.toString();
}
}

View File

@ -1,22 +1,38 @@
package org.bench4q.web.controller;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public abstract class BaseActionController {
private HttpRequest httpRequest;
protected final String masterIP = "133.133.12.1:8080/";
protected final String AUTH_HEADER_PROPERTY = "Authorization";
protected final String ACCES_TOCKEN_STARTER = "Bearer ";
public HttpRequest getHttpRequest() {
return httpRequest;
}
public void setHttpRequest(HttpRequest httpRequest) {
this.httpRequest = httpRequest;
}
}
package org.bench4q.web.controller;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.master.communication.HttpRequester;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public abstract class BaseActionController {
private HttpRequester httpRequester;
protected final String masterIP = "133.133.12.1:8080/";
protected final String AUTH_HEADER_PROPERTY = "Authorization";
protected final String ACCES_TOCKEN_STARTER = "Bearer ";
public HttpRequester getHttpRequester() {
return httpRequester;
}
@Autowired
public void setHttpRequester(HttpRequester httpRequester) {
this.httpRequester = httpRequester;
}
public String getMasterIP() {
return masterIP;
}
public Map<String, String> makeAccessTockenMap(String accessToken) {
Map<String, String> properties = new HashMap<String, String>();
properties
.put(AUTH_HEADER_PROPERTY, ACCES_TOCKEN_STARTER + accessToken);
return properties;
}
}

View File

@ -163,8 +163,8 @@ public class ScriptActionController extends BaseActionController {
OperateScriptServerResponseModel operateScriptServerResponseModel = extractoperateScriptServerResponseModel(httpResponse
.getContent());
List<Script> scripts = operateScriptServerResponseModel.getScripts();
System.out.println(objecttoString(scripts));
return objecttoString(scripts);
System.out.println(listToString(scripts));
return listToString(scripts);
}
@RequestMapping("editscript.do")
@ -240,7 +240,7 @@ public class ScriptActionController extends BaseActionController {
* "createDate"); }
*/
public String objecttoString(List<Script> scripts) {
public String listToString(List<Script> scripts) {
if (scripts == null) {
return "";
}

View File

@ -58,8 +58,8 @@
<!-- user dropdown starts -->
<div class="btn-group pull-right">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-user">${userName}</i><span class="hidden-phone"> </span> <span
class="caret"></span>
<i class="icon-user"></i>
<span class="hidden-phone">${userName}</span> <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><a href="#">Profile</a></li>
@ -137,7 +137,7 @@
</div>
</div>
<div class="box-content">
<table id="scripttab"
<table id="agentsTab"
class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
@ -146,6 +146,7 @@
<th>Port</th>
<th>Status</th>
<th>MaxLoad</th>
<th>Actions</th>
</tr>
</thead>
<tbody>

View File

@ -0,0 +1,21 @@
$(document).ready(function() {
loadAgents();
});
function loadAgents() {
$('#scripttab').dataTable().fnClearTable();
$.post("loadscript.do", {}, function(data) {
showAgents(data);
});
}
function showAgents(data) {
var jsonObject = eval(data);
for ( var i = 0; i < jsonObject.length; i++) {
addtablerow(jsonObject[i].name, jsonObject[i].ID, time);
$('#agentsTab >tbody').children("tr").eq(i)
.attr("id", jsonObject[i].ID);
$('#agentsTab >tbody').children("tr").eq(i).attr("name",
jsonObject[i].name);
}
}

View File

@ -1,4 +1,3 @@
function register(){
$.post(reg.do);
function register(){
$.post("reg.do");
}