refactoring
This commit is contained in:
parent
f5d842f6a0
commit
efb26042f0
|
@ -1,8 +1,8 @@
|
|||
package org.bench4q.master.api;
|
||||
|
||||
import java.util.Random;
|
||||
import org.bench4q.master.api.model.OperateScriptServerResponseModel;
|
||||
import org.bench4q.master.entity.Constant;
|
||||
import org.bench4q.master.entity.LocalPort;
|
||||
import org.bench4q.master.entity.ScriptCapturer;
|
||||
import org.bench4q.master.service.PortPoolService;
|
||||
import org.bench4q.master.service.ScriptService;
|
||||
|
@ -60,22 +60,15 @@ public class RecordScriptController extends BaseController {
|
|||
@RequestMapping(value = "/startScriptRecordServer", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public OperateScriptServerResponseModel startScriptRecordServer() {
|
||||
if (!this.checkScope(Constant.NORAML_AUTHENTICATION)) {
|
||||
return returnResponseModel(false, "has no power for this!!!");
|
||||
}
|
||||
/*
|
||||
* if (!this.checkScope(Constant.NORAML_AUTHENTICATION)) { return
|
||||
* returnResponseModel(false, "has no power for this!!!"); }
|
||||
*/
|
||||
synchronized (PORT_LOCK) {
|
||||
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(this.getPortPoolService().getScriptPortPool()
|
||||
.get(ran).getPort());
|
||||
this.getPortPoolService().getScriptPortPool().remove(ran);
|
||||
LocalPort localPort = this.getPortPoolService().getAPortNotInUse();
|
||||
this.setPortForRecord(localPort.getPort());
|
||||
localPort.setInUse(true);
|
||||
}
|
||||
|
||||
// String pathString = this.getClass().getResource("/").getPath();
|
||||
|
@ -97,9 +90,10 @@ public class RecordScriptController extends BaseController {
|
|||
@RequestMapping(value = "/stopScriptRecordServer", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public OperateScriptServerResponseModel stopScriptRecordServer() {
|
||||
if (!checkScope(Constant.NORAML_AUTHENTICATION)) {
|
||||
return returnResponseModel(false, "has no power for this!!!");
|
||||
}
|
||||
/*
|
||||
* if (!checkScope(Constant.NORAML_AUTHENTICATION)) { return
|
||||
* returnResponseModel(false, "has no power for this!!!"); }
|
||||
*/
|
||||
if (this.getScriptCapturer().equals(null)) {
|
||||
return returnResponseModel(false,
|
||||
"there is no RecordingServer to stop");
|
||||
|
|
|
@ -5,5 +5,4 @@ public class Constant {
|
|||
public static byte SUPER_AUTHENTICATION = 1;
|
||||
public static String TESTIPADRESS = "127.0.0.1";
|
||||
public static String TESTSCRIPTFOLDER = "C:\\Script\\";
|
||||
// spublic static Vector<Integer> ScriptPortPool = new Vector<Integer>();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package org.bench4q.master.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
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;
|
||||
|
@ -99,24 +99,17 @@ public class PortPoolService {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean addPortToLocalPool(int port) {
|
||||
synchronized (RecordPortController.getSyncObject()) {
|
||||
if (this.getScriptPortPool().get(port) != null) {
|
||||
return false;
|
||||
public LocalPort getAPortNotInUse() {
|
||||
Iterator<LocalPort> iterator = ScriptPortPool.values().iterator();
|
||||
LocalPort localPort = new LocalPort();
|
||||
while (iterator.hasNext()) {
|
||||
localPort = iterator.next();
|
||||
if (localPort.isInUse()) {
|
||||
continue;
|
||||
} else {
|
||||
return localPort;
|
||||
}
|
||||
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;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package org.bench4q.master.test;
|
||||
|
||||
import org.bench4q.master.api.RecordPortController;
|
||||
import org.bench4q.master.api.RecordScriptController;
|
||||
import org.bench4q.master.api.model.OperateScriptServerResponseModel;
|
||||
|
||||
public class RecordScriptControllerTest {
|
||||
private RecordScriptController recordScriptController = new RecordScriptController();
|
||||
private RecordPortController recordPortController = new RecordPortController();
|
||||
|
||||
public RecordScriptController getRecordScriptController() {
|
||||
return recordScriptController;
|
||||
}
|
||||
|
||||
public void setReCordScriptController(RecordScriptController reController) {
|
||||
this.recordScriptController = reController;
|
||||
}
|
||||
|
||||
public RecordPortController getRecordPortController() {
|
||||
return recordPortController;
|
||||
}
|
||||
|
||||
public void setRecordPortController(
|
||||
RecordPortController recordPortController) {
|
||||
this.recordPortController = recordPortController;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
RecordScriptControllerTest recordScriptControllerTest = new RecordScriptControllerTest();
|
||||
recordScriptControllerTest.getRecordPortController().addPortToPortPool(
|
||||
6565);
|
||||
|
||||
OperateScriptServerResponseModel operateScriptServerResponseModel = recordScriptControllerTest
|
||||
.getRecordScriptController().startScriptRecordServer();
|
||||
System.out.println(operateScriptServerResponseModel.isSuccess());
|
||||
|
||||
operateScriptServerResponseModel = recordScriptControllerTest
|
||||
.getRecordScriptController().stopScriptRecordServer();
|
||||
System.out.println(operateScriptServerResponseModel.isSuccess());
|
||||
|
||||
recordScriptControllerTest.getRecordPortController().addPortToPortPool(
|
||||
6565);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue