Skip to content

Commit

Permalink
Further refactoring of messages to be entirely file-backed
Browse files Browse the repository at this point in the history
  • Loading branch information
blanu committed Oct 20, 2011
1 parent b387f6e commit 1702f27
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 99 deletions.
15 changes: 11 additions & 4 deletions android/src/net/blanu/sneakermesh/LANProbeService.java
Expand Up @@ -19,18 +19,19 @@
public class LANProbeService extends Service implements Logger public class LANProbeService extends Service implements Logger
{ {
private static final String TAG = "LANProbe"; private static final String TAG = "LANProbe";
Sneakermesh mesh=null; static Sneakermesh mesh=null;
LANProbe probe=null; static LANProbe probe=null;


public String BROADCAST_ACTION; static public String BROADCAST_ACTION;
Intent intent; static Intent intent;
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();


@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
BROADCAST_ACTION=this.getPackageName()+".log"; BROADCAST_ACTION=this.getPackageName()+".log";
intent = new Intent(BROADCAST_ACTION); intent = new Intent(BROADCAST_ACTION);
log("LANProbeService.onCreate");
} }


@Override @Override
Expand All @@ -39,6 +40,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {


if(mesh==null) if(mesh==null)
{ {
log("Fresh mesh");
if(checkStorage()) if(checkStorage())
{ {
log("New mesh"); log("New mesh");
Expand All @@ -59,6 +61,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
probe.start(); probe.start();
} }
} }
else
{
log("singleton!");
}


return START_STICKY; return START_STICKY;
} }
Expand All @@ -82,6 +88,7 @@ public IBinder onBind(Intent intent) {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
log("LANProbeService.onDestroy");
} }


public void log(String s) public void log(String s)
Expand Down
2 changes: 1 addition & 1 deletion android/src/net/blanu/sneakermesh/SneakermeshActivity.java
Expand Up @@ -28,14 +28,14 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);


serviceIntent=new Intent(this, LANProbeService.class); serviceIntent=new Intent(this, LANProbeService.class);
startService(serviceIntent);
} }


@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// The activity has become visible (it is now "resumed"). // The activity has become visible (it is now "resumed").


startService(serviceIntent);
doBindService(); doBindService();
} }


Expand Down
Expand Up @@ -45,14 +45,14 @@ public void onCreate(Bundle savedInstanceState) {
REFRESH_ACTION=this.getPackageName()+".refresh"; REFRESH_ACTION=this.getPackageName()+".refresh";


serviceIntent=new Intent(this, LANProbeService.class); serviceIntent=new Intent(this, LANProbeService.class);
startService(serviceIntent);
} }


@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// The activity has become visible (it is now "resumed"). // The activity has become visible (it is now "resumed").


startService(serviceIntent);
registerReceiver(broadcastReceiver, new IntentFilter(REFRESH_ACTION)); registerReceiver(broadcastReceiver, new IntentFilter(REFRESH_ACTION));
doBindService(); doBindService();
} }
Expand Down
92 changes: 37 additions & 55 deletions shared/src/net/blanu/sneakermesh/Message.java
Expand Up @@ -5,10 +5,12 @@
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;


