Currently, I'm working on speed test CLI and I successfully implement CLI with java with Jython. But I got an issue that every time calculate server-side internet speed and I want to consume user side network resources. so anybody knows what is the correct way of implementation this java code is following:
package com.speedtest.serviceimpl;
import java.io.InputStream;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.speedtest.dto.SpeedTestResponse;
import com.speedtest.service.SpeedTestJythonService;
@Service
public class JythonService implements SpeedTestJythonService {
private static final Logger Log =LoggerFactory.getLogger(JythonService.class);
PythonInterpreter pythonInterpreter;
public JythonService() {
pythonInterpreter = new PythonInterpreter();
}
@Override
public SpeedTestResponse execMethodInPyClass(InputStream inputStream,HttpServletRequest request) {
try {
StringWriter stringWriter = new StringWriter();
Double upload_speed = new Double(0.0);
Double download_speed = new Double(0.0);
Double download_bytes = new Double(0.0);
Double upload_bytes = new Double(0.0);
String service_provider_name = new String();
pythonInterpreter.setOut(stringWriter);
pythonInterpreter.execfile(inputStream);
System.out.println("Before Response==> "+stringWriter.toString());
String res = stringWriter.toString().replace("|", "");
System.out.println("Aftr Response==> "+res);
JSONObject obj = new JSONObject(res);
Double download = obj.getDouble("download");
Double upload = obj.getDouble("upload");
Double ping = obj.getDouble("ping");
Long bytes_sent = obj.getLong("bytes_sent");
Long bytes_received = obj.getLong("bytes_received");
JSONObject json_server = obj.getJSONObject("server");
String name = json_server.getString("name");
String country_name = json_server.getString("country");
JSONObject json_client = obj.getJSONObject("client");
String isp = json_client.getString("isp");
String ip = json_client.getString("ip");
String country_code = json_client.getString("country");
String isprating = json_client.getString("isprating");
Log.info("obj", obj.toString());
Log.info("download", download.toString());
Log.info("upload", upload.toString());
Log.info("ping", ping.toString());
Log.info("bytes_sent", bytes_sent.toString());
Log.info("bytes_received", bytes_received.toString());
download_speed = (download / 1000.0 / 1000.0);
upload_speed = (upload / 1000.0 / 1000.0);
download_bytes = bytes_received.doubleValue();
upload_bytes = bytes_sent.doubleValue();
SpeedTestResponse response = new SpeedTestResponse();
response.setDownload(download_speed);
response.setIspName(isp);
response.setPing(ping);
response.setUpload(upload_speed);
return response;
} catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
}
python code is SpeedTest.py