Permalink
Browse files

some file transfer works

  • Loading branch information...
Albert Zuendorf
Albert Zuendorf committed Sep 1, 2015
1 parent 7f01353 commit bfeae6d9432db9453a420b8e0e9424e668d6b913
@@ -0,0 +1 @@
+*.gitignore.*
@@ -0,0 +1 @@
+*.jsonchgs
@@ -0,0 +1 @@
+*.gitignore.*
@@ -0,0 +1 @@
+*.jsonchgs
@@ -0,0 +1 @@
+*.jsonchgs
@@ -0,0 +1 @@
+*.jsonchgs
@@ -1 +1,2 @@
*.jsonchgs
+*.gitignore.*
@@ -1,2 +1,4 @@
*.json
*.png
+*.jsonchgs
+*.docx
@@ -1 +1,2 @@
*.json
+*.jsonchgs
@@ -0,0 +1 @@
+*.gitignore.*
@@ -0,0 +1 @@
+*.jsonchgs
@@ -0,0 +1 @@
+*.jsonchgs
@@ -1 +1,5 @@
*.jsonchgs
+*.png.*
+*.gitignore.*
+*.docx.*
+*.db.*
@@ -1 +1,4 @@
*.json
+*.png
+*.jsonchgs
+*.docx
@@ -1,10 +1,18 @@
package org.sdmlib.modelspace;
import java.io.BufferedReader;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.management.RuntimeErrorException;
import org.sdmlib.replication.ChangeEvent;
@@ -53,7 +61,8 @@ else if (jsonObject.has(ChangeEvent.PROPERTY_OBJECTID))
private Socket clientSocket;
private BufferedReader in;
private String line;
- ModelCloudProxy proxy;
+ ModelCloudProxy proxy;
+ private InputStream inputStream;
public ClientSocketHandler(ModelCloud modelCloud, Socket clientSocket)
{
@@ -67,17 +76,112 @@ public void run()
// open clientSocket and read messages
try
{
- InputStream inputStream = clientSocket.getInputStream();
+ inputStream = clientSocket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
in = new BufferedReader(inputStreamReader);
line = in.readLine();
while (line != null)
{
- Platform.runLater(new HandleLineRunnable(line));
-
- System.out.println(line);
+ if (line.startsWith("{"))
+ {
+ try {
+
+ final JsonObject jsonObject = new JsonObject();
+ jsonObject.withValue(line);
+
+ if (jsonObject.has("msgtype"))
+ {
+ String msgtype = jsonObject.getString("msgtype");
+ if (msgtype.equals("fileTransfer"))
+ {
+ // read the file content
+ int fileSize = jsonObject.getInt("fileSize");
+ final byte[] content = new byte[fileSize];
+
+ int readSize = 0;
+
+ while (readSize < fileSize)
+ {
+ int blockSize = inputStream.read(content, readSize, fileSize-readSize);
+
+ if (blockSize == -1)
+ {
+ // something did not work.
+ throw new RuntimeException("Receiving file data problem occured");
+ }
+ readSize += blockSize;
+ }
+
+ System.out.println("Received " + jsonObject.getString("fileName") + " " + readSize + " of " + fileSize + " bytes.");
+
+ if (readSize == fileSize)
+ {
+ Platform.runLater(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ String fileName = modelCloud.getLocation() + "/" + jsonObject.getString("fileName");
+ long lastModified = (long) jsonObject.get("lastModified");
+ // got it, write the file
+ try
+ {
+ Path path = Paths.get(fileName);
+
+ if (Files.exists(path))
+ {
+ // mv old version to .fileData
+ Path targetPath = path.getParent();
+ String targetFileDir = targetPath.toString();
+ Path plainNamePath = path.getName(path.getNameCount()-1);
+ String plainName = plainNamePath.toString();
+
+ Date date = new Date(lastModified);
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat(".yyyy.MM.dd_HH.mm.ss");
+ String dateString = simpleDateFormat.format(date);
+
+ String fullTargetFileName = targetFileDir + "/.fileData/" + plainName + dateString;
+
+ targetPath = Paths.get(fullTargetFileName);
+
+ Files.move(path, targetPath);
+ }
+
+ // write new file
+ Files.write(path, content);
+ new File(fileName).setLastModified(lastModified);
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+ }
+ else
+ {
+ Platform.runLater(new HandleLineRunnable(line));
+ }
+ }
+ else
+ {
+ Platform.runLater(new HandleLineRunnable(line));
+ }
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+ else
+ {
+ // only json messages are accepted
+ System.out.println("only json messages are accepted\n" + line);
+ }
line = in.readLine();
}
@@ -21,19 +21,20 @@ and associated documentation files (the "Software"), to deal in the Software wit
package org.sdmlib.modelspace;
-import org.sdmlib.serialization.PropertyChangeInterface;
-
-import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
+import java.beans.PropertyChangeSupport;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import org.sdmlib.StrUtil;
import org.sdmlib.modelspace.util.ModelCloudProxySet;
import org.sdmlib.modelspace.util.ModelSpaceProxySet;
-
+import org.sdmlib.serialization.PropertyChangeInterface;
import de.uniks.networkparser.json.JsonObject;
-import org.sdmlib.StrUtil;
-
public class ModelCloud implements PropertyChangeInterface
{
@@ -176,7 +177,7 @@ public ModelCloudProxy createServers()
return value;
}
- public ModelCloudProxy getProxy(String hostName, int portNo)
+ public ModelCloudProxy getProxy(String hostName, long portNo)
{
for (ModelCloudProxy proxy : this.getServers())
{
@@ -312,7 +313,7 @@ public void subscribeForModelSpace(String modelSpaceName)
JsonObject jsonObject = new JsonObject();
jsonObject.withKeyValue("msgtype", "subscribe")
- .withKeyValue("hostName", "localhost")
+ .withKeyValue("hostName", this.getHostName())
.withKeyValue("portNo", this.getAcceptPort())
.withKeyValue("modelSpaceName", modelSpaceName);
@@ -356,6 +357,18 @@ public void setMyTaskLane(TaskLane myTaskLane)
}
+ private String location;
+
+ public void setLocation(String location)
+ {
+ this.location = location;
+ }
+
+ public String getLocation()
+ {
+ return location;
+ }
+
//==========================================================================
@@ -368,7 +381,6 @@ public String getHostName()
return this.hostName;
}
-
public void setHostName(String value)
{
if ( ! StrUtil.stringEquals(this.hostName, value)) {
@@ -378,7 +390,6 @@ public void setHostName(String value)
getPropertyChangeSupport().firePropertyChange(PROPERTY_HOSTNAME, oldValue, value);
}
}
-
public ModelCloud withHostName(String value)
{
@@ -431,6 +442,29 @@ public void handleTask(Task task)
}
}
}
+ else if (task.getState().equals("sendFileToTarget") && task.getLane() == myTaskLane)
+ {
+ // I shall send the file to my partner
+ TaskLane targetCloudLane = task.getFileTargetCloud();
+ ModelCloudProxy targetCloudProxy = this.getProxy(targetCloudLane.getHostName(), targetCloudLane.getPortNo());
+ if (targetCloudProxy.getState().equals("online"))
+ {
+ ModelCloudChannel channel = targetCloudProxy.getChannel();
+
+ // open file
+ Path path = Paths.get(this.location + "/" + task.getSpaceName() + "/" + task.getFileName());
+ try
+ {
+ byte[] allBytes = Files.readAllBytes(path);
+ channel.sendFile(task.getSpaceName() + "/" + task.getFileName(), task.getLastModified(), allBytes);
+ task.setState("prepareToReceive");
+ }
+ catch (IOException e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
}
-
}
@@ -80,6 +80,7 @@ public void start(Stage stage) throws Exception
space = new ModelSpace(idMap, userName, ApplicationType.JavaFX).open(location + "/.cloudData");
+ modelCloud.setLocation(location);
InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
@@ -97,12 +98,15 @@ public void start(Stage stage) throws Exception
HBox hBox = new HBox(8);
+ hBox.setPadding(new Insets(0, 0, 0, 24));
hBox.getChildren().addAll(label, acceptPortField);
Label otherServersLabel = new Label ("Other cloud servers:");
- VBox otherSeversVBox = new VBox(8);
- FX.bindListProperty(idMap, otherSeversVBox, modelCloud, ModelCloud.PROPERTY_SERVERS, ModelCloudProxyController.class);
+ VBox otherServersVBox = new VBox(8);
+ otherServersVBox.setPadding(new Insets(0, 0, 0, 24));
+
+ FX.bindListProperty(idMap, otherServersVBox, modelCloud, ModelCloud.PROPERTY_SERVERS, ModelCloudProxyController.class);
hostNameField = new TextField("hostName?");
portNoField = new TextField("portNo?");
@@ -123,6 +127,7 @@ public void handle(ActionEvent arg0)
});
addServerBox = new HBox(8);
+ addServerBox.setPadding(new Insets(0, 0, 0, 24));
addServerBox.getChildren().addAll(hostNameField, portNoField, button);
// add listener for model space proxies
@@ -145,7 +150,7 @@ public void handle(ActionEvent arg0)
taskIdMap.put("taskBoard", taskBoard);
- taskSpace = new ModelSpace(taskIdMap, userName, ApplicationType.JavaFX)
+ taskSpace = new ModelSpace(taskIdMap, laneName, ApplicationType.JavaFX)
.open(location + "/.cloudTasks");
myTaskLane = taskBoard.getOrCreateLane(modelCloud.getHostName(), modelCloud.getAcceptPort());
@@ -162,13 +167,13 @@ public void handle(ActionEvent arg0)
root = new VBox(8);
root.setPadding(new Insets(24));
- root.getChildren().addAll(hostNameLabel, hBox, otherServersLabel, otherSeversVBox, addServerBox, modelSpaceProxiesVbox, tasksVBox);
+ root.getChildren().addAll(hostNameLabel, hBox, otherServersLabel, otherServersVBox, addServerBox, modelSpaceProxiesVbox, tasksVBox);
ScrollPane scrollPane = new ScrollPane(root);
scrollPane.setStyle("-fx-background: white;");
- Scene scene = new Scene(scrollPane, 400, 600);
+ Scene scene = new Scene(scrollPane, 450, 600);
stage.setScene(scene);
Oops, something went wrong.

0 comments on commit bfeae6d

Please sign in to comment.