Permalink
Browse files

Modified when file size is sent

  • Loading branch information...
jonaias committed Nov 22, 2011
1 parent 5088233 commit 15ccf05f0cbbaefd14e508f8ebe08461765a40bc
View
@@ -0,0 +1 @@
+netcat -i 1 -l -p 9764 -u | netcat -v -u 192.168.0.116 9764
@@ -0,0 +1,2 @@
+rlwrap netcat localhost 23753
+numerorandom /teste.txt
View
@@ -0,0 +1,3 @@
+rlwrap netcat -u localhost 9226
+{"messageType":1, "name":"James Stewart", "deviceID": "numerorandom"}
+{"messageType":6, "path":"/teste.txt", "deviceID": "numerorandom"}
View
@@ -1,3 +1,3 @@
#
#
-echo '{"messageType":4,"sharedList":[{"permissions":"r","path":"\/teste.txt"}],"deviceID":"numerorandom"}' | netcat -vv -c -u localhost 9764
+echo '{"messageType":4,"sharedList":[{"permissions":"r","path":"\/teste.txt", "size":22}],"deviceID":"numerorandom"}' | netcat -vv -c -u localhost 9764
@@ -8,80 +8,101 @@
import java.io.OutputStream;
import java.net.Socket;
+import android.os.Environment;
import android.util.Log;
public class FileClient extends Thread {
private String mDeviceID;
private String mPath;
+ private String mDestinationFolder;
private String mDestination;
private Socket mSocket;
private int mSize;
private Double mCurrentProgress;
-
- public FileClient(String deviceID, String path, String destination,
- int size, Socket socket) {
- mDeviceID = deviceID;
- mPath = path;
- mSize = size;
- mSocket = socket;
- mDestination = destination;
-
+
+ //private Person mPerson;
+
+ public FileClient(Person person, SharedWithMeItem sharedWithMeItem) {
+ mSocket = null;
+
+ mPath = sharedWithMeItem.getSharedPath();
+ mDestinationFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(); // TODO: Pegar o destino.
+ mDestination = mDestinationFolder+mPath;
+
+ mSize = (int)sharedWithMeItem.getFileSize();
+ mDeviceID = person.getDeviceID();
+
+ try {
+ mSocket = new Socket(person.getIP(),
+ NetworkProtocol.FILE_PORT);
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return;
+ }
+
Log.i("FileClient", "Created transfer with path \"" + mPath
+ "\" on socket \"" + mSocket + "\" to destination \""
- + mDestination + "\".");
+ + mDestinationFolder + "\".");
}
@Override
public void run() {
mCurrentProgress = 0.0;
- try {
-
- OutputStream sout = mSocket.getOutputStream();
- String s = mDeviceID + " " + mPath + "\n";
- sout.write(s.getBytes(), 0, s.getBytes().length);
-
- int BLOCK_SIZE = NetworkProtocol.BLOCK_SIZE;
-
- File file = new File(mDestination);
- FileOutputStream fos = new FileOutputStream(file);
- BufferedOutputStream out = new BufferedOutputStream(fos);
-
- InputStream in = mSocket.getInputStream();
- int nPackets = (int) Math.ceil(((double) mSize)
- / ((double) BLOCK_SIZE));
-
- int bytesReceived;
- byte[] received = new byte[mSize];
-
- Log.i("FileClient", "Started transfer on socket \"" + mSocket
- + "\".");
-
- for (int i = 0; i < nPackets; i++) {
- int sizeToReceive = (mSize - i * BLOCK_SIZE >= BLOCK_SIZE) ? BLOCK_SIZE
- : mSize - i * BLOCK_SIZE;
-
- bytesReceived = in
- .read(received, i * BLOCK_SIZE, sizeToReceive);
+ if(mSocket != null){
+ try {
- synchronized (mCurrentProgress) {
- mCurrentProgress += ((double) bytesReceived) / ((double) mSize);
+ OutputStream sout = mSocket.getOutputStream();
+ String s = mDeviceID + " " + mPath + "\n";
+ sout.write(s.getBytes(), 0, s.getBytes().length);
+
+ int BLOCK_SIZE = NetworkProtocol.BLOCK_SIZE;
+
+ File file = new File(mDestination);
+ FileOutputStream fos = new FileOutputStream(file);
+ BufferedOutputStream out = new BufferedOutputStream(fos);
+
+ InputStream in = mSocket.getInputStream();
+ int nPackets = (int) Math.ceil(((double) mSize)
+ / ((double) BLOCK_SIZE));
+
+ int bytesReceived;
+ byte[] received = new byte[mSize];
+
+ Log.i("FileClient", "Started transfer on socket \"" + mSocket
+ + "\".");
+
+ for (int i = 0; i < nPackets; i++) {
+ int sizeToReceive = (mSize - i * BLOCK_SIZE >= BLOCK_SIZE) ? BLOCK_SIZE
+ : mSize - i * BLOCK_SIZE;
+
+ bytesReceived = in
+ .read(received, i * BLOCK_SIZE, sizeToReceive);
+
+ synchronized (mCurrentProgress) {
+ mCurrentProgress += ((double) bytesReceived) / ((double) mSize);
+ }
}
+
+ out.write(received, 0, mSize);
+ out.flush();
+
+ sout.close();
+ out.close();
+ in.close();
+ mSocket.close();
+
+ } catch (IOException e) {
+ e.printStackTrace();
}
-
- out.write(received, 0, mSize);
- out.flush();
-
- sout.close();
- out.close();
- in.close();
- mSocket.close();
-
- } catch (IOException e) {
- e.printStackTrace();
+
+ Log.i("FileClient", "Ended transfer on socket \"" + mSocket + "\".");
+ }
+ else{
+ Log.i("FileClient", "Cannot connect to \"" + mSocket + "\".");
}
-
- Log.i("FileClient", "Ended transfer on socket \"" + mSocket + "\".");
}
public String getPath () {
@@ -13,6 +13,7 @@
import org.json.JSONException;
import org.json.JSONObject;
+import android.os.Environment;
import android.util.Log;
public class NetworkListener extends Thread {
@@ -101,6 +102,7 @@ private void parseJSON(String json, DatagramPacket packet) {
JSONArray sharedList = obj.getJSONArray("sharedList");
String currPath, currPermissions;
+ long currSize;
Person person = NetworkManager.getInstance()
.getPersonByDeviceID(deviceID);
@@ -114,6 +116,8 @@ private void parseJSON(String json, DatagramPacket packet) {
.getString("path");
currPermissions = sharedList.getJSONObject(i)
.getString("permissions");
+ currSize = sharedList.getJSONObject(i)
+ .getLong("size");
boolean read = false, write = false;
@@ -123,7 +127,7 @@ private void parseJSON(String json, DatagramPacket packet) {
write = true;
person.addSharedWithMeItem(new SharedWithMeItem(
- currPath, read, write));
+ currPath, read, write,currSize));
}
}
}
@@ -161,27 +165,29 @@ private void parseJSON(String json, DatagramPacket packet) {
break;
case (NetworkProtocol.MESSAGE_DOWNLOAD_ACCEPT): {
Log.i("NetworkListener", "MESSAGE_DOWNLOAD_ACCEPT: " + obj);
-
- String path = obj.getString("path");
- String destination = null; // TODO: Pegar o destino.
- int size = 0; // TODO: Como pegar o tamanho??? Ele pode
- // mandar....
- size = Integer.valueOf(obj.getString("size"));
-
- Socket socket = null;
- try {
- socket = new Socket(packet.getAddress(),
- NetworkProtocol.FILE_PORT);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return;
+ SharedWithMeItem tempSharedWithMeItem = null;
+ String path;
+ Person tempPerson;
+
+ path = obj.getString("path");
+ tempPerson = NetworkManager.getInstance().getPersonByDeviceID(deviceID);
+
+ Iterator<SharedWithMeItem> sharedWithMeItemIterator = tempPerson.getSharedWithMeItems().iterator();
+ while(sharedWithMeItemIterator.hasNext()){
+ tempSharedWithMeItem = sharedWithMeItemIterator.next();
+ if (tempSharedWithMeItem.getSharedPath().equals(path)){
+ break;
+ }
+ }
+
+ if ((tempPerson!=null)&&(tempSharedWithMeItem!=null)){
+ FileClient client = new FileClient(tempPerson,tempSharedWithMeItem );
+ client.setName("FileClient:" + tempSharedWithMeItem.getSharedPath());
+ client.start();
+ }
+ else{
+ Log.i("NetworkListener", "Person or SharedWithMeItem is null, cannot create NetworkListener");
}
-
- FileClient client = new FileClient(deviceID, path, destination,
- size, socket);
- client.setName("FileClient:" + destination);
- client.start();
}
break;
case (NetworkProtocol.MESSAGE_DOWNLOAD_DENY):
@@ -43,13 +43,13 @@ private NetworkManager() {
// TODO Auto-generated catch block
e1.printStackTrace();
}
- SharedWithMeItem sharedWithMeItem1 = new SharedWithMeItem("/Documents/", true, false);
- SharedWithMeItem sharedWithMeItem2 = new SharedWithMeItem("/cool music.mp3", true, true);
- SharedWithMeItem sharedWithMeItem3 = new SharedWithMeItem("/picture.png", true, true);
- SharedWithMeItem sharedWithMeItem4 = new SharedWithMeItem("/Movies/", true, true);
- SharedWithMeItem sharedWithMeItem5 = new SharedWithMeItem("/Movies/alternative movie.mpg", true, true);
- SharedWithMeItem sharedWithMeItem6 = new SharedWithMeItem("/Movies/unknown.mpg", true, true);
- SharedWithMeItem sharedWithMeItem7 = new SharedWithMeItem("/Movies/Bad movie/unknown.mpg", true, true);
+ SharedWithMeItem sharedWithMeItem1 = new SharedWithMeItem("/Documents/", true, false,0);
+ SharedWithMeItem sharedWithMeItem2 = new SharedWithMeItem("/cool music.mp3", true, true,0);
+ SharedWithMeItem sharedWithMeItem3 = new SharedWithMeItem("/picture.png", true, true,0);
+ SharedWithMeItem sharedWithMeItem4 = new SharedWithMeItem("/Movies/", true, true,0);
+ SharedWithMeItem sharedWithMeItem5 = new SharedWithMeItem("/Movies/alternative movie.mpg", true, true,0);
+ SharedWithMeItem sharedWithMeItem6 = new SharedWithMeItem("/Movies/unknown.mpg", true, true,0);
+ SharedWithMeItem sharedWithMeItem7 = new SharedWithMeItem("/Movies/Bad movie/unknown.mpg", true, true,0);
teste2.addSharedWithMeItem(sharedWithMeItem1);
teste2.addSharedWithMeItem(sharedWithMeItem2);
teste2.addSharedWithMeItem(sharedWithMeItem3);
@@ -1,5 +1,6 @@
package org.AndroidShareApp.core;
+import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@@ -116,6 +117,8 @@ public void sendShareInfoForEverbody(){
sharedItemJSON.put("permissions", sharedPerson.getPermissionString());
+ sharedItemJSON.put("size", sharedByMeItem.getFileSize());
+
sharedListJSON.put(sharedItemJSON);
tempJsonObject.accumulate("sharedList",sharedListJSON);
@@ -89,7 +89,7 @@ public void deleteSharedWithMeItem(SharedWithMeItem item) {
Log.i("Person", this.mName+ ":testing "+buff);
if(buff.matches("[/]?[^/]*[/]?")){
Log.i("Person", this.mName+ ":added "+buff);
- SharedWithMeItem itemToAdd = new SharedWithMeItem(buff, tempItem.canRead(),tempItem.canWrite());
+ SharedWithMeItem itemToAdd = new SharedWithMeItem(buff, tempItem.canRead(),tempItem.canWrite(),tempItem.getFileSize());
tempSharedWithMeItems.add(itemToAdd);
}
}
@@ -9,6 +9,7 @@
private String mFullPath;
private ArrayList<SharedPerson> mPerson;
private boolean mIsActive;
+ private long mFileSize;
public SharedByMeItem(String fullPath, SharedPerson person) {
mFullPath = fullPath;
@@ -17,13 +18,15 @@ public SharedByMeItem(String fullPath, SharedPerson person) {
/* TODO: Pegar o sharedPath a partir do fullPath. */
mSharedPath = getSharedPath(fullPath);
mIsActive = false;
+ mFileSize = (new File(mFullPath)).length();
}
public SharedByMeItem(String fullPath) {
mFullPath = fullPath;
/* TODO: Pegar o sharedPath a partir do fullPath. */
mSharedPath = getSharedPath(fullPath);
mPerson = new ArrayList<SharedPerson>();
+ mFileSize = (new File(mFullPath)).length();
}
private static String getSharedPath(String fullPath) {
@@ -78,4 +81,8 @@ public void managePerson(SharedPerson person) {
mPerson.add(person);
}
}
+
+ public long getFileSize(){
+ return mFileSize;
+ }
}
@@ -5,11 +5,13 @@
private String mSharedPath;
private boolean mRead;
private boolean mWrite;
+ private long mSize;
- public SharedWithMeItem (String sharedPath, boolean read, boolean write) {
+ public SharedWithMeItem (String sharedPath, boolean read, boolean write, long size) {
mSharedPath = sharedPath;
mRead = read;
mWrite = write;
+ mSize = size;
}
public String getSharedPath () {
@@ -37,6 +39,10 @@ public String typeString(){
}
}
+ public long getFileSize(){
+ return mSize;
+ }
+
@Override
public int compareTo(SharedWithMeItem other) {
return this.mSharedPath.compareTo(other.mSharedPath);

0 comments on commit 15ccf05

Please sign in to comment.