Skip to content

Commit

Permalink
use TransportPaths class instead of static path variables (#354)
Browse files Browse the repository at this point in the history
Co-authored-by: Darren Shen <[email protected]>
  • Loading branch information
pankinkun and Darren Shen authored Oct 15, 2024
1 parent c3a425f commit 61f3141
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.discdd.android.fragments.LogFragment;
import net.discdd.android.fragments.PermissionsFragment;
import net.discdd.pathutils.TransportPaths;
import net.discdd.transport.TransportSecurity;

import org.whispersystems.libsignal.InvalidKeyException;
Expand All @@ -46,6 +47,7 @@ public class BundleTransportActivity extends AppCompatActivity {
private TitledFragment serverUploadFragment;
private TitledFragment transportWifiFragment;
private TitledFragment storageFragment;
private TransportPaths transportPaths;

record ConnectivityEvent(boolean internetAvailable) {}

Expand Down Expand Up @@ -81,6 +83,7 @@ protected void onCreate(Bundle savedInstanceState) {

LogFragment.registerLoggerHandler();

this.transportPaths = new TransportPaths(getApplicationContext().getExternalFilesDir(null).toPath());
var resources = getApplicationContext().getResources();

try (InputStream inServerIdentity = resources.openRawResource(net.discdd.android_core.R.raw.server_identity)) {
Expand All @@ -92,8 +95,8 @@ protected void onCreate(Bundle savedInstanceState) {

serverUploadFragment = new TitledFragment(getString(R.string.upload),
new ServerUploadFragment(connectivityEventPublisher,
transportSecurity.getTransportID()));
transportWifiFragment = new TitledFragment(getString(R.string.local_wifi), new TransportWifiDirectFragment());
transportSecurity.getTransportID(), this.transportPaths));
transportWifiFragment = new TitledFragment(getString(R.string.local_wifi), new TransportWifiDirectFragment(this.transportPaths));
storageFragment = new TitledFragment("Storage Settings", new StorageFragment());

permissionsFragment = new PermissionsFragment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;

import android.content.Context;

import net.discdd.bundlerouting.service.BundleExchangeServiceImpl;
import net.discdd.grpc.BundleSender;
import net.discdd.grpc.GetRecencyBlobRequest;
import net.discdd.grpc.GetRecencyBlobResponse;
import net.discdd.pathutils.TransportPaths;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
Expand All @@ -23,8 +20,6 @@
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.okhttp.OkHttpServerBuilder;
import io.grpc.stub.StreamObserver;

public class RpcServer {
Expand All @@ -42,12 +37,12 @@ public RpcServer(BundleExchangeServiceImpl.BundleExchangeEventListener listener)
this.listener = listener;
}

public void startServer(Context context) {
public void startServer(TransportPaths transportPaths) {
if (server != null && !server.isShutdown()) {
return;
}
var toServerPath = context.getExternalFilesDir(null).toPath().resolve("BundleTransmission/server");
var toClientPath = context.getExternalFilesDir(null).toPath().resolve("BundleTransmission/client");
var toServerPath = transportPaths.toServerPath;
var toClientPath = transportPaths.toClientPath;
var bundleExchangeService = new BundleExchangeServiceImpl() {
@Override
protected void onBundleExchangeEvent(BundleExchangeEvent bundleExchangeEvent) {
Expand All @@ -57,7 +52,7 @@ protected void onBundleExchangeEvent(BundleExchangeEvent bundleExchangeEvent) {
@Override
protected Path pathProducer(BundleExchangeName bundleExchangeName, BundleSender bundleSender) {
return bundleExchangeName.isDownload() ? toClientPath.resolve(bundleExchangeName.encryptedBundleId()) :
toServerPath.resolve(bundleExchangeName.encryptedBundleId());
toServerPath.resolve(bundleExchangeName.encryptedBundleId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import androidx.fragment.app.Fragment;

import net.discdd.pathutils.TransportPaths;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SubmissionPublisher;
Expand All @@ -35,11 +37,13 @@ public class ServerUploadFragment extends Fragment {
private TextView serverConnnectedStatus;
private String transportID;
private ExecutorService executor = Executors.newFixedThreadPool(2);
private TransportPaths transportPaths;

public ServerUploadFragment(SubmissionPublisher<BundleTransportActivity.ConnectivityEvent> connectivityFlow,
String transportID) {
String transportID, TransportPaths transportPaths) {
this.connectivityFlow = connectivityFlow;
this.transportID = transportID;
this.transportPaths = transportPaths;
}

@Override
Expand Down Expand Up @@ -86,8 +90,9 @@ private void connectToServer() {
"Initiating server exchange to " + serverDomain + ":" + serverPort + "...\n"));

TransportToBundleServerManager transportToBundleServerManager =
new TransportToBundleServerManager(requireActivity().getExternalFilesDir(null).toPath(),
serverDomain, serverPort, this::connectToServerComplete,
new TransportToBundleServerManager(transportPaths,
serverDomain, serverPort,
this::connectToServerComplete,
e -> connectToServerError(e, serverDomain + ":" + serverPort));
executor.execute(transportToBundleServerManager);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;

import com.google.protobuf.ByteString;

import net.discdd.bundlerouting.service.BundleUploadResponseObserver;
import net.discdd.grpc.BundleChunk;
import net.discdd.grpc.BundleDownloadRequest;
Expand All @@ -18,6 +16,7 @@
import net.discdd.grpc.BundleUploadResponse;
import net.discdd.grpc.EncryptedBundleId;
import net.discdd.grpc.GetRecencyBlobRequest;
import net.discdd.pathutils.TransportPaths;
import net.discdd.utils.Constants;

import java.io.File;
Expand Down Expand Up @@ -52,16 +51,16 @@ public class TransportToBundleServerManager implements Runnable {
private final Function<Exception, Void> connectError;
private final String transportTarget;

public TransportToBundleServerManager(Path filePath, String host, String port,
public TransportToBundleServerManager(TransportPaths transportPaths, String host, String port,
Function<Void, Void> connectComplete,
Function<Exception, Void> connectError) {
this.connectComplete = connectComplete;
this.connectError = connectError;
this.transportTarget = host + ":" + port;
this.transportSenderId =
BundleSender.newBuilder().setId("bundle_transport").setType(BundleSenderType.TRANSPORT).build();
this.fromClientPath = filePath.resolve("BundleTransmission/server");
this.fromServerPath = filePath.resolve("BundleTransmission/client");
this.fromClientPath = transportPaths.toServerPath;
this.fromServerPath = transportPaths.toClientPath;
}

@Override
Expand All @@ -80,15 +79,6 @@ public void run() {
.addAllBundlesFromClientsOnTransport(bundlesFromClients)
.addAllBundlesFromServerOnTransport(bundlesFromServer).build());

try {
if (!Files.exists(fromServerPath) || !Files.isDirectory(fromClientPath)) {
Files.createDirectories(fromServerPath);
Files.createDirectories(fromClientPath);
}
} catch (Exception e) {
logger.log(SEVERE, "Failed to get inventory", e);
}

processDeleteBundles(inventoryResponse.getBundlesToDeleteList());
processUploadBundles(inventoryResponse.getBundlesToUploadList(), exchangeStub);
processDownloadBundles(inventoryResponse.getBundlesToDownloadList(), exchangeStub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.fragment.app.Fragment;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import net.discdd.pathutils.TransportPaths;
import net.discdd.wifidirect.WifiDirectManager;

import java.net.Inet4Address;
Expand All @@ -37,10 +38,12 @@ public class TransportWifiDirectFragment extends Fragment {
private TextView myWifiStatusView;
private SharedPreferences sharedPreferences;
private TransportWifiDirectService btService;
private TransportPaths transportPaths;

public TransportWifiDirectFragment() {
public TransportWifiDirectFragment(TransportPaths transportPaths) {
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_WIFI_EVENT_ACTION);
intentFilter.addAction(TransportWifiDirectService.NET_DISCDD_BUNDLETRANSPORT_CLIENT_LOG_ACTION);
this.transportPaths = transportPaths;
}

private void processDeviceInfoChange() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.discdd.bundletransport;

import android.content.Context;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ServiceInfo;
Expand All @@ -22,6 +22,7 @@

import net.discdd.android.fragments.LogFragment;
import net.discdd.bundlerouting.service.BundleExchangeServiceImpl;
import net.discdd.pathutils.TransportPaths;
import net.discdd.wifidirect.WifiDirectManager;
import net.discdd.wifidirect.WifiDirectStateListener;

Expand All @@ -46,11 +47,14 @@ public class TransportWifiDirectService extends Service
public static final String WIFI_DIRECT_PREFERENCE_BG_SERVICE = "background_wifi";
private final IBinder binder = new TransportWifiDirectServiceBinder();
private final RpcServer grpcServer = new RpcServer(this);
private TransportPaths transportPaths;
private WifiDirectManager wifiDirectManager;
private SharedPreferences sharedPreferences;
Context getApplicationContext;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
this.transportPaths = new TransportPaths(getApplicationContext().getExternalFilesDir(null).toPath());
super.onStartCommand(intent, flags, startId);
// TransportWifiDirectService doesn't use LogFragment directly, but we do want our
// logs to go to its logger
Expand Down Expand Up @@ -125,7 +129,7 @@ private void startRpcServer() {
if (grpcServer.isShutdown()) {
appendToClientLog("Starting gRPC server");
logger.log(INFO, "starting grpc server from main activity!!!!!!!");
grpcServer.startServer(getApplicationContext());
grpcServer.startServer(this.transportPaths);
appendToClientLog("server started");
}
}
Expand Down Expand Up @@ -181,4 +185,5 @@ TransportWifiDirectService getService() {
return TransportWifiDirectService.this;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package net.discdd.client.bundletransmission;

import com.google.protobuf.ByteString;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import io.grpc.Grpc;
import lombok.Getter;
import net.discdd.bundlerouting.RoutingExceptions;
import net.discdd.bundlerouting.WindowUtils.WindowExceptions;
Expand Down Expand Up @@ -56,7 +55,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -70,7 +68,6 @@
import java.util.function.Consumer;
import java.util.logging.Logger;

import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static java.util.logging.Level.WARNING;
Expand Down Expand Up @@ -477,6 +474,7 @@ private Path downloadBundles(List<String> bundleRequests, BundleSender sender,
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
logger.log(SEVERE, "Failed to close file output stream", e);
Expand Down
37 changes: 37 additions & 0 deletions bundle-core/src/main/java/net/discdd/pathutils/TransportPaths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.discdd.pathutils;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import static java.util.logging.Level.SEVERE;

// BundleTransport
// |_ files
// |_ BundelSecurity (TO-DO)
// |_ Server_Keys - server public keys
// |_ Transport_Keys - transport key pairs
// |_ BundleTransmission
// |_ client - bundles to send to client + recencyBlob
// |_ server - bundles to send to server

public class TransportPaths{
private static final Logger logger = Logger.getLogger(TransportPaths.class.getName());

public final Path toClientPath;

public final Path toServerPath;

public TransportPaths(Path rootDir){
this.toClientPath = rootDir.resolve("BundleTransmission/client");
this.toServerPath = rootDir.resolve("BundleTransmission/server");

try {
if (!Files.exists(toClientPath) || !Files.isDirectory(toServerPath)) {
Files.createDirectories(toClientPath);
Files.createDirectories(toServerPath);
}
} catch (Exception e) {
logger.log(SEVERE, "Failed to create transport storage directories", e);
}
}
}

0 comments on commit 61f3141

Please sign in to comment.