Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Further refactoring of messages to be entirely file-backed

  • Loading branch information...
commit 1702f2714f79834453f26a1406ea938ce5dca20a 1 parent b387f6e
@blanu authored
View
15 android/src/net/blanu/sneakermesh/LANProbeService.java
@@ -19,11 +19,11 @@
public class LANProbeService extends Service implements Logger
{
private static final String TAG = "LANProbe";
- Sneakermesh mesh=null;
- LANProbe probe=null;
+ static Sneakermesh mesh=null;
+ static LANProbe probe=null;
- public String BROADCAST_ACTION;
- Intent intent;
+ static public String BROADCAST_ACTION;
+ static Intent intent;
private final IBinder mBinder = new LocalBinder();
@Override
@@ -31,6 +31,7 @@ public void onCreate() {
super.onCreate();
BROADCAST_ACTION=this.getPackageName()+".log";
intent = new Intent(BROADCAST_ACTION);
+ log("LANProbeService.onCreate");
}
@Override
@@ -39,6 +40,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if(mesh==null)
{
+ log("Fresh mesh");
if(checkStorage())
{
log("New mesh");
@@ -59,6 +61,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
probe.start();
}
}
+ else
+ {
+ log("singleton!");
+ }
return START_STICKY;
}
@@ -82,6 +88,7 @@ public IBinder onBind(Intent intent) {
@Override
public void onDestroy() {
super.onDestroy();
+ log("LANProbeService.onDestroy");
}
public void log(String s)
View
2  android/src/net/blanu/sneakermesh/SneakermeshActivity.java
@@ -28,6 +28,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
serviceIntent=new Intent(this, LANProbeService.class);
+ startService(serviceIntent);
}
@Override
@@ -35,7 +36,6 @@ protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
- startService(serviceIntent);
doBindService();
}
View
2  android/src/net/blanu/sneakermesh/SneakermeshListActivity.java
@@ -45,6 +45,7 @@ public void onCreate(Bundle savedInstanceState) {
REFRESH_ACTION=this.getPackageName()+".refresh";
serviceIntent=new Intent(this, LANProbeService.class);
+ startService(serviceIntent);
}
@Override
@@ -52,7 +53,6 @@ protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
- startService(serviceIntent);
registerReceiver(broadcastReceiver, new IntentFilter(REFRESH_ACTION));
doBindService();
}
View
92 shared/src/net/blanu/sneakermesh/Message.java
@@ -5,10 +5,12 @@
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.security.NoSuchAlgorithmException;
public abstract class Message implements Comparable<Message>
{
@@ -18,6 +20,8 @@
public int type;
public long timestamp;
public int size;
+ public File file;
+ public String digest;
public int compareTo(Message m)
{
@@ -32,13 +36,15 @@ public static Message readMessage(DataInputStream is) throws IOException
System.out.println("timestamp: "+ts);
int num=is.readInt();
System.out.println("num: "+num);
-
+
switch(msgType)
{
case -1:
return null;
case MSG_TEXT:
return new TextMessage(ts, num, is);
+ case MSG_PHOTO:
+ return new PhotoMessage(ts, num, is);
default:
System.out.println("Unknown message: "+msgType);
return null;
@@ -50,23 +56,45 @@ static public Message readMessage(File f) throws IOException
long ts=f.lastModified();
System.out.println("timestamp: "+ts);
int num=(int)f.length();
- InputStream is=new FileInputStream(f);
if(f.getAbsolutePath().contains("Pictures"))
{
- return new PhotoMessage(ts, num, is);
+ return new PhotoMessage(ts, num, f);
}
else
{
- return new TextMessage(ts, num, is);
+ return new TextMessage(ts, num, f);
}
}
- public Message(int t, long ts, int num)
+ public Message(int t, long ts, int num, InputStream is) throws IOException
+ {
+ type=t;
+ timestamp=ts;
+ size=num;
+
+ File file=File.createTempFile("sneakermesh", "tmp");
+ FileOutputStream out=new FileOutputStream(file);
+ digest=Util.pump(is, out, num);
+ out.close();
+ file.setLastModified(timestamp);
+ }
+
+ public Message(int t, long ts, int num, File f) throws IOException
{
type=t;
timestamp=ts;
size=num;
+ file=f;
+ digest=Util.hash(file);
+ }
+
+ public void save(File destDir)
+ {
+ File dest=new File(destDir, digest);
+ file.renameTo(dest);
+ dest.setLastModified(timestamp);
+ file=dest;
}
public void write(DataOutputStream out) throws IOException
@@ -77,60 +105,14 @@ public void write(DataOutputStream out) throws IOException
writeData(out);
}
- public void write(File f) throws IOException
+ public void writeData(OutputStream out) throws FileNotFoundException
{
- OutputStream out=new FileOutputStream(f);
- writeData(out);
- out.close();
- f.setLastModified(timestamp);
- }
-
- static protected String readDigest(InputStream is)
- {
- byte[] digest=fillBuffer(is, (512/8)*2);
- return new String(digest);
- }
-
- static protected byte[] fillBuffer(InputStream is, int size)
- {
- byte[] digest=new byte[size];
- int offset=0;
- int count=0;
- while(count<digest.length)
- {
- int read;
- try {
- read = is.read(digest, offset, digest.length-offset);
- offset=offset+read;
- count=count+read;
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return digest;
- }
-
- public String getDigest()
- {
- try {
- ByteArrayOutputStream out=new ByteArrayOutputStream();
- DataOutputStream dout=new DataOutputStream(out);
- write(dout);
- dout.close();
-
- byte[] digest=Skein.hash(out.toByteArray());
- return Util.asHex(digest);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
+ InputStream in=new FileInputStream(file);
+ Util.pump(in, out, size);
}
-
+
public String toString()
{
return "[Message: "+timestamp+"]";
}
-
- abstract public void writeData(OutputStream out) throws IOException;
}
View
21 shared/src/net/blanu/sneakermesh/PhotoMessage.java
@@ -15,27 +15,16 @@
{
public String path;
- public PhotoMessage(long ts, String s)
+ public PhotoMessage(long ts, int num, InputStream is) throws IOException
{
- super(MSG_PHOTO, ts, s.length());
- path=s;
- }
-
- public PhotoMessage(String s)
- {
- this(new Date().getTime(), s);
+ super(MSG_PHOTO, ts, num, is);
}
-
- public PhotoMessage(long ts, int num, InputStream is)
+
+ public PhotoMessage(long ts, int num, File f) throws IOException
{
- this(ts, new String(Util.fillBuffer(is, num)));
+ super(MSG_PHOTO, ts, num, f);
}
- public void writeData(OutputStream out) throws IOException
- {
- out.write(path.getBytes());
- }
-
public String toString()
{
return "[Photo: "+path+"]";
View
11 shared/src/net/blanu/sneakermesh/Sneakermesh.java
@@ -252,8 +252,7 @@ private void execute(WantCommand msg)
private void execute(GiveCommand cmd) throws IOException
{
log("executing give: "+cmd);
- File file=new File(root, cmd.digest);
- cmd.msg.write(file);
+ cmd.msg.save(root);
synchronized(want)
{
@@ -282,12 +281,8 @@ public void addTextMessage(String msg) throws IOException
public void addMessage(Message msg) throws IOException
{
- String digest=msg.getDigest();
- File file=new File(root, digest);
- FileOutputStream out=new FileOutputStream(file);
- DataOutputStream dout=new DataOutputStream(out);
- msg.write(dout);
- out.close();
+ String digest=msg.digest;
+ msg.save(root);
synchronized(have)
{
View
41 shared/src/net/blanu/sneakermesh/TextMessage.java
@@ -1,5 +1,6 @@
package net.blanu.sneakermesh;
+import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
@@ -17,29 +18,43 @@
{
public String text;
- public TextMessage(long ts, String s)
+ public TextMessage(long ts, int num, InputStream is) throws IOException
{
- super(MSG_TEXT, ts, s.length());
- text=s;
- }
-
- public TextMessage(String s)
- {
- this(new Date().getTime(), s);
+ super(MSG_TEXT, ts, num, is);
}
- public TextMessage(long ts, int num, InputStream is)
+ public TextMessage(long ts, int num, File f) throws IOException
{
- this(ts, new String(Util.fillBuffer(is, num)));
+ super(MSG_TEXT, ts, num, f);
}
- public void writeData(OutputStream out) throws IOException
+ public TextMessage(long ts, String s) throws IOException
{
- out.write(text.getBytes());
+ super(MSG_TEXT, ts, s.length(), new ByteArrayInputStream(s.getBytes()));
}
+ public TextMessage(String s) throws IOException
+ {
+ this(new Date().getTime(), s);
+ }
+
+ public String getText()
+ {
+ try
+ {
+ FileInputStream in=new FileInputStream(file);
+ byte[] buff=Util.fillBuffer(in, size);
+ return new String(buff);
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
public String toString()
{
- return "[Text: "+text+"]";
+ return "[Text: "+file.length()+"]";
}
}
View
66 shared/src/net/blanu/sneakermesh/Util.java
@@ -8,6 +8,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -48,7 +49,7 @@ static protected String readDigest(InputStream is)
return digest;
}
- public String hash(File f) throws NoSuchAlgorithmException, IOException
+ public static String hash(File f) throws IOException
{
try
{
@@ -70,6 +71,69 @@ public String hash(File f) throws NoSuchAlgorithmException, IOException
return null;
}
}
+
+ public static String pump(InputStream in, OutputStream out, long maxlen)
+ {
+ int buffsize=1024;
+ byte[] buff=new byte[buffsize];
+ int count=0;
+
+ MessageDigest sha1=null;
+ try
+ {
+ sha1 = MessageDigest.getInstance("SHA1");
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+
+ BufferedInputStream bis = new BufferedInputStream(in);
+ DigestInputStream is = new DigestInputStream(bis, sha1);
+
+ try {
+ int toread;
+ if(count+buffsize>maxlen)
+ {
+ toread=(int)(buffsize-((count+buffsize)-maxlen));
+ }
+ else
+ {
+ toread=buffsize;
+ }
+
+ int read=is.read(buff, 0, toread);
+ while(read!=-1 && count<maxlen)
+ {
+ out.write(buff, 0, read);
+ count=count+read;
+ if(count+buffsize>maxlen)
+ {
+ toread=(int)(buffsize-((count+buffsize)-maxlen));
+ }
+ else
+ {
+ toread=buffsize;
+ }
+ read=is.read(buff, 0, toread);
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ System.out.println("pump read "+count+" of "+maxlen);
+
+ if(sha1==null)
+ {
+ return null;
+ }
+ else
+ {
+ byte[] hash = sha1.digest();
+ return asHex(hash);
+ }
+ }
static String asHex(byte[] hash) {
Formatter formatter = new Formatter();
Please sign in to comment.
Something went wrong with that request. Please try again.