add control on port

This commit is contained in:
coderfengyun 2013-07-18 17:36:17 +08:00
parent 765f0c13bb
commit f5d842f6a0
8 changed files with 224 additions and 45 deletions

View File

@ -1,7 +1,7 @@
package org.bench4q.master.api;
import org.bench4q.master.api.model.OrganizeRecordPortResponseModel;
import org.bench4q.master.entity.Constant;
import org.bench4q.master.entity.LocalPort;
import org.bench4q.master.service.PortPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class RecordPortController extends BaseController {
private PortPoolService portPoolService = new PortPoolService();
private static Object syncObject = new Object();
public PortPoolService getPortPoolService() {
return portPoolService;
@ -25,11 +26,18 @@ public class RecordPortController extends BaseController {
this.portPoolService = portPoolService;
}
public static Object getSyncObject() {
return syncObject;
}
public static void setSyncObject(Object syncObject) {
RecordPortController.syncObject = syncObject;
}
@RequestMapping(value = "/AddPortToPortPool", method = RequestMethod.GET)
@ResponseBody
public OrganizeRecordPortResponseModel addPortToPortPool(
@RequestParam int port, @RequestParam String accessToken) {
OrganizeRecordPortResponseModel organizeRecordPortResponseModel = new OrganizeRecordPortResponseModel();
@RequestParam int port) {
/*
* if(!this.checkScope(Constant.SUPER_AUTHENTICATION)) {
* organizeRecordPortResponseModel.setSuccess(false);
@ -37,19 +45,38 @@ public class RecordPortController extends BaseController {
* .setFailCauseString("you don't hava the power to add port to pool!");
* return organizeRecordPortResponseModel; }
*/
if (Constant.ScriptPortPool.contains(port)) {
organizeRecordPortResponseModel.setSuccess(false);
organizeRecordPortResponseModel
.setFailCauseString("The pot you add already exists in the port popl!");
return organizeRecordPortResponseModel;
LocalPort localPort = new LocalPort();
localPort.setPort(port);
localPort.setInUse(false);
if (this.getPortPoolService().getScriptPortPool().put(port, localPort) == null) {
return setOrganizePortResponseModel(false,
"add to local pool fails 1");
}
if (!this.getPortPoolService().addPortToDBPool(port)) {
return setOrganizePortResponseModel(false, "add to DB pool fails 2");
}
return setOrganizePortResponseModel(true, "");
}
Constant.ScriptPortPool.add(port);
@RequestMapping(value = "/removePortFromPool", method = RequestMethod.POST)
public OrganizeRecordPortResponseModel removePortFromPool(
@RequestParam int port) {
if (this.getPortPoolService().getScriptPortPool().remove(port) == null) {
return setOrganizePortResponseModel(false,
"fail in add to local pool 1");
}
if (!this.getPortPoolService().removePortFromDBPool(port)) {
return setOrganizePortResponseModel(false,
"remove from local fails");
}
return setOrganizePortResponseModel(true, "");
}
organizeRecordPortResponseModel.setSuccess(true);
organizeRecordPortResponseModel.setFailCauseString("add port " + port
+ " to ScriptPortPool sucessfully!");
private OrganizeRecordPortResponseModel setOrganizePortResponseModel(
boolean success, String failString) {
OrganizeRecordPortResponseModel organizeRecordPortResponseModel = new OrganizeRecordPortResponseModel();
organizeRecordPortResponseModel.setSuccess(success);
organizeRecordPortResponseModel.setFailCauseString(failString);
return organizeRecordPortResponseModel;
}
}

View File

@ -4,6 +4,7 @@ import java.util.Random;
import org.bench4q.master.api.model.OperateScriptServerResponseModel;
import org.bench4q.master.entity.Constant;
import org.bench4q.master.entity.ScriptCapturer;
import org.bench4q.master.service.PortPoolService;
import org.bench4q.master.service.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -19,6 +20,7 @@ public class RecordScriptController extends BaseController {
private ScriptCapturer scriptCapturer;
private ScriptService scriptService;
private static final Object PORT_LOCK = new Object();
private PortPoolService portPoolService = new PortPoolService();
public int getPortForRecord() {
return portForRecord;
@ -46,6 +48,15 @@ public class RecordScriptController extends BaseController {
this.scriptService = scriptService;
}
public PortPoolService getPortPoolService() {
return portPoolService;
}
@Autowired
public void setPortPoolService(PortPoolService portPoolService) {
this.portPoolService = portPoolService;
}
@RequestMapping(value = "/startScriptRecordServer", method = RequestMethod.GET)
@ResponseBody
public OperateScriptServerResponseModel startScriptRecordServer() {
@ -53,15 +64,18 @@ public class RecordScriptController extends BaseController {
return returnResponseModel(false, "has no power for this!!!");
}
synchronized (PORT_LOCK) {
int portSumNum = Constant.ScriptPortPool.size();
int portSumNum = this.getPortPoolService().getScriptPortPool()
.size();
if (portSumNum == 0) {
return returnResponseModel(false,
"Script Record port not enough, please wait for a while!");
}
// TODO:
Random rand = new Random();
int ran = rand.nextInt(portSumNum);
setPortForRecord(Constant.ScriptPortPool.get(ran));
Constant.ScriptPortPool.remove(ran);
setPortForRecord(this.getPortPoolService().getScriptPortPool()
.get(ran).getPort());
this.getPortPoolService().getScriptPortPool().remove(ran);
}
// String pathString = this.getClass().getResource("/").getPath();
@ -92,7 +106,8 @@ public class RecordScriptController extends BaseController {
}
scriptCapturer.stopCurrentRecord();
Constant.ScriptPortPool.add(this.getPortForRecord());
this.getPortPoolService().getScriptPortPool()
.get(this.getPortForRecord()).setInUse(false);
return returnResponseModel(true, "RecordServer stop");
}

View File

@ -1,11 +1,9 @@
package org.bench4q.master.entity;
import java.util.Vector;
public class Constant {
public static byte NORAML_AUTHENTICATION = 0;
public static byte SUPER_AUTHENTICATION = 1;
public static String TESTIPADRESS = "127.0.0.1";
public static String TESTSCRIPTFOLDER = "C:\\Script\\";
public static Vector<Integer> ScriptPortPool = new Vector<Integer>();
// spublic static Vector<Integer> ScriptPortPool = new Vector<Integer>();
}

View File

@ -0,0 +1,15 @@
package org.bench4q.master.entity;
import org.bench4q.master.entity.db.Port;
public class LocalPort extends Port {
private boolean inUse;
public boolean isInUse() {
return inUse;
}
public void setInUse(boolean inUse) {
this.inUse = inUse;
}
}

View File

@ -2,6 +2,9 @@ package org.bench4q.master.entity.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@ -10,6 +13,8 @@ public class Port {
private int id;
private int port;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
public int getId() {
return id;

View File

@ -1,36 +1,122 @@
package org.bench4q.master.service;
import java.util.Vector;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.master.api.RecordPortController;
import org.bench4q.master.entity.LocalPort;
import org.bench4q.master.entity.db.Port;
import org.bench4q.master.helper.SessionHelper;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PortPoolService {
private static Vector<Integer> ScriptPortPool = new Vector<Integer>();
private static Object syncObject = new Object();
private static Map<Integer, LocalPort> ScriptPortPool = new HashMap<Integer, LocalPort>();
public static Object getSyncObject() {
return syncObject;
}
private SessionHelper sessionHelper = new SessionHelper();
public Vector<Integer> getScriptPortPool() {
public Map<Integer, LocalPort> getScriptPortPool() {
return PortPoolService.ScriptPortPool;
}
public boolean addPortToPool(int port) {
synchronized (PortPoolService.syncObject) {
if (this.getScriptPortPool().contains(port)) {
return false;
}
this.getScriptPortPool().add(port);
}
return true;
public SessionHelper getSessionHelper() {
return sessionHelper;
}
public void removePortFromPool(int port) {
if (this.getScriptPortPool().contains(port)) {
@Autowired
public void setSessionHelper(SessionHelper sessionHelper) {
this.sessionHelper = sessionHelper;
}
public boolean addPortToDBPool(int port) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Port portDB = (Port) session.createCriteria(Port.class)
.add(Restrictions.eq("port", port)).uniqueResult();
if (portDB != null) {
return false;
}
portDB = new Port();
portDB.setPort(port);
session.merge(portDB);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean removePortFromDBPool(int port) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
Port portDB = (Port) session.createCriteria(Port.class)
.add(Restrictions.eq("port", port)).uniqueResult();
if (portDB == null) {
return false;
}
session.delete(portDB);
transaction.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean existInDBPool(int port) {
Session session = this.getSessionHelper().openSession();
try {
Port portDB = (Port) session.createCriteria(Port.class)
.add(Restrictions.eq("port", port)).uniqueResult();
if (portDB == null) {
return false;
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (session != null) {
session.close();
}
}
}
public boolean addPortToLocalPool(int port) {
synchronized (RecordPortController.getSyncObject()) {
if (this.getScriptPortPool().get(port) != null) {
return false;
}
LocalPort localPort = new LocalPort();
localPort.setPort(port);
localPort.setInUse(false);
return this.getScriptPortPool().put(port, localPort) != null;
}
}
public boolean removePortFromLocalPool(int port) {
synchronized (RecordPortController.getSyncObject()) {
if (this.getScriptPortPool().get(port) == null) {
return false;
}
return this.getScriptPortPool().remove(port) != null;
}
}
}

View File

@ -16,12 +16,13 @@
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="org.bench4q.master.entity.db.User" />
<mapping class="org.bench4q.master.entity.db.Script"/>
<mapping class="org.bench4q.master.entity.db.Behavior"/>
<mapping class="org.bench4q.master.entity.db.LoadProfile"/>
<mapping class="org.bench4q.master.entity.db.Plugin"/>
<mapping class="org.bench4q.master.entity.db.Sample"/>
<mapping class="org.bench4q.master.entity.db.Timer"/>
<mapping class="org.bench4q.master.entity.db.Agent"/>
<mapping class="org.bench4q.master.entity.db.Script" />
<mapping class="org.bench4q.master.entity.db.Behavior" />
<mapping class="org.bench4q.master.entity.db.LoadProfile" />
<mapping class="org.bench4q.master.entity.db.Plugin" />
<mapping class="org.bench4q.master.entity.db.Sample" />
<mapping class="org.bench4q.master.entity.db.Timer" />
<mapping class="org.bench4q.master.entity.db.Agent" />
<mapping class="org.bench4q.master.entity.db.Port" />
</session-factory>
</hibernate-configuration>

View File

@ -0,0 +1,32 @@
package org.bench4q.master.test;
import org.bench4q.master.api.RecordPortController;
import org.bench4q.master.api.model.OrganizeRecordPortResponseModel;
public class RecordPortControllerTest {
private RecordPortController recordPortController;
public RecordPortControllerTest() {
this.setRecordPortController(new RecordPortController());
}
public RecordPortController getRecordPortController() {
return recordPortController;
}
private void setRecordPortController(
RecordPortController recordPortController) {
this.recordPortController = recordPortController;
}
public static void main(String[] args) {
int port = 8985;
RecordPortController recordPortController = new RecordPortController();
OrganizeRecordPortResponseModel organizeRecordPortResponseModel = recordPortController
.addPortToPortPool(port);
System.out.println(organizeRecordPortResponseModel.isSuccess());
System.out
.println(organizeRecordPortResponseModel.getFailCauseString());
recordPortController.removePortFromPool(port);
}
}