public abstract class Message implements Comparable<Message> public abstract class Message implements Comparable<Message>
{ {
Expand All @@ -18,6 +20,8 @@ public abstract class Message implements Comparable<Message>
public int type; public int type;
public long timestamp; public long timestamp;
public int size; public int size;
public File file;
public String digest;


public int compareTo(Message m) public int compareTo(Message m)
{ {
Expand All @@ -32,13 +36,15 @@ public static Message readMessage(DataInputStream is) throws IOException
System.out.println("timestamp: "+ts); System.out.println("timestamp: "+ts);
int num=is.readInt(); int num=is.readInt();
System.out.println("num: "+num); System.out.println("num: "+num);

switch(msgType) switch(msgType)
{ {
case -1: case -1:
return null; return null;
case MSG_TEXT: case MSG_TEXT:
return new TextMessage(ts, num, is); return new TextMessage(ts, num, is);
case MSG_PHOTO:
return new PhotoMessage(ts, num, is);
default: default:
System.out.println("Unknown message: "+msgType); System.out.println("Unknown message: "+msgType);
return null; return null;
Expand All @@ -50,23 +56,45 @@ static public Message readMessage(File f) throws IOException
long ts=f.lastModified(); long ts=f.lastModified();
System.out.println("timestamp: "+ts); System.out.println("timestamp: "+ts);
int num=(int)f.length(); int num=(int)f.length();
InputStream is=new FileInputStream(f);


if(f.getAbsolutePath().contains("Pictures")) if(f.getAbsolutePath().contains("Pictures"))
{ {
return new PhotoMessage(ts, num, is); return new PhotoMessage(ts, num, f);
} }
else 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; type=t;
timestamp=ts; timestamp=ts;
size=num; 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 public void write(DataOutputStream out) throws IOException
Expand All @@ -77,60 +105,14 @@ public void write(DataOutputStream out) throws IOException
writeData(out); writeData(out);
} }


public void write(File f) throws IOException public void writeData(OutputStream out) throws FileNotFoundException
{ {
OutputStream out=new FileOutputStream(f); InputStream in=new FileInputStream(file);
writeData(out); Util.pump(in, out, size);
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;
}
} }

public String toString() public String toString()
{ {
return "[Message: "+timestamp+"]"; return "[Message: "+timestamp+"]";
} }

abstract public void writeData(OutputStream out) throws IOException;
} }
21 changes: 5 additions & 16 deletions shared/src/net/blanu/sneakermesh/PhotoMessage.java
Expand Up @@ -15,27 +15,16 @@ public class PhotoMessage extends Message
{ {
public String path; 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()); super(MSG_PHOTO, ts, num, is);
path=s;
}

public PhotoMessage(String s)
{
this(new Date().getTime(), s);
} }

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() public String toString()
{ {
return "[Photo: "+path+"]"; return "[Photo: "+path+"]";
Expand Down
11 changes: 3 additions & 8 deletions shared/src/net/blanu/sneakermesh/Sneakermesh.java
Expand Up @@ -252,8 +252,7 @@ private void execute(WantCommand msg)
private void execute(GiveCommand cmd) throws IOException private void execute(GiveCommand cmd) throws IOException
{ {
log("executing give: "+cmd); log("executing give: "+cmd);
File file=new File(root, cmd.digest); cmd.msg.save(root);
cmd.msg.write(file);


synchronized(want) synchronized(want)
{ {
Expand Down Expand Up @@ -282,12 +281,8 @@ public void addTextMessage(String msg) throws IOException


public void addMessage(Message msg) throws IOException public void addMessage(Message msg) throws IOException
{ {
String digest=msg.getDigest(); String digest=msg.digest;
File file=new File(root, digest); msg.save(root);
FileOutputStream out=new FileOutputStream(file);
DataOutputStream dout=new DataOutputStream(out);
msg.write(dout);
out.close();


synchronized(have) synchronized(have)
{ {
Expand Down
41 changes: 28 additions & 13 deletions shared/src/net/blanu/sneakermesh/TextMessage.java
@@ -1,5 +1,6 @@
package net.blanu.sneakermesh; package net.blanu.sneakermesh;


import java.io.ByteArrayInputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
Expand All @@ -17,29 +18,43 @@ public class TextMessage extends Message
{ {
public String text; 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()); super(MSG_TEXT, ts, num, is);
text=s;
}

public TextMessage(String s)
{
this(new Date().getTime(), s);
} }


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() public String toString()
{ {
return "[Text: "+text+"]"; return "[Text: "+file.length()+"]";
} }
} }

0 comments on commit 1702f27

Please sign in to comment.