package jp.dip.arimodoki.websocket;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import jp.dip.arimodoki.common.CConst;
import jp.dip.arimodoki.common.JsonConvert;
import jp.dip.arimodoki.common.JsonConvertIf;
import jp.dip.arimodoki.model.data.CpuUsed;
import jp.dip.arimodoki.model.data.CpuUsedIf;
public class CPUHandlerRun implements Runnable,CConst {
private final int LOOP_MAX = 40;
volatile Thread kicker = null;
private int loop = 0;
private int curpos = 0;
private long old_time=0;
private double old_use=0.0;
private JsonConvertIf jsonConvert = new JsonConvert();
private List<CpuUsedIf> cpuUsed = new ArrayList<>();
private WebSocketSession session;
private Map<String, WebSocketSession> sessionMap ;
public CPUHandlerRun() {}
public CPUHandlerRun(WebSocketSession session, Map<String, WebSocketSession> sessionMap) {
this.session = session;
this.sessionMap = sessionMap;
}
private double calc_usage() throws Exception {
File file = new File("/proc/stat");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine().trim();
String[] vals = line.split("\\s+");
int usr = Integer.parseInt(vals[1]);
int nice = Integer.parseInt(vals[2]);
int sys = Integer.parseInt(vals[3]);
reader.close();
long now = System.currentTimeMillis() / 10;
double usage = (usr + nice + sys - old_use) / (now - old_time);
old_use = usr + nice + sys;
old_time = now;
return usage*10;
}
public void start() {
if (this.kicker == null) {
this.loop = 0;
this.curpos = 0;
this.cpuUsed.clear();
for(int i = 0 ; i < LOOP_MAX ; i++) {
CpuUsedIf used = new CpuUsed("0",0);
this.cpuUsed.add(used);
}
this.kicker = new Thread(this);
this.kicker.start();
}
}
public void stop() {
if (this.kicker != null) {
this.kicker = null;
}
}
public void run() {
while (this.kicker != null) {
Thread thisThread = Thread.currentThread();
if (this.kicker == thisThread) {
if (loop < LOOP_MAX) {
double usage = 0.0;
try {
usage = this.calc_usage();
} catch (Exception e) {
logger.log_error(e,"calc_usage");
}
CpuUsedIf used = this.cpuUsed.get(this.curpos);
Calendar cal = Calendar.getInstance();
used.setTime(Integer.toString(cal.get(Calendar.SECOND)));
used.setUsed(usage);
String msg = "";
try {
msg = this.jsonConvert.Serialize(this.cpuUsed);
} catch (Exception e) {
logger.log_error(e,"used Serialize");
}
this.curpos++;
for (Entry<String, WebSocketSession> entry : this.sessionMap.entrySet()) {
try {
String targetId = entry.getKey();
if(targetId.equals(this.session.getId())) {
entry.getValue().sendMessage(new TextMessage(msg.getBytes()));
}
} catch (Exception e) {
logger.log_error(e,"sendMessage");
}
}
loop++;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
logger.log_error(e,"sleep");
}
} else {
this.stop();
}
}
}
}
}
|