Skip to content

Commit 4b1506e

Browse files
thscthsc
authored andcommitted
working on first version that really works in Android
1 parent a3ffcf4 commit 4b1506e

15 files changed

+237
-87
lines changed
Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,31 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
16
package net.sharksystem.asp3;
27

38
import java.io.IOException;
49
import java.util.Iterator;
5-
import java.util.List;
610

711
/**
8-
* A chunk represents a set of messages addressed by an URI.
9-
*
10-
*
12+
*
1113
* @author thsc
12-
* @see ASPChunkStorage for more details
1314
*/
1415
public interface ASP3Chunk {
15-
1616
/**
1717
*
1818
* @return number of message in that chunk
1919
*/
2020
int getNumberMessage();
21-
22-
/**
23-
*
24-
* @return recipients of that chunk
25-
*/
26-
List<CharSequence> getRecipients();
27-
28-
/**
29-
* add recipients
30-
* @param recipient
31-
* @throws IOException
32-
*/
33-
void addRecipient(CharSequence recipient) throws IOException;
34-
35-
/**
36-
* set a list of recipients. Former recipients are dikscarded
37-
* @param recipients
38-
* @throws IOException
39-
*/
40-
void setRecipients(List<CharSequence> recipients) throws IOException;
41-
42-
/**
43-
* recipient is removed
44-
* @param recipients
45-
* @throws IOException
46-
*/
47-
void removeRecipient(CharSequence recipients) throws IOException;
48-
21+
4922
/**
5023
* URI which represents topic of messages in that chunk
5124
* @return
5225
* @throws IOException
5326
*/
5427
String getUri() throws IOException;
5528

56-
/**
57-
* adds a message
58-
* @param message
59-
* @throws IOException
60-
*/
61-
void add(CharSequence message) throws IOException;
62-
6329
/**
6430
*
6531
* @return iterator of all messages in the chunk
@@ -73,4 +39,6 @@ public interface ASP3Chunk {
7339
* have an undefined behaviour.
7440
*/
7541
public void drop();
42+
43+
public int getEra() throws IOException;
7644
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.sharksystem.asp3;
2+
3+
import java.io.IOException;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
/**
8+
* A chunk represents a set of messages addressed by an URI.
9+
*
10+
*
11+
* @author thsc
12+
* @see ASPChunkStorage for more details
13+
*/
14+
public interface ASP3Chunk2Send extends ASP3Chunk {
15+
/**
16+
*
17+
* @return recipients of that chunk
18+
*/
19+
List<CharSequence> getRecipients();
20+
21+
/**
22+
* add recipients
23+
* @param recipient
24+
* @throws IOException
25+
*/
26+
void addRecipient(CharSequence recipient) throws IOException;
27+
28+
/**
29+
* set a list of recipients. Former recipients are dikscarded
30+
* @param recipients
31+
* @throws IOException
32+
*/
33+
void setRecipients(List<CharSequence> recipients) throws IOException;
34+
35+
/**
36+
* recipient is removed
37+
* @param recipients
38+
* @throws IOException
39+
*/
40+
void removeRecipient(CharSequence recipients) throws IOException;
41+
42+
/**
43+
* adds a message
44+
* @param message
45+
* @throws IOException
46+
*/
47+
void add(CharSequence message) throws IOException;
48+
}

src/net/sharksystem/asp3/ASP3ChunkFS.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author thsc
2020
*/
21-
class ASP3ChunkFS implements ASP3Chunk {
21+
class ASP3ChunkFS implements ASP3Chunk2Send, ASP3ChunkReceived {
2222
public static final String META_DATA_EXTENSION = "meta";
2323
public static final String DATA_EXTENSION = "content";
2424
public static final String DEFAULT_URL = "content://sharksystem.net/noContext";
@@ -29,11 +29,19 @@ class ASP3ChunkFS implements ASP3Chunk {
2929
private File messageFile;
3030

3131
private int numberMessage = 0;
32+
private int era;
33+
private String sender;
3234

3335

3436
ASP3ChunkFS(ASP3StorageFS storage, String targetUri, int era) throws IOException {
37+
this(storage, targetUri, era, null);
38+
}
39+
40+
ASP3ChunkFS(ASP3StorageFS storage, String targetUri, int era, String sender) throws IOException {
3541
this.storage = storage;
3642
this.uri = targetUri;
43+
this.era = era;
44+
this.sender = sender;
3745

3846
String trunkName = this.storage.getFullFileName(era, targetUri);
3947

@@ -194,6 +202,21 @@ public int getNumberMessage() {
194202
return this.numberMessage;
195203
}
196204

205+
@Override
206+
public int getEra() throws IOException {
207+
return this.era;
208+
}
209+
210+
@Override
211+
public CharSequence getSender() throws IOException {
212+
return this.sender;
213+
}
214+
215+
@Override
216+
public void addReceivedMessage(CharSequence message) throws IOException {
217+
this.add(message);
218+
}
219+
197220
private class MessageIter implements Iterator {
198221

199222
private final DataInputStream dis;

src/net/sharksystem/asp3/ASP3ChunkReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public void run() {
3636
b.append("start reading ");
3737
System.out.println(b.toString());
3838
//>>>>>>>>>>>>>>>>>>>debug
39-
39+
40+
this.reader.newSender(peer);
4041
try {
4142
String chunkUrl = dis.readUTF();
4243
//<<<<<<<<<<<<<<<<<<debug
@@ -58,7 +59,7 @@ public void run() {
5859
//>>>>>>>>>>>>>>>>>>>debug
5960

6061
if(this.reader != null) {
61-
this.reader.read(chunkUrl, peer, message);
62+
this.reader.read(chunkUrl, message);
6263
}
6364
}
6465
} catch (IOException ex) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.sharksystem.asp3;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
*
7+
* @author thsc
8+
*/
9+
public interface ASP3ChunkReceived extends ASP3Chunk {
10+
CharSequence getSender() throws IOException;
11+
12+
void addReceivedMessage(CharSequence message) throws IOException;
13+
}

src/net/sharksystem/asp3/ASP3ChunkStorage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
*
4747
* @author thsc
4848
*/
49-
public interface ASP3ChunkStorage extends ASP3Storage {
49+
public interface ASP3ChunkStorage /*extends ASP3Storage */{
50+
5051

5152
/**
5253
* Adds a recipient to chunk recipient list.
@@ -119,4 +120,6 @@ public interface ASP3ChunkStorage extends ASP3Storage {
119120
* @return
120121
*/
121122
public int getPreviousEra(int era);
123+
124+
public ASP3Storage getReceivedChunkStorage(CharSequence sender);
122125
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.sharksystem.asp3;
2+
3+
/**
4+
*
5+
* @author thsc
6+
*/
7+
class ASP3DefaultReader implements ASP3Reader {
8+
9+
private String sender;
10+
private int era;
11+
private ASP3Storage storage;
12+
private ASP3Engine engine;
13+
14+
public ASP3DefaultReader() {
15+
}
16+
17+
public void newSender(String sender) {
18+
this.sender = sender;
19+
}
20+
21+
@Override
22+
public void read(String urlTarget, String message) {
23+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
24+
}
25+
26+
void setEngine(ASP3Engine engine) {
27+
this.engine = engine;
28+
}
29+
30+
}

src/net/sharksystem/asp3/ASP3Engine.java

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see ASP3Reader
1919
* @author thsc
2020
*/
21-
public class ASP3Engine implements ASP3ChunkStorage, ASP3ProtocolEngine {
21+
public abstract class ASP3Engine implements ASP3ChunkStorage, ASP3ProtocolEngine {
2222
public static final String ANONYMOUS_OWNER = "anon";
2323
static String DEFAULT_OWNER = ANONYMOUS_OWNER;
2424
static int DEFAULT_INIT_ERA = 0;
@@ -31,9 +31,9 @@ public class ASP3Engine implements ASP3ChunkStorage, ASP3ProtocolEngine {
3131
protected ASP3Memento memento = null;
3232

3333
private final ASP3Reader reader;
34-
/* private */ final ASP3Storage chunkStorage;
34+
/* private */ final private ASP3Storage chunkStorage;
3535

36-
ASP3Engine(ASP3Storage chunkStorage, ASP3Reader reader)
36+
protected ASP3Engine(ASP3Storage chunkStorage, ASP3Reader reader)
3737
throws ASP3Exception, IOException {
3838

3939
this.chunkStorage = chunkStorage;
@@ -45,9 +45,9 @@ public class ASP3Engine implements ASP3ChunkStorage, ASP3ProtocolEngine {
4545
}
4646
*/
4747
}
48-
49-
ASP3Engine(ASP3Storage chunkStorage) throws ASP3Exception, IOException {
50-
this(chunkStorage, null);
48+
49+
ASP3Storage getStorage() {
50+
return this.chunkStorage;
5151
}
5252

5353
//////////////////////////////////////////////////////////////////////
@@ -71,26 +71,11 @@ public void removeRecipient(CharSequence urlTarget, CharSequence recipients) thr
7171

7272
@Override
7373
public void add(CharSequence urlTarget, CharSequence message) throws IOException {
74-
ASP3Chunk chunk = this.chunkStorage.getChunk(urlTarget, this.era);
74+
ASP3Chunk2Send chunk = this.chunkStorage.getChunk(urlTarget, this.era);
7575

7676
chunk.add(message);
7777
}
7878

79-
@Override
80-
public ASP3Chunk getChunk(CharSequence urlTarget, int era) throws IOException {
81-
return this.chunkStorage.getChunk(urlTarget, era);
82-
}
83-
84-
@Override
85-
public List<ASP3Chunk> getChunks(int era) throws IOException {
86-
return this.chunkStorage.getChunks(era);
87-
}
88-
89-
@Override
90-
public void dropChunks(int era) throws IOException {
91-
this.chunkStorage.dropChunks(era);
92-
}
93-
9479
//////////////////////////////////////////////////////////////////////
9580
// ProtocolEngine //
9681
//////////////////////////////////////////////////////////////////////
@@ -198,7 +183,7 @@ public void handleConnection(InputStream is, OutputStream os) {
198183
do {
199184
lastRound = workingEra == currentEra;
200185

201-
List<ASP3Chunk> chunks = this.chunkStorage.getChunks(workingEra);
186+
List<ASP3Chunk2Send> chunks = this.chunkStorage.getChunks(workingEra);
202187
//<<<<<<<<<<<<<<<<<<debug
203188
b = new StringBuilder();
204189
b.append(this.getLogStart());
@@ -207,7 +192,7 @@ public void handleConnection(InputStream is, OutputStream os) {
207192
System.out.println(b.toString());
208193
//>>>>>>>>>>>>>>>>>>>debug
209194

210-
for(ASP3Chunk chunk : chunks) {
195+
for(ASP3Chunk2Send chunk : chunks) {
211196
//<<<<<<<<<<<<<<<<<<debug
212197
b = new StringBuilder();
213198
b.append(this.getLogStart());
@@ -369,7 +354,7 @@ private synchronized void incrementEra() throws IOException {
369354
this.chunkStorage.dropChunks(this.era);
370355
}
371356

372-
private void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
357+
private void sendChunk(ASP3Chunk2Send chunk, DataOutputStream dos)
373358
throws IOException {
374359

375360
//<<<<<<<<<<<<<<<<<<debug
@@ -434,7 +419,7 @@ private void sendChunk(ASP3Chunk chunk, DataOutputStream dos)
434419
* @param chunk
435420
* @return
436421
*/
437-
private boolean isPublic(ASP3Chunk chunk) {
422+
private boolean isPublic(ASP3Chunk2Send chunk) {
438423
return chunk.getRecipients().isEmpty();
439424
}
440425

0 commit comments

Comments
 (0)