Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft implementation of FDO_SYS:fetch #637

Merged
merged 3 commits into from
Nov 13, 2023
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 @@ -335,6 +335,12 @@ private void fetch(String fetchFileName, int mtu) throws IOException {

String fileName = fetchFileName;
logger.info("Filename " + fileName);

ServiceInfoKeyValuePair kvPair = new ServiceInfoKeyValuePair();
kvPair.setKeyName(FdoSys.FETCHFILE);
kvPair.setValue(Mapper.INSTANCE.writeValue(fileName));
queue.add(kvPair);

if (!Path.of(fetchFileName).isAbsolute()) {
fileName = Path.of(getAppData(), fetchFileName).toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

package org.fidoalliance.fdo.protocol.db;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Map;
Expand Down Expand Up @@ -43,6 +48,7 @@ public class FdoSysOwnerModule implements ServiceInfoModule {


private final LoggerService logger = new LoggerService(FdoSysOwnerModule.class);
private String fetchFileName;

@Override
public String getName() {
Expand Down Expand Up @@ -103,6 +109,11 @@ public void receive(ServiceInfoModuleState state, ServiceInfoKeyValuePair kvPair
}
}
break;
case FdoSys.FETCHFILE:
if (state.isActive()) {
fetchFileName = Mapper.INSTANCE.readValue(kvPair.getValue(), String.class);
}
break;
case FdoSys.DATA:
if (state.isActive()) {
byte[] data = Mapper.INSTANCE.readValue(kvPair.getValue(), byte[].class);
Expand Down Expand Up @@ -174,6 +185,11 @@ protected boolean infoReady(FdoSysModuleExtra extra) {
&& extra.getFilter().containsKey(DevMod.KEY_ARCH);
}

private String getAppData() throws IOException {
File file = new File(System.getProperty("app-data.dir"));
return file.getCanonicalPath();
}

protected boolean checkFilter(Map<String, String> devMap, Map<String, String> filterMap) {
return !devMap.entrySet().containsAll(filterMap.entrySet());
}
Expand All @@ -187,7 +203,17 @@ protected void onStatusCb(ServiceInfoModuleState state, FdoSysModuleExtra extra,
protected void onFetch(ServiceInfoModuleState state, FdoSysModuleExtra extra,
byte[] data) throws IOException {

logger.warn(new String(data, StandardCharsets.US_ASCII));
Path path = Paths.get(getAppData(), fetchFileName);
try {
if (Files.exists(path)) {
Files.write(path, data, StandardOpenOption.APPEND);
} else {
Files.write(path, data, StandardOpenOption.CREATE_NEW);
}
} catch (IOException e) {
logger.error("Error writing to file: " + e.getMessage());
}

}

protected void onEot(ServiceInfoModuleState state, FdoSysModuleExtra extra, EotResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public final class FdoSys {
public static final String DATA = NAME + ":data";
public static final String EOT = NAME + ":eot";
public static final String FILEDESC = NAME + ":filedesc";
public static final String FETCHFILE = NAME + ":fetchfile";
public static final String WRITE = NAME + ":write";
public static final String KEEP_ALIVE = NAME + ":keepalive";
public static final String RET_CODE = NAME + ":retcode";
Expand Down
Loading