Skip to content

Commit

Permalink
Merge pull request #38 from 22hours/Http(Client)
Browse files Browse the repository at this point in the history
Http(client)
  • Loading branch information
damin8 authored Jan 26, 2020
2 parents 800cb0d + 12f96b5 commit fa8917f
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 128 deletions.
81 changes: 50 additions & 31 deletions dev/Client(PC)/system-monitor/PCClient/Http/PCGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
Expand All @@ -12,55 +14,72 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import PCClient.Module.*;
import PCClient.Module.Shutdown;
import PCModel.PC;

public class PCGet {
private static PCGet instance = null;
private static final String id = "1";

private PCGet() {

}

public static PCGet Instance() {
if (instance == null) {
private static PCGet instance;
public static PCGet getInstance() {
if(instance == null) {
instance = new PCGet();
}
return instance;
}

public void GetMethod()
public void GetMethod(PC pc)
throws URISyntaxException, ClientProtocolException, IOException{
URI uri = new URI("http://172.20.10.10:12345/system_monitor/pc/"+id);
System.out.println(uri);

URI uri = new URI("http://203.229.204.25:80/pc/5");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(new HttpGet(uri));
/*if (response.getStatusLine().getStatusCode() == 200) {
ResponseHandler<String> handler = new BasicResponseHandler();
String body = handler.handleResponse(response);
System.out.println(body);
} else {
System.out.println("response is error : " + response.getStatusLine().getStatusCode());
}*/
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
try {
JsonParser jsonParser = new JsonParser();
JsonArray jsonArray = (JsonArray)jsonParser.parse(content);
for(int i=0;i<jsonArray.size();i++) {
JsonObject object = (JsonObject)jsonArray.get(i);
String id = object.get("id").getAsString();
String power_status = object.get("power_status").getAsString();
String end_time = object.get("end_time").getAsString();
if(power_status.equals("false")) {
//Shutdown.shutdown();
}
//endtime °ü¸® ÇØÁà¾ß ÇÑ´Ù.

JsonElement jsonElement = JsonParser.parseString(content);
JsonObject jsonObject = jsonElement.getAsJsonObject();
String id = jsonObject.get("id").getAsString();
String power_status;
String end_time;
if(jsonObject.get("power_status").isJsonNull()) {
power_status = "null";
}
else{
power_status = jsonObject.get("power_status").getAsString();
}
if(jsonObject.get("end_time").isJsonNull()) {
end_time = "null";
}
else {
end_time = jsonObject.get("end_time").getAsString();
}
System.out.println("=====GET======");
System.out.println("id = "+id);
System.out.println("power_status = " + power_status);
System.out.println("end_time = "+end_time);
if(!id.equals("5")) {
System.out.println("잘못된 정보 수신!");
return;
}
if(!end_time.equals(pc.getEnd_time()) && !end_time.equals("null")) { //Android 사용자가 연장 신청하면 다를 수 있다.
TimeDifference timeDifference = new TimeDifference();
Shutdown.getInstance().stopshutdown();
// Android 사용자가 바꿨다면 end_time이 더 커야 되는게 정상.
String difference = timeDifference.calc(pc.getEnd_time(), end_time);
Shutdown.getInstance().shutdown(difference);
pc.setEnd_time(end_time);
}
if(power_status.equals("false") && !power_status.equals("null")) {
pc.setPower_status(power_status);
PCPost.getInstance().PCShutdown(pc);
Shutdown.getInstance().stopshutdown();
Shutdown.getInstance().shutdown("0");
}
}
catch(Exception e) {
Expand Down
175 changes: 106 additions & 69 deletions dev/Client(PC)/system-monitor/PCClient/Http/PCPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,106 +14,143 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import PCClient.Module.CPU;
import PCClient.Module.Memory;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import PCClient.Module.*;
import PCClient.Module.Shutdown;
import PCModel.PC;

public class PCPost {
private static PCPost instance = null;
private static final String id = "1";
private static PCPost instance;
private PCPost() {

}
public static PCPost Instance() {
if(instance==null) {
public static PCPost getInstance() {
if(instance == null) {
instance = new PCPost();
}
return instance;
}
public void PostMethod()
throws URISyntaxException, ClientProtocolException, IOException{
URI uri = new URI("http://172.20.10.10:12345/system_monitor/");
System.out.println(uri);

public void PostMethod(PC pc) throws URISyntaxException, ClientProtocolException, IOException {
URI uri = new URI("http://203.229.204.25:80/pc/5");
TimeDifference timeDifference = new TimeDifference();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",id));
params.add(new BasicNameValuePair("power_status","true"));
params.add(new BasicNameValuePair("start_time","18:00"));
params.add(new BasicNameValuePair("end_time","21:00"));
/*List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", pc.getId()));
params.add(new BasicNameValuePair("power_status", pc.getPower_status()));
params.add(new BasicNameValuePair("start_time", pc.getStart_time()));
params.add(new BasicNameValuePair("end_time", pc.getEnd_time()));
*/
/*
Gson gson = new Gson();
StringEntity postingString = new StringEntity(gson.toJson(pc));
postRequest.setEntity(postingString);
postRequest.setHeader("Content-type","application/json");
System.out.println(postingString);
*/
JsonObject json = new JsonObject();
json.addProperty("id", "5");
json.addProperty("cpu_data", pc.getCpu_data());
json.addProperty("ram_data", pc.getRam_data());
json.addProperty("power_status", pc.getPower_status());
json.addProperty("start_time", pc.getStart_time());
json.addProperty("end_time", pc.getEnd_time());
String difference = timeDifference.calc(pc.getStart_time(), pc.getEnd_time());
Shutdown.getInstance().stopshutdown();
Shutdown.getInstance().shutdown(difference); // óÀ½¿¡ ¿¹¾à ¼³Á¤
postRequest.setEntity(new StringEntity(json.toString(), "UTF8"));
postRequest.addHeader("Content-type", "application/json");
System.out.println("=====POST======");
System.out.println("id = 5");
System.out.println("power_status = " + pc.getPower_status());
System.out.println("end_time = "+pc.getEnd_time());
System.out.println("cpu_data = "+ pc.getCpu_data());
System.out.println("start_time = " + pc.getStart_time());
System.out.println("ram_data = "+pc.getRam_data());
try {
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,"UTF-8");
postRequest.setEntity(ent);
}
catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
System.out.println(content);
}
catch (ClientProtocolException e) {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
}
}
public void GeneralPollingPost()
throws URISyntaxException, ClientProtocolException, IOException{
URI uri = new URI("http://172.20.10.10:12345/"+id);
System.out.println(uri);


public void GeneralPollingPost(PC pc) throws URISyntaxException, ClientProtocolException, IOException {
URI uri = new URI("http://203.229.204.25:80/pc/5");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",id));
params.add(new BasicNameValuePair("cpu_data",CPU.getCPU().showCPU()));
params.add(new BasicNameValuePair("ram_data",Memory.getMemory().showMemory()));
try {
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,"UTF-8");
postRequest.setEntity(ent);
}
catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
pc.setCpu_data(CPU.getCPU().showCPU());
pc.setRam_data(Memory.getMemory().showMemory());
JsonObject json = new JsonObject();
json.addProperty("id", "5");
json.addProperty("cpu_data", pc.getCpu_data());
json.addProperty("ram_data", pc.getRam_data());
System.out.println("=====POST======");
System.out.println("id = 5");
System.out.println("cpu_data = " + pc.getCpu_data());
System.out.println("ram_data = "+pc.getRam_data());
postRequest.setEntity(new StringEntity(json.toString(), "UTF8"));
postRequest.addHeader("Content-type", "application/json");
try {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
System.out.println(content);
}
catch (ClientProtocolException e) {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
}
}
public void Extension()
throws URISyntaxException, ClientProtocolException, IOException{
URI uri = new URI("http://172.20.10.10:12345/"+id);
System.out.println(uri);


public void Extension(PC pc) throws URISyntaxException, ClientProtocolException, IOException {
URI uri = new URI("http://203.229.204.25:80/pc/" + pc.getId());

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",id));
params.add(new BasicNameValuePair("end_time","23:00"));
JsonObject json = new JsonObject();
json.addProperty("id", "5");
json.addProperty("end_time", pc.getEnd_time());
System.out.println("=====POST======");
System.out.println("id = 5");
System.out.println("end_time = " + pc.getEnd_time());
Shutdown.getInstance().stopshutdown();
TimeDifference timeDifference = new TimeDifference();
String difference = timeDifference.calc(pc.getStart_time(), pc.getEnd_time());
Shutdown.getInstance().shutdown(difference);
postRequest.setEntity(new StringEntity(json.toString(), "UTF8"));
postRequest.addHeader("Content-type", "application/json");
try {
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,"UTF-8");
postRequest.setEntity(ent);
}
catch(UnsupportedEncodingException e) {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
}
}

public void PCShutdown(PC pc) throws URISyntaxException, ClientProtocolException, IOException {
URI uri = new URI("http://203.229.204.25:80/pc/5");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(uri);
JsonObject json = new JsonObject();
json.addProperty("id", "5");
json.addProperty("power_status", pc.getPower_status());
System.out.println("=====POST======");
System.out.println("id = 5");
System.out.println("power_status = " + pc.getPower_status());
postRequest.setEntity(new StringEntity(json.toString(), "UTF8"));
postRequest.addHeader("Content-type", "application/json");
try {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
System.out.println(content);
}
catch (ClientProtocolException e) {
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
}
}
Expand Down
25 changes: 18 additions & 7 deletions dev/Client(PC)/system-monitor/PCClient/JavaSwing/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URISyntaxException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
Expand All @@ -13,18 +16,26 @@
import PCClient.Http.PCGet;
import PCClient.Http.PCPost;
import PCClient.Module.*;

import PCClient.Module.Shutdown;
import PCModel.PC;
public class Frame {

public static void main(String[] args) {
try{
PCGet.Instance().GetMethod();
//PCPost.Instance().PostMethod();
//Shutdown.shutdown();
}
catch(Exception e) {
GetMACAddress MAC = new GetMACAddress();
String id = MAC.getLocalMacAddress();
PC pc = new PC(id);
ShutdownHook s = new ShutdownHook(pc);
s.AttachShutdownHook();
try {
PCPost.getInstance().PostMethod(pc);
} catch (URISyntaxException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread generalPostThread = new GeneralPostPolling(pc);
Thread generalGetThread = new GeneralGetPolling(pc);
generalPostThread.start();
generalGetThread.start();
/*JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel label = new JLabel("22Hours");
Expand Down
10 changes: 4 additions & 6 deletions dev/Client(PC)/system-monitor/PCClient/Module/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
RuntimeMXBean runbean = (RuntimeMXBean) ManagementFactory.getRuntimeMXBean();
public String showCPU() {
try {
int cpuUsage = (int)osBean.getSystemCpuLoad() * 100;
String temp = Integer.toString(cpuUsage);
/*String cpuUsage = "CPU Usage : ";
String cpuUsage = "";
cpuUsage += String.format("%.2f", osBean.getSystemCpuLoad() * 100);
cpuUsage += "%\n";
Thread.sleep(1000);*/
return temp;
cpuUsage += "%";
Thread.sleep(1000);
return cpuUsage;
}
catch(Exception e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit fa8917f

Please sign in to comment.