Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ protected void onCreate(Bundle savedInstanceState) {


}

String target = getIntent().getStringExtra("navigateToFragment");
if (target != null && target.equals("PingFragment")) {
navController.navigate(R.id.ping_fragment);
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public int getCount() {
return count;
}

public int getTimeoutMillis() {
public double getTimeoutMillis() {
return timeoutMillis;
}

Expand All @@ -62,7 +62,7 @@ public Network getNetwork() {

private String destination;
private int count;
private int timeoutMillis;
private double timeoutMillis;
private int packetSize;
private double intervalMillis;
private Network network;
Expand Down Expand Up @@ -118,7 +118,7 @@ public PingParameter(String stringParameter, String testUUID) {
count = Integer.parseInt(parts[i + 1]);
break;
case "-W":
timeoutMillis = Integer.parseInt(parts[i + 1]);
timeoutMillis = Double.parseDouble(parts[i + 1]);
break;
case "-s":
packetSize = Integer.parseInt(parts[i + 1]);
Expand Down Expand Up @@ -182,7 +182,7 @@ protected PingParameter(Parcel in) {
super(in);
destination = in.readString();
count = in.readInt();
timeoutMillis = in.readInt();
timeoutMillis = in.readDouble();
packetSize = in.readInt();
intervalMillis = in.readDouble();
network = in.readParcelable(Network.class.getClassLoader());
Expand Down Expand Up @@ -210,7 +210,7 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeString(destination);
dest.writeInt(count);
dest.writeInt(timeoutMillis);
dest.writeDouble(timeoutMillis);
dest.writeInt(packetSize);
dest.writeDouble(intervalMillis);
dest.writeParcelable(network, flags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import static android.view.View.GONE;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.content.Context;
Expand Down Expand Up @@ -52,6 +53,7 @@
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.METRIC_TYPE;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.MetricCalculator;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Metric.MetricView;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PacketLossLine;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.RTTLine;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.Worker.PingWorker;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Preferences.SPType;
Expand Down Expand Up @@ -79,6 +81,7 @@ public class PingFragment extends Fragment {
private LiveData<WorkInfo> workInfoLiveData;

private MaterialTextView pingTextView;
private int counter = 0;

public PingFragment() {
}
Expand Down Expand Up @@ -140,33 +143,49 @@ private void checkLastUUID(String uuidStr) {
Log.d(TAG, "registerObserver: workInfo-State: " + workInfo.getState());
String rtt = progress.getString(PingWorker.RTT);
if(rtt != null) {
Log.d(TAG, "checkLastUUID: got RTT line");
RTTLine rttLine = new Gson().fromJson(rtt, RTTLine.class);
Log.d(TAG, "checkLastUUID: got ICMP-SEQ: "+rttLine.getIcmpSeq());
rttMetric.update(rttLine.getRtt());
StringBuilder sb = new StringBuilder();
sb.append("Host: ").append(rttLine.getHost()).append(" ");
sb.append("Seq: ").append(rttLine.getIcmpSeq()).append(" ");
sb.append("TTL: ").append(rttLine.getTtl()).append(" ");
sb.append("RTT: ").append(rttLine.getRtt()).append(" ms");
pingTextView.setText(sb.toString()+"\n"+pingTextView.getText());

counter++;
if(counter > 1e4){
pingTextView.setText("");
rttMetric.getMetricCalculator().resetMetric();
packetLossMetric.getMetricCalculator().resetMetric();
counter = 0;
}
}


double packetLoss = progress.getDouble(PingWorker.PACKET_LOSS, -1.0);

if(packetLoss != -1.0) {
packetLossMetric.setVisibility(View.VISIBLE);
Log.d(TAG, "onChanged: Packet Loss: " + packetLoss);
packetLossMetric.update(packetLoss);
String packetLoss = progress.getString(PingWorker.PACKET_LOSS);
if(packetLoss != null){
Log.d(TAG, "checkLastUUID: got PACKETLOSS line");
PacketLossLine packetLossLine = new Gson().fromJson(packetLoss,PacketLossLine.class);
packetLossMetric.update(packetLossLine.getPacketLoss());
packetLossMetric.setVisibility(VISIBLE);
}


switch (workInfo.getState()) {
case RUNNING:
case SUCCEEDED:
break;
case FAILED:
Log.e(TAG, "checkLastUUID: Work failed: " + workInfo.getOutputData().getString(PingWorker.REASON));
pingTextView.setText(workInfo.getOutputData().getString(PingWorker.REASON));
rttMetric.getMetricCalculator().resetMetric();
packetLossMetric.getMetricCalculator().resetMetric();
packetLossMetric.setVisibility(GONE);
spg.getSharedPreference(SPType.ping_sp).edit().putBoolean("ping_running", false).apply();
toggleGroup.check(R.id.ping_stop);
break;
case CANCELLED:
workInfoLiveData.removeObserver(observer); // Optionally clean up
// workInfoLiveData.removeObserver(observer); // Optionally clean up
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ public Point getPoint(){
.time(System.currentTimeMillis(), WritePrecision.MS);
//ping does not provide timestamp for packet loss line
}
@Override
public String toString() {
return "PacketLossLine{" +
"packetLoss=" + packetLoss +
", packetsTransmitted=" + packetsTransmitted +
", packetsReceived=" + packetsReceived + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private void stopWorker(){
return;
}
workManager.cancelWorkById(UUID.fromString(lastUUID));
workManager.cancelAllWorkByTag(PingWorker.TAG);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;

Expand All @@ -32,13 +34,16 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Inputs.PingInput;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.MainActivity;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Parameter.PingParameter;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PacketLossLine;
import de.fraunhofer.fokus.OpenMobileNetworkToolkit.Ping.PingInformations.PingInformation;
Expand Down Expand Up @@ -93,19 +98,33 @@ public PingWorker(@NonNull Context context, @NonNull WorkerParameters workerPara
}

private ForegroundInfo createForegroundInfo(String progress) {
notification = notificationBuilder
Intent intent = new Intent(this.ct, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("navigateToFragment", "PingFragment"); // Identifier for your fragment

PendingIntent pendingIntent = PendingIntent.getActivity(
this.ct,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);

Notification notification = notificationBuilder
.setContentTitle("Ping")
.setContentText(progress)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setColor(Color.WHITE)
.setSmallIcon(R.mipmap.ic_launcher_foreground)
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_DEFAULT)
.setContentIntent(pendingIntent) // 👈 attach pending intent here
.build();

return new ForegroundInfo(notificationID, notification, FOREGROUND_SERVICE_TYPE);
}



@NonNull
@Override
public Result doWork() {
Expand All @@ -128,6 +147,7 @@ public Result doWork() {

Log.d(TAG, "doWork: executing " + String.join(" ", command));
int result = -1;
String error = "";
try {


Expand All @@ -137,6 +157,7 @@ public Result doWork() {
FileOutputStream pingStream = new FileOutputStream(pingInput.getPingParameter().getLogfile(), true);
PingParser pingParser = new PingParser();
String line;

while ((line = reader.readLine()) != null) {
Log.d(TAG, "doWork: "+line);
pingStream.write((line + "\n").getBytes());
Expand All @@ -151,16 +172,9 @@ public Result doWork() {
switch (pingInformation.getLineType()){
case RTT:
rtt = ((RTTLine)pingInformation).getRtt();

progressOutput.putDouble(RTT, rtt);
progressOutput.putString(RTT, new Gson().toJson((RTTLine)pingInformation));


setProgressAsync(progressOutput.build());
// setProgressAsync(progressOutput.build());
setForegroundAsync(createForegroundInfo(((RTTLine) pingInformation).getHost()+": " + rtt + " ms"));



Log.d(TAG, "doWork: RTT: " + rtt);
break;
case UNREACHABLE:
Expand All @@ -171,25 +185,36 @@ public Result doWork() {
progressOutput.putString(REASON, "Request timeout");
break;
case PACKET_LOSS:
double packetLoss = ((PacketLossLine)pingInformation).getPacketLoss();
setProgressAsync(new Data.Builder().putDouble(PACKET_LOSS, packetLoss).build());
Log.d(TAG, "doWork: Packet Loss: " + packetLoss);
PacketLossLine packetLoss = ((PacketLossLine)pingInformation);
progressOutput.putString(PACKET_LOSS, new Gson().toJson(packetLoss));
// setProgressAsync(progressOutput.build());
Log.d(TAG, "doWork: Packet Loss: " + packetLoss.toString());
break;
case UNKNOWN:
Log.w(TAG, "doWork: Unknown line type");
break;
}
setProgressAsync(progressOutput.build());

if(this.isStopped()){
break;
}


}
error = new BufferedReader(
new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
process.destroy();
pingStream.close();
reader.close();

result = process.waitFor();



Thread.sleep(200); // Sleep so that the Observer can process the last lines
} catch (IOException e) {
Log.e(TAG, "Error while executing ping command: " + e.toString());
return Result.failure(output.putString(REASON, "Error while executing ping command.").build());
Expand All @@ -198,9 +223,10 @@ public Result doWork() {
return Result.failure(output.putString(REASON, "Error while waiting for ping command.").build());
}

if (result != 0) {
if (result != 0) {;
Log.e(TAG, "Ping command failed with result: " + result);
return Result.failure(output.putString(REASON, "Ping command failed with result: " + result).build());
Log.e(TAG, "Ping command failed because: "+error);
return Result.failure(output.putString(REASON, "Ping command failed: " + error).build());
}

return Result.success(output.putBoolean(PING, true).build());
Expand Down