Skip to content

Commit

Permalink
Bundleclient content provider fixes (#28)
Browse files Browse the repository at this point in the history
* cleaned up code for bundle client content provider

* content provider bug fixes

* completed insert and query messages from bundleclient content provider

* transport bug fixes
  • Loading branch information
ManasC478 authored Feb 18, 2024
1 parent 2ab61c1 commit ec0a359
Show file tree
Hide file tree
Showing 12 changed files with 326 additions and 231 deletions.
3 changes: 2 additions & 1 deletion BundleClient/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
android:multiprocess="true"
android:grantUriPermissions="true"
android:writePermission="android.permission.messagingproviderwriteperm"
android:readPermission="android.permission.messagingproviderreadperm"></provider>
android:readPermission="android.permission.messagingproviderreadperm">
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@ public void processAcknowledgement(String bundleId) {
Map<String, Long> largestADUIdDeliveredDetails = this.getLargestADUIdDeliveredDetails();
for (String appId : details.keySet()) {
Long aduId = details.get(appId);
this.dataStoreAdaptor.deleteADUs(appId, aduId);
try {
this.dataStoreAdaptor.deleteADUs(appId, aduId);
} catch (IOException e){
Log.e("bundleclient", "Could not delete ADUs up to adu: "+aduId+", error: "+e.getMessage());
continue;
}

if (!largestADUIdDeliveredDetails.containsKey(appId)
|| aduId > largestADUIdDeliveredDetails.get(appId)) {
largestADUIdDeliveredDetails.put(appId, aduId);
Expand Down Expand Up @@ -298,10 +304,16 @@ public void storeADUs(List<ADU> adus) {
if (largestAduIdReceived != null && adu.getADUId() <= largestAduIdReceived) {
continue;
}
Log.d(HelloworldActivity.TAG, "[ADM] Updating Largest ADU id: "+adu.getADUId()+","+adu.getSource());
this.stateManager.updateLargestADUIdReceived(adu.getAppId(), adu.getADUId());
Log.d(HelloworldActivity.TAG, "[ADM] Updated Largest ADU id: "+adu.getADUId()+","+adu.getSource());
this.dataStoreAdaptor.persistADU(adu);


try {
this.dataStoreAdaptor.persistADU(adu);
Log.d(HelloworldActivity.TAG, "[ADM] Updating Largest ADU id: "+adu.getADUId()+","+adu.getSource());
this.stateManager.updateLargestADUIdReceived(adu.getAppId(), adu.getADUId());
Log.d(HelloworldActivity.TAG, "[ADM] Updated Largest ADU id: "+adu.getADUId()+","+adu.getSource());
} catch (IOException e) {
Log.e("bundleclient", "Could not persist adu: "+adu.getADUId()+", error: " + e.getMessage());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public DataStoreAdaptor(String appRootDataDirectory){
receiveFileStoreHelper = new FileStoreHelper(appRootDataDirectory + "/receive");
}

private void sendDataToApp(ADU adu){
private void sendDataToApp(ADU adu) throws IOException {
//notify app that someone sent data for the app
Intent intent = new Intent("android.intent.dtn.SEND_DATA");
intent.setPackage(adu.getAppId());
intent.setType("text/plain");
Log.d(HelloworldActivity.TAG,new String(receiveFileStoreHelper.getDataFromFile(adu.getSource()))+", Source:"+adu.getSource());
intent.putExtra(Intent.EXTRA_TEXT, receiveFileStoreHelper.getDataFromFile(adu.getSource()));
byte[] data = receiveFileStoreHelper.getDataFromFile(adu.getSource());
Log.d(HelloworldActivity.TAG,new String(data)+", Source:"+adu.getSource());
intent.putExtra(Intent.EXTRA_TEXT, data);
applicationContext = HelloworldActivity.ApplicationContext;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
applicationContext.startForegroundService(intent);
Expand All @@ -51,9 +52,9 @@ private void sendDataToApp(ADU adu){
}
}

public void persistADU(ADU adu) {
public void persistADU(ADU adu) throws IOException {
Log.d(HelloworldActivity.TAG,"Persisting ADUs: " + adu.getADUId() +","+ adu.getSource());
receiveFileStoreHelper.AddFile(adu.getAppId(), receiveFileStoreHelper.getDataFromFile(adu.getSource()));
receiveFileStoreHelper.addFile(adu.getAppId(), receiveFileStoreHelper.getDataFromFile(adu.getSource()));
sendDataToApp(adu);
System.out.println(
"[ADM-DSA] Persisting inbound ADU "
Expand All @@ -69,7 +70,7 @@ public void persistADU(ADU adu) {
receiveFileStoreHelper.deleteFile(aduId+"");
}*/

public void deleteADUs(String appId, long aduIdEnd) {
public void deleteADUs(String appId, long aduIdEnd) throws IOException {

sendFileStoreHelper.deleteAllFilesUpTo(appId, aduIdEnd);
System.out.println("[DSA] Deleted ADUs for application " + appId + " with id upto " + aduIdEnd);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ddd.datastore.filestore;

import android.net.Uri;
import android.util.Log;

import com.ddd.client.applicationdatamanager.ApplicationDataManager;
Expand All @@ -11,6 +12,7 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
Expand All @@ -28,7 +30,7 @@ public FileStoreHelper(String rootFolder, String appFolder){
this.appFolder = appFolder;
}

public static String convertStreamToString(InputStream is) throws Exception {
public static String convertStreamToString(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
Expand All @@ -39,7 +41,7 @@ public static String convertStreamToString(InputStream is) throws Exception {
return sb.toString();
}

public static String getStringFromFile (String filePath) throws Exception {
public static String getStringFromFile (String filePath) throws IOException {
File fl = new File(filePath);
System.out.println(filePath);
FileInputStream fin = new FileInputStream(fl);
Expand All @@ -49,91 +51,111 @@ public static String getStringFromFile (String filePath) throws Exception {
return ret;
}

public Metadata getMetadata(String folder){
try {
String data = getStringFromFile(RootFolder + "/" + folder + "/metadata.json");
Gson gson = new Gson();
return gson.fromJson(data, Metadata.class);
} catch (Exception e) {
e.printStackTrace();
public void createAppIdDirIfNotExists(String appId) throws IOException {
File f = new File(RootFolder + File.separator + appId);
if (f.isFile()) {
f.delete();
}
if (!f.exists()) {
f.mkdirs();
registerAppId(appId);
}
return null;

getIfNotCreateMetadata(appId);
}

public void setMetadata(String folder, Metadata metadata){
try {
File metadataFile = new File(RootFolder + "/" + folder + "/metadata.json");
metadataFile.createNewFile();
Gson gson = new Gson();
String metadataString = gson.toJson(metadata);
FileOutputStream oFile = new FileOutputStream(metadataFile, false);
oFile.write(metadataString.getBytes());
oFile.close();
} catch (Exception e) {
e.printStackTrace();
}
private Metadata getMetadata(String folder) throws IOException{
String data = getStringFromFile(RootFolder + File.separator + folder + File.separator + "metadata.json");
Gson gson = new Gson();
return gson.fromJson(data, Metadata.class);
}

public byte[] getDataFromFile(File file){
private Metadata getIfNotCreateMetadata(String folder) throws IOException {
try {
FileInputStream fis = new FileInputStream(file);
byte[] res = new byte[fis.available()];
fis.read(res);
return res;
}catch (Exception ex){
ex.printStackTrace();
return getMetadata(folder);
} catch (FileNotFoundException e) {
setMetadata(folder, new Metadata(0, 0,0,0));
return getMetadata(folder);
}
return null;
}

private byte[] readFile(String file){
try {
File f = new File(file);
FileInputStream fis = new FileInputStream(f);
byte[] res = new byte[fis.available()];
fis.read(res);
return res;
}catch (Exception ex){
ex.printStackTrace();
}
return null;
private void setMetadata(String folder, Metadata metadata) throws IOException{
Gson gson = new Gson();
String metadataString = gson.toJson(metadata);
writeFile(RootFolder + File.separator + folder + File.separator + "metadata.json", metadataString.getBytes());
}

public byte[] getDataFromFile(File file) throws IOException{
FileInputStream fis = new FileInputStream(file);
byte[] res = new byte[fis.available()];
fis.read(res);
fis.close();
return res;
}

private byte[] readFile(String file) throws IOException {
File f = new File(file);
return getDataFromFile(f);
}

private void writeFile(File file, byte[] data) throws IOException {
FileOutputStream oFile = new FileOutputStream(file, false);
oFile.write(data);
oFile.close();
}

private void writeFile(String filePath, byte[] data) throws IOException {
File file = new File(filePath);
file.createNewFile();
writeFile(file, data);
}

public List<byte[]> getAppData(String appId){
public List<byte[]> getAppData(String appId) throws IOException{
List<byte[]> appDataList = new ArrayList<>();
String folder = RootFolder+"/"+appId;
Metadata metadata = getMetadata(folder);
Metadata metadata = getIfNotCreateMetadata(appId);
String folder = RootFolder+File.separator+appId;

for(long i=metadata.lastProcessedMessageId+1;i <= metadata.lastReceivedMessageId;i++){
appDataList.add(readFile(folder+"/"+i+".txt"));
appDataList.add(readFile(folder+File.separator+i+".txt"));
}
//metadata.lastProcessedMessageId= metadata.lastReceivedMessageId;
//setMetadata(folder, metadata);
return appDataList;
}

public byte[] getADU(String appId, String aduId){
return readFile(RootFolder+"/"+ appId+"/"+aduId+".txt");
public List<byte[]> getAllAppData(String appId) throws IOException {
List<byte[]> dataList = new ArrayList<>();
Metadata metadata = getIfNotCreateMetadata(appId);
String folder = RootFolder+File.separator+appId;
for(long i=1; i <= metadata.lastReceivedMessageId;i++){
byte[] data = readFile(folder+File.separator+i+".txt");
Log.d("bundleclient", data.toString());
dataList.add(data);
}

return dataList;
}

public byte[] getADU(String appId, String aduId) throws IOException{
return readFile(RootFolder+File.separator+ appId+File.separator+aduId+".txt");
}

public File getADUFile(String appId, String aduId){
return new File(RootFolder+"/"+ appId+"/"+aduId+".txt");
return new File(RootFolder+File.separator+ appId+File.separator+aduId+".txt");
}

public byte[] getNextAppData(String folder){
Metadata metadata = getMetadata(folder);
if(metadata==null){
metadata = new Metadata(1, 0,0,0);
setMetadata(folder, metadata);
}
public byte[] getNextAppData(String folder) throws IOException{
Metadata metadata = getIfNotCreateMetadata(folder);
long nextMessageId = metadata.lastProcessedMessageId+1;
if(nextMessageId> metadata.lastReceivedMessageId){
Log.d("deepak","no data to show");
if(nextMessageId > metadata.lastReceivedMessageId){
Log.d("bundleclient","no data to show");
if(nextMessageId>1){
nextMessageId--;
}else
}else {
return null;
}
}
byte[] appData = readFile(RootFolder + "/" + folder+"/"+nextMessageId+".txt");
byte[] appData = readFile(RootFolder + File.separator + folder+File.separator+nextMessageId+".txt");
metadata.lastProcessedMessageId = nextMessageId;
setMetadata(folder, metadata);
return appData;
Expand All @@ -152,59 +174,26 @@ private void registerAppId(String appId){
adm.registerAppId(appId);
}

public void AddFile(String folder, byte data[]){
File f = new File(RootFolder+"/"+folder);
if(f.isDirectory()){
Log.d("deepak", RootFolder+"/"+folder+" is a directory");
int noOfMessages = f.list().length;
Log.d("deepak", "noOfMessages-"+noOfMessages);
File dataFile = new File(RootFolder+"/"+folder+"/"+noOfMessages+".txt");
FileOutputStream oFile = null;
try {
dataFile.createNewFile();
oFile = new FileOutputStream(dataFile, false);
oFile.write(data);
oFile.close();

Metadata metadata = getMetadata(folder);
metadata.lastReceivedMessageId++;
setMetadata(folder, metadata);
} catch (Exception e) {
Log.d("deepak", "Error: "+e.getMessage());
e.printStackTrace();
}
}else{
//first ADU for an application
registerAppId(folder);
f.mkdirs();
File metadataFile = new File(RootFolder +"/"+ folder + "/metadata.json");
try {
new File(RootFolder +"/"+ folder).mkdirs();
metadataFile.createNewFile();
Gson gson = new Gson();
Metadata metadata = new Metadata(0, 0,1,0);
String metadataString = gson.toJson(metadata);
FileOutputStream oFile = new FileOutputStream(metadataFile, false);
oFile.write(metadataString.getBytes());
oFile.close();

File dataFile = new File(RootFolder +"/"+ folder +"/1.txt");
dataFile.createNewFile();
oFile = new FileOutputStream(dataFile, false);
oFile.write(data);
oFile.close();
}catch(Exception ex){
Log.d("deepak", "error"+ex.getMessage());
ex.printStackTrace();
}
public Uri addFile(String folder, byte data[]) throws IOException {
Metadata metadata = getIfNotCreateMetadata(folder);
metadata.lastReceivedMessageId++;
setMetadata(folder, metadata);

}
File f = new File(RootFolder+File.separator+folder);
String messagePath;

int numFile = f.list().length;
messagePath = f.getPath()+File.separator+numFile+".txt";


writeFile(messagePath, data);
return Uri.parse(messagePath);
}

public void deleteAllFilesUpTo(String appId, long aduId){
public void deleteAllFilesUpTo(String appId, long aduId) throws IOException{
//check if there are enough files
String folder = appId;
Metadata metadata = getMetadata(folder);
Metadata metadata = getIfNotCreateMetadata(folder);
if(metadata.lastSentMessageId >= aduId){
System.out.println("[FileStoreHelper.deleteAllFilesUpTo] Data already deleted.");
return;
Expand All @@ -218,7 +207,7 @@ public void deleteAllFilesUpTo(String appId, long aduId){
}

public void deleteFile(String fileName){
File file = new File(RootFolder+"/"+fileName);
File file = new File(RootFolder+File.separator+fileName);
file.delete();
}
}
Loading

0 comments on commit ec0a359

Please sign in to comment.