Skip to content

Commit

Permalink
v2.3 PC연장신청, MSG 충돌 해결
Browse files Browse the repository at this point in the history
#69 해결
- TEST 내용 Issue에 있어서 비교해봐도 좋음.
  • Loading branch information
jongwuner committed Mar 6, 2020
1 parent 618fca3 commit d702764
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion dev/Server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'com.hours22'
version = '2.1-SNAPSHOT'
version = '2.3-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,20 @@ public void GetIndex(HttpServletRequest req, HttpServletResponse response) throw
"O pc 언제꺼 ??? db에 반영 됌.<br>" +
"- observer pattern 적용 필요\r\n" +
"- 비동기 처리 필요");

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : / <- GET method [Client Ip : "+ cic.getClientIp(req) +" ] at " + transFormat.format(new Date()));
}

@RequestMapping(value = "/testpage", method = RequestMethod.GET)
public @ResponseBody String GetTestPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
dss.test();

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : /testpage <- GET method [Client Ip : " +cic.getClientIp(request)+" ] at " + transFormat.format(new Date()));
return "Test Page";
}

@RequestMapping(value = "/pc/{id}", method = RequestMethod.GET)
public @ResponseBody String GetPcInstanceData(HttpServletRequest req, HttpServletResponse response, @PathVariable String id) throws IOException {

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : /pc/"+ id +" <- GET method [Client Ip : "+ cic.getClientIp(req) + " ] at " + transFormat.format(new Date()));

Expand All @@ -91,7 +88,6 @@ public void GetIndex(HttpServletRequest req, HttpServletResponse response) throw
@RequestMapping(value = "/pc/{id}", method = RequestMethod.POST)
public void PostPcInstanceData(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String, String> map, @PathVariable String id) throws IOException {
String json = ojm.writeValueAsString(map);

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : "+ json +" <- POST method [Client : "+ cic.getClientIp(request) + " ] at "+transFormat.format(new Date()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ public class MobileController{

@Autowired
ClientInfoController cic;

SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

@RequestMapping(value = "/mobile/pc", method = RequestMethod.GET)
public void GetPcData(HttpServletRequest request, HttpServletResponse response) throws IOException {

response.setContentType("application/json;charset=UTF-8");
// RedisLoad_JsonToObj();
// HttpResponse_ObjToJson();
System.out.println("--------------------------------------------------------------------------------------------");

System.out.println("Input : /mobile/pc <- GET method [Client Ip : "+ cic.getClientIp(request)+"] at "+transFormat.format(new Date()) );

lc.getConnection();
Expand Down Expand Up @@ -132,13 +129,11 @@ public String call() throws Exception {

@RequestMapping(value = "/mobile/pc/{id}/data", method = RequestMethod.GET)
public void GetPcRamCpuData(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws IOException {

response.setContentType("application/json;charset=UTF-8");
// RedisLoad_JsonToObj();
// HttpResponse_ObjToJson();

lc.getConnection();

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : /mobile/pc/"+id+"/data <- GET method [Client Ip : "+ cic.getClientIp(request) +" ] at " + transFormat.format(new Date()));

Expand All @@ -154,15 +149,13 @@ public void GetPcRamCpuData(HttpServletRequest request, HttpServletResponse resp


@RequestMapping(value = "/mobile/pc/{id}/power/{endTime}", method = RequestMethod.POST)

public @ResponseBody Callable<Map<String, String>> PostPcPowerOff(HttpServletRequest request, HttpServletResponse response, @PathVariable String id, @PathVariable String endTime) throws IOException, InterruptedException, ParseException {

response.setContentType("application/json;charset=UTF-8");

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : /pc/"+id+"/power/"+endTime+" <- POST method(언제꺼? v2.0) [Client Ip : " +cic.getClientIp(request)+" ] at "+transFormat.format(new Date()));
System.out.println("현재 Thread ID : " + Thread.currentThread().getId());


lc.getConnection();
String jsonStringForAndroid = lc.getConnectionHgetall(id);
Expand All @@ -180,31 +173,30 @@ public void GetPcRamCpuData(HttpServletRequest request, HttpServletResponse resp
String res = thread.getName();
if(res.equals("PCPowerOffExc-Return-"+id)) {
thread.interrupt();
System.out.println("******"+res+" 스레드를 종료시킵니다.******");
System.out.println("******"+res+" 스레드를 종료시킵니다. [PowerReturn]******");
}
if(res.equals("PCPowerOffMsg-Return-"+id)) {
thread.interrupt();
System.out.println("******"+res+" 스레드를 종료시킵니다. [MessageReturn]******");
}
}


Date now = new Date();
String form = endTime;
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

System.out.println("Mobile에서 PC종료시간을 새로 설정하였습니다. (변경된 methd)" + transFormat.format(now));
String [] seq = form.split("-");
form = seq[0] + "-" + seq[1] + "-" + seq[2] +" "+seq[3] +":"+seq[4];
System.out.println("종료예약 설정 [시간 : "+form+"]");

Map<String, String> jsonObject = new HashMap<String, String>();
jsonObject.put("id", id);

jsonObject.put("powerStatus", "OFF");

jsonObject.put("endTime", endTime);
String jsonString = ojm.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);

//lc.getConnectionHsetAllData(id, jsonObject);
lc.getConnectionHset(id, jsonObject);


// response
System.out.println("hget 디버깅 결과 : " + jsonString);
Expand Down Expand Up @@ -264,9 +256,9 @@ public Map<String, String> call() throws Exception {
lc.getConnectionHset(id, jsonObjectExit);
lc.getConnectionExit();

return jsonObjectExit;
}
}
};
return jsonObjectExit;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hours22.system_monitor_ver11.client.ClientInfoController;
import com.hours22.system_monitor_ver11.client.ServerTimer;

import com.hours22.system_monitor_ver11.db.DataService;
import com.hours22.system_monitor_ver11.db.LettuceController;

Expand Down Expand Up @@ -63,13 +62,11 @@ public void PostPcData(HttpServletRequest request, HttpServletResponse response,

System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("Input : /pc/"+id+"/data <- POST method [Client Ip : " +cic.getClientIp(request)+"] at "+transFormat.format(new Date()) );

lc.getConnection();
lc.getConnectionHset(id, map);
lc.getConnectionExit();
System.out.println(map.toString());


Set<Thread> setOfThread = Thread.getAllStackTraces().keySet();
for(Thread thread : setOfThread){
System.out.println("Active Thread's [ Number : " +thread.getId()+" / Name : "+thread.getName()+" ] ");
Expand All @@ -82,12 +79,11 @@ public void PostPcData(HttpServletRequest request, HttpServletResponse response,
}
}


response.getWriter().print("Success /pc/"+id+"/data <- POST method !!");
}


@RequestMapping(value = "/pc/{id}/power/{endTime}", method = RequestMethod.POST)

public @ResponseBody Callable<Map<String, String>> PostPcPowerOff(HttpServletRequest request, HttpServletResponse response, @PathVariable String id, @PathVariable String endTime) throws IOException, InterruptedException, ParseException {

response.setContentType("application/json;charset=UTF-8");
Expand All @@ -106,8 +102,12 @@ public void PostPcData(HttpServletRequest request, HttpServletResponse response,
String res = thread.getName();
if(res.equals("PCPowerOffExc-Return-"+id)) {
thread.interrupt();
System.out.println("******"+res+" 스레드를 종료시킵니다.******");
System.out.println("******"+res+" 스레드를 종료시킵니다. [PowerReturn]******");
opt = 2;
};
if(res.equals("PCPowerOffMsg-Return-"+id)) {
thread.interrupt();
System.out.println("******"+res+" 스레드를 종료시킵니다. [MessageReturn]******");
}
}

Expand All @@ -117,7 +117,6 @@ public void PostPcData(HttpServletRequest request, HttpServletResponse response,
ct.setName("PCPowerOffExc-"+id);
System.out.println("현재 Thread [ ID : " + ct.getId()+ " / Name : "+ct.getName()+" ] ");



lc.getConnection();
String jsonStringForAndroid = lc.getConnectionHgetall(id);
Expand All @@ -127,18 +126,30 @@ public void PostPcData(HttpServletRequest request, HttpServletResponse response,


Date now = new Date();
String form = endTime;
SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

String eform = endTime;
String startTime = transFormat.format(now);

char[] sfromArr = null;
sfromArr = startTime.toCharArray();
for(int i=4; i<=13; i+=3) {
sfromArr[i] = '-';
}
startTime = String.valueOf(sfromArr);

//2020-05-15 11:34

if(opt == 1)
System.out.println(id+" PC가 켜졌습니다! (변경된 methd)" + transFormat.format(now));
else if(opt == 2)
System.out.println(id+" PC의 종료시간이 변경되었습니다." + transFormat.format(now));

String [] seq = form.split("-");
form = seq[0] + "-" + seq[1] + "-" + seq[2] +" "+seq[3] +":"+seq[4];
System.out.println("종료예약 설정 [시간 : "+form+"]");
String [] eseq = eform.split("-");
eform = eseq[0] + "-" + eseq[1] + "-" + eseq[2] +" "+eseq[3] +":"+eseq[4];
System.out.println("종료예약 설정 [시간 : "+eform+"]");

Map<String, String> jsonObject = new HashMap<String, String>();
if(opt == 1)
jsonObject.put("startTime", startTime);
jsonObject.put("id", id);
jsonObject.put("powerStatus", "OFF");
jsonObject.put("endTime", endTime);
Expand All @@ -150,7 +161,7 @@ else if(opt == 2)
// response
System.out.println("hget 디버깅 결과 : " + jsonString);

Date to = transFormat.parse(form);
Date to = transFormat.parse(eform);
lc.getConnectionExit();

return new Callable<Map<String, String>>() {
Expand Down Expand Up @@ -181,7 +192,7 @@ public Map<String, String> call() throws Exception {
//Thread.currentThread().interrupt();
Thread.sleep(2000);
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName() +" Thread가 중지되었습니다.");
System.out.println(Thread.currentThread().getName() +" Thread가 중지되었습니다.[Power]");
interrupted = true;
break;
}
Expand Down Expand Up @@ -210,6 +221,7 @@ public Map<String, String> call() throws Exception {
}
};
}

@RequestMapping(value = "/pc/{id}/message/{min}", method = RequestMethod.GET)
public @ResponseBody Callable<Map<String, String>> GetWarningMsg(HttpServletRequest request, HttpServletResponse response, @PathVariable String id, @PathVariable String min) throws IOException, InterruptedException, ParseException {
response.setContentType("application/json;charset=UTF-8");
Expand Down Expand Up @@ -238,7 +250,6 @@ public Map<String, String> call() throws Exception {




String EndTime;
lc.getConnection();
EndTime = lc.getConnectionHgetField(id, "endTime");
Expand Down Expand Up @@ -285,16 +296,16 @@ public Map<String, String> call() throws Exception {
long start, end, slept;
boolean interrupted = false;

Date nnow = new Date();
Date now = new Date();

while(to.getTime() - nnow.getTime() > 0) {
while(goal.getTime() - now.getTime() > 0) {
try{
// do stuff
nnow = new Date();
now = new Date();
//Thread.currentThread().interrupt();
Thread.sleep(2000);
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName() +" Thread가 중지되었습니다.");
System.out.println(Thread.currentThread().getName() +" Thread가 중지되었습니다. [Msg]");
interrupted = true;
break;
}
Expand All @@ -321,6 +332,5 @@ public Map<String, String> call() throws Exception {
}
}
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -72,11 +74,11 @@ public void test() {

public Map<String, String> GetAllPcDataRedis() throws JsonProcessingException {

//최종 완성될 JSONObject 선언(전체)
//���� �ϼ��� JSONObject ����(��ü)
JSONObject jsonObject = new JSONObject();
//person의 JSON정보를 담을 Array 선언
//person�� JSON������ ���� Array ����
JSONArray jsonArray = new JSONArray();
//person의 한명 정보가 들어갈 JSONObject 선언
//person�� �Ѹ� ������ �� JSONObject ����
JSONObject jsonInfo = new JSONObject();

int i = 0;
Expand All @@ -93,10 +95,22 @@ public Map<String, String> GetAllPcDataRedis() throws JsonProcessingException {
String tkey = new String(cursor.next());
//String kvalue = ojm.writeValueAsString((PcData)valueOperations.get(tkey));
String kvalue = mc.getConnectionHgetall(tkey);
System.out.println("key data [" + ++i +"] = " + tkey +" "+ kvalue); // 조회된 Key의 이름을 출력
System.out.println("key data [" + ++i +"번째] = " + tkey +" "+ kvalue); // ��ȸ�� Key�� �̸��� ���
System.out.println();
//arrList.put(Integer.toString(i), kvalue);
jsonArray.add(kvalue);

// String -> JsonParser
JSONParser parser = new JSONParser();
Object obj = null;
try {
obj = parser.parse(kvalue);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject jsonTmpObj = (JSONObject) obj;

jsonArray.add(jsonTmpObj);
}
} finally {
jsonObject.put("pcs", jsonArray);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ public void getConnectionHset(String key, Map<String, String> map) {
// field : cpuData
if(map.get("cpuData") != null) {
syncCmd.hset(key, "cpuData", map.get("cpuData"));

System.out.println("hset "+key+ " cpuData -> true");
} else {
System.out.println("hset "+key+ " cpuData -> false");
Expand All @@ -210,13 +209,11 @@ public void getConnectionHset(String key, Map<String, String> map) {
System.out.println("hset "+key+ " startTime -> true");
} else {
System.out.println("hset "+key+ " startTime -> false");

}

// field : endTime
if(map.get("endTime") != null) {
syncCmd.hset(key, "endTime", map.get("endTime"));

System.out.println("hset "+key+ " endTime -> true");
} else {
System.out.println("hset "+key+ " endTime -> false");
Expand Down

0 comments on commit d702764

Please sign in to comment.