Skip to content

Commit d1ede45

Browse files
committed
Batchprocessor returns asap peer. Can write real test now.
1 parent 18c791a commit d1ede45

File tree

10 files changed

+86
-53
lines changed

10 files changed

+86
-53
lines changed

src/net/sharksystem/asap/ASAPChannelImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public HashMap<String, String> getExtraData() throws IOException {
3636

3737
@Override
3838
public ASAPMessages getMessages() throws IOException {
39-
return this.asapEngine.getChunkStorage().getASAPChunkCache(this.getUri(), this.asapEngine.getEra());
39+
return this.asapEngine.getChunkStorage().getASAPMessages(this.getUri(), this.asapEngine.getEra());
4040
}
4141

4242
@Override

src/net/sharksystem/asap/ASAPChunkStorage.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public interface ASAPChunkStorage {
3131
* @return a chunk cache which hides details of era
3232
* @throws IOException
3333
*/
34-
public ASAPMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException;
34+
ASAPMessages getASAPMessages(CharSequence uri, int toEra) throws IOException;
3535

36-
public ASAPMessages getASAPChunkCache(CharSequence uri, int fromEra, int toEra) throws IOException;
36+
ASAPMessages getASAPMessages(CharSequence uri, int fromEra, int toEra) throws IOException;
37+
38+
ASAPMessages getASAPMessages(String uri) throws ASAPException, IOException;
3739
}

src/net/sharksystem/asap/ASAPChunkStorageFS.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616
* @author thsc
1717
*/
1818
class ASAPChunkStorageFS implements ASAPChunkStorage {
19-
2019
private final String rootDirectory;
2120
private final String format;
21+
private int era = -1;
2222

2323
ASAPChunkStorageFS(String rootDirectory, String format) {
2424
this.rootDirectory = rootDirectory;
2525
this.format = format;
2626
}
2727

28+
ASAPChunkStorageFS(String rootDirectory, String format, int era) {
29+
this.rootDirectory = rootDirectory;
30+
this.format = format;
31+
this.era = era;
32+
}
33+
2834
public String getFormat() {
2935
return this.format;
3036
}
@@ -126,22 +132,15 @@ public void dropChunks(int era) throws IOException {
126132
}
127133

128134
@Override
129-
public ASAPMessages getASAPChunkCache(CharSequence uri, int toEra) throws IOException {
135+
public ASAPMessages getASAPMessages(CharSequence uri, int toEra) throws IOException {
130136
// INIT ++++++++++++++++++++++ toEra +++++++++++++++++++++ MAX
131137

132138
int fromEra = ASAP.nextEra(toEra); // the whole cycle
133-
/*
134-
// go back 1000 eras
135-
int fromEra = toEra;
136-
for(int i = 0; i < 1000; i++) {
137-
fromEra = ASAP.previousEra(fromEra);
138-
}
139-
*/
140-
return this.getASAPChunkCache(uri, fromEra, toEra);
139+
return this.getASAPMessages(uri, fromEra, toEra);
141140
}
142141

143142
@Override
144-
public ASAPMessages getASAPChunkCache(CharSequence uri, int fromEra, int toEra) throws IOException {
143+
public ASAPMessages getASAPMessages(CharSequence uri, int fromEra, int toEra) throws IOException {
145144
Log.writeLog(this, "create ASAPInMemoMessages");
146145

147146
return new ASAPInMemoMessages(
@@ -152,4 +151,12 @@ public ASAPMessages getASAPChunkCache(CharSequence uri, int fromEra, int toEra)
152151
toEra // anything before
153152
);
154153
}
154+
155+
@Override
156+
public ASAPMessages getASAPMessages(String uri) throws ASAPException, IOException {
157+
if(this.era == -1) {
158+
throw new ASAPException("internal error: era not set - use other constructor or method");
159+
}
160+
return this.getASAPMessages(uri, this.era);
161+
}
155162
}

src/net/sharksystem/asap/ASAPEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public ASAPMessages getChunkChain(int uriPosition) throws IOException, ASAPExcep
247247
}
248248

249249
public ASAPMessages getChunkChain(CharSequence uri, int toEra) throws IOException {
250-
return this.chunkStorage.getASAPChunkCache(uri, toEra);
250+
return this.chunkStorage.getASAPMessages(uri, toEra);
251251
}
252252

253253
public ASAPMessages getChunkChain(CharSequence uri) throws IOException {
@@ -267,7 +267,7 @@ public ASAPMessages getChunkChain(int uriPosition, int toEra)
267267
throw new ASAPException("uri at postion is null. Position: " + uriPosition);
268268
}
269269

270-
return this.chunkStorage.getASAPChunkCache(uri, toEra);
270+
return this.chunkStorage.getASAPMessages(uri, toEra);
271271
}
272272

273273
//////////////////////////////////////////////////////////////////////

src/net/sharksystem/asap/ASAPEngineFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private ASAPMementoFS getMemento(String rootDirectory) {
198198
@Override
199199
public ASAPChunkStorage getReceivedChunksStorage(CharSequence sender) {
200200
String dir = this.rootDirectory + "/" + sender;
201-
return new ASAPChunkStorageFS(dir, this.format);
201+
return new ASAPChunkStorageFS(dir, this.format, this.era);
202202
}
203203

204204
public ASAPStorage getExistingIncomingStorage(CharSequence sender) throws IOException, ASAPException {

src/net/sharksystem/asap/apps/ASAPJavaApplicationFS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void chunkReceived(String format, String sender, String uri, int era) {
9797
System.out.println(getLogStart() + "chunk received - convert to asap message received");
9898
try {
9999
ASAPEngine engine = ASAPJavaApplicationFS.this.multiEngine.getEngineByFormat(format);
100-
ASAPMessages messages = engine.getReceivedChunksStorage(sender).getASAPChunkCache(uri, era);
100+
ASAPMessages messages = engine.getReceivedChunksStorage(sender).getASAPMessages(uri, era);
101101
this.listener.asapMessagesReceived(messages);
102102
} catch (ASAPException | IOException e) {
103103
System.out.println(getLogStart() + e.getLocalizedMessage());

src/net/sharksystem/asap/util/Batchprocessor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap.util;
22

33
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.ASAPPeer;
45
import net.sharksystem.cmdline.CmdLineUI;
56

67
import java.io.ByteArrayInputStream;
@@ -28,6 +29,10 @@ public Batchprocessor(boolean cleanup) {
2829
}
2930
}
3031

32+
public void setOutputstream(PrintStream ps) {
33+
this.cmdLineUI.setOutStreams(ps);
34+
}
35+
3136
public void addCommand(String cmd) {
3237
this.cmdList.add(cmd);
3338
}
@@ -84,4 +89,8 @@ private void doExecution() {
8489
public void run() {
8590
this.doExecution();
8691
}
92+
93+
public ASAPPeer getASAPPeer(String peerName) throws ASAPException {
94+
return this.cmdLineUI.getASAPPeer(peerName);
95+
}
8796
}

src/net/sharksystem/asap/util/Helper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static ASAPMessages getMessagesByChunkReceivedInfos(String format, String
112112
ASAPChunkStorage chunkStorage = existingASAPEngineFS.getReceivedChunksStorage(sender);
113113
Log.writeLog(Helper.class, "got incoming channel of " + sender);
114114

115-
ASAPMessages asapMessages = chunkStorage.getASAPChunkCache(uri, era, era);
115+
ASAPMessages asapMessages = chunkStorage.getASAPMessages(uri, era, era);
116116
Log.writeLog(Helper.class, "got messages uri: " + uri + " / era: " + era);
117117

118118
return asapMessages;

src/net/sharksystem/cmdline/CmdLineUI.java

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class CmdLineUI {
2828
public static final String SLEEP = "sleep";
2929
public static final String SHOW_LOG = "showlog";
3030

31-
private PrintStream consoleOutput;
31+
private PrintStream standardOut = System.out;
32+
private PrintStream standardError = System.err;
33+
3234
private BufferedReader userInput;
3335

3436
public static final String PEERS_ROOT_FOLDER = "asapPeers";
@@ -48,6 +50,11 @@ public CmdLineUI(PrintStream out) throws IOException, ASAPException {
4850
this(out, null);
4951
}
5052

53+
public void setOutStreams(PrintStream ps) {
54+
this.standardOut = ps;
55+
this.standardError = ps;
56+
}
57+
5158
/**
5259
* only for batch processing - removes anything from the past
5360
* @throws IOException
@@ -58,7 +65,7 @@ public CmdLineUI() {
5865
}
5966

6067
public CmdLineUI(PrintStream os, InputStream is) throws IOException, ASAPException {
61-
this.consoleOutput = os;
68+
this.standardOut = os;
6269
this.userInput = is != null ? new BufferedReader(new InputStreamReader(is)) : null;
6370

6471
// set up peers
@@ -132,11 +139,11 @@ public void printUsage() {
132139
b.append(EXIT);
133140
b.append(".. exit");
134141

135-
this.consoleOutput.println(b.toString());
142+
this.standardOut.println(b.toString());
136143
}
137144

138145
public void printUsage(String cmdString, String comment) throws ASAPException {
139-
PrintStream out = this.consoleOutput;
146+
PrintStream out = this.standardOut;
140147

141148
if(comment == null) comment = " ";
142149
out.println("malformed command: " + comment);
@@ -228,7 +235,7 @@ public void printUsage(String cmdString, String comment) throws ASAPException {
228235
private List<String> cmds = new ArrayList<>();
229236

230237
public void runCommandLoop(PrintStream os, InputStream is) {
231-
this.consoleOutput = os;
238+
this.standardOut = os;
232239
this.userInput = is != null ? new BufferedReader(new InputStreamReader(is)) : null;
233240

234241
this.runCommandLoop();
@@ -301,15 +308,15 @@ public void runCommandLoop() {
301308
this.doKill("all");
302309
again = false; break; // end loop
303310

304-
default: System.err.println("unknown command:" + cmdLineString);
311+
default: this.standardError.println("unknown command:" + cmdLineString);
305312
this.printUsage();
306313
rememberCommand = false;
307314
break;
308315
}
309316
} catch (ASAPException ex) {
310317
rememberCommand = false;
311318
} catch (IOException ex) {
312-
this.consoleOutput.println("cannot read from input stream");
319+
this.standardOut.println("cannot read from input stream");
313320
System.exit(0);
314321
}
315322

@@ -350,7 +357,7 @@ private ASAPStorage getStorage(String peername, String appName) throws ASAPExcep
350357
return asapPeer.getEngineByFormat(appName);
351358
}
352359

353-
private ASAPPeer getASAPPeer(String peerName) throws ASAPException {
360+
public ASAPPeer getASAPPeer(String peerName) throws ASAPException {
354361
ASAPPeer asapPeer = this.peers.get(peerName);
355362
if(asapPeer == null) {
356363
throw new ASAPException("engine does not exist: " + peerName);
@@ -381,14 +388,14 @@ public TCPStreamCreatedHandler(ASAPPeer asapPeer) {
381388

382389
@Override
383390
public void streamCreated(TCPStream channel) {
384-
CmdLineUI.this.consoleOutput.println("Channel created");
391+
CmdLineUI.this.standardOut.println("Channel created");
385392

386393
try {
387394
this.asapPeer.handleConnection(
388395
channel.getInputStream(),
389396
channel.getOutputStream());
390397
} catch (IOException | ASAPException e) {
391-
CmdLineUI.this.consoleOutput.println("call of engine.handleConnection failed: "
398+
CmdLineUI.this.standardOut.println("call of engine.handleConnection failed: "
392399
+ e.getLocalizedMessage());
393400
}
394401
}
@@ -446,9 +453,9 @@ public void doOpen(String parameterString) throws ASAPException {
446453
}
447454

448455
public void doList() throws ASAPException {
449-
this.consoleOutput.println("connections:");
456+
this.standardOut.println("connections:");
450457
for(String connectionName : this.streams.keySet()) {
451-
this.consoleOutput.println(connectionName);
458+
this.standardOut.println(connectionName);
452459
}
453460

454461
this.doPrintAllInformation();
@@ -460,23 +467,23 @@ public void doKill(String parameterString) throws ASAPException {
460467
try {
461468
String channelName = st.nextToken();
462469
if(channelName.equalsIgnoreCase("all")) {
463-
System.out.println("kill all open channels..");
470+
this.standardOut.println("kill all open channels..");
464471
for(TCPStream channel : this.streams.values()) {
465472
channel.kill();
466473
}
467474
this.streams = new HashMap<>();
468-
System.out.println(".. done");
475+
this.standardOut.println(".. done");
469476
} else {
470477

471478
TCPStream channel = this.streams.remove(channelName);
472479
if (channel == null) {
473-
System.err.println("channel does not exist: " + channelName);
480+
this.standardError.println("channel does not exist: " + channelName);
474481
return;
475482
}
476-
System.out.println("kill channel");
483+
this.standardOut.println("kill channel");
477484
channel.kill();
478485

479-
System.out.println(".. done");
486+
this.standardOut.println(".. done");
480487
}
481488
}
482489
catch(RuntimeException e) {
@@ -580,7 +587,7 @@ public void doCreateASAPMessage(String parameterString) throws ASAPException {
580587
// first - get storage
581588
ASAPStorage asapStorage = this.getStorage(peername, appName);
582589
if(asapStorage == null) {
583-
System.err.println("storage does not exist: " + peername + ":" + appName);
590+
this.standardError.println("storage does not exist: " + peername + ":" + appName);
584591
return;
585592
}
586593
asapStorage.add(uri, message);
@@ -618,10 +625,10 @@ public void doSetSendReceivedMessage(String parameterString) throws ASAPExceptio
618625

619626
public void doPrintAllInformation() throws ASAPException {
620627
try {
621-
this.consoleOutput.println(this.peers.keySet().size() + " peers in folder: " + PEERS_ROOT_FOLDER);
628+
this.standardOut.println(this.peers.keySet().size() + " peers in folder: " + PEERS_ROOT_FOLDER);
622629
for(String peername : this.peers.keySet()) {
623-
this.consoleOutput.println("+++++++++++++++++++");
624-
this.consoleOutput.println("Peer: " + peername);
630+
this.standardOut.println("+++++++++++++++++++");
631+
this.standardOut.println("Peer: " + peername);
625632
ASAPPeer asapPeer = this.peers.get(peername);
626633

627634
for (CharSequence format : asapPeer.getFormats()) {
@@ -631,7 +638,7 @@ public void doPrintAllInformation() throws ASAPException {
631638
this.printChannelInfo(asapStorage, uri, format);
632639
}
633640
}
634-
this.consoleOutput.println("+++++++++++++++++++\n");
641+
this.standardOut.println("+++++++++++++++++++\n");
635642
}
636643
}
637644
catch(RuntimeException | IOException | ASAPException e) {
@@ -654,7 +661,7 @@ public void doPrintStorageInformation(String parameterString) throws ASAPExcepti
654661
}
655662

656663
// iterate URI
657-
this.consoleOutput.println(asapStorage.getChannelURIs().size() +
664+
this.standardOut.println(asapStorage.getChannelURIs().size() +
658665
" channels in storage " + appName +
659666
" (note: channels without messages are considered non-existent)");
660667
for(CharSequence uri : asapStorage.getChannelURIs()) {
@@ -673,7 +680,7 @@ public void doSleep(String parameterString) throws ASAPException {
673680
Thread.sleep(Long.parseLong(parameterString));
674681
}
675682
catch(InterruptedException e) {
676-
this.consoleOutput.println("sleep interrupted");
683+
this.standardOut.println("sleep interrupted");
677684
}
678685
catch(RuntimeException e) {
679686
this.printUsage(PRINT_STORAGE_INFORMATION, e.getLocalizedMessage());
@@ -686,14 +693,14 @@ private void doShowLog() {
686693
boolean first = true;
687694
for(String c : this.cmds) {
688695
if (!first) {
689-
this.consoleOutput.println("\\n\" + ");
696+
this.standardOut.println("\\n\" + ");
690697
} else {
691698
first = false;
692699
}
693-
this.consoleOutput.print("\"");
694-
this.consoleOutput.print(c);
700+
this.standardOut.print("\"");
701+
this.standardOut.print(c);
695702
}
696-
this.consoleOutput.println("\"");
703+
this.standardOut.println("\"");
697704
}
698705

699706
public void doPrintChannelInformation(String parameterString) throws ASAPException {
@@ -708,7 +715,7 @@ public void doPrintChannelInformation(String parameterString) throws ASAPExcepti
708715
// first - get storage
709716
ASAPStorage asapStorage = this.getStorage(peername, appName);
710717
if(asapStorage == null) {
711-
System.err.println("storage does not exist: " + peername + ":" + appName);
718+
this.standardError.println("storage does not exist: " + peername + ":" + appName);
712719
return;
713720
}
714721

@@ -726,12 +733,12 @@ private void printChannelInfo(ASAPStorage asapStorage, CharSequence uri, CharSeq
726733
ASAPChannel channel = asapStorage.getChannel(uri);
727734
Set<CharSequence> recipients = channel.getRecipients();
728735

729-
this.consoleOutput.println("Peer:App:Channel == " + channel.getOwner() + ":" + appName + ":" + channel.getUri());
730-
this.consoleOutput.println("#Messages == " + channel.getMessages().size());
731-
this.consoleOutput.println("#Recipients == " + recipients.size() +
736+
this.standardOut.println("Peer:App:Channel == " + channel.getOwner() + ":" + appName + ":" + channel.getUri());
737+
this.standardOut.println("#Messages == " + channel.getMessages().size());
738+
this.standardOut.println("#Recipients == " + recipients.size() +
732739
" (0 means: open channel - no restrictions - anybody receives from this channel)");
733740
for(CharSequence recipient : recipients) {
734-
this.consoleOutput.println(recipient);
741+
this.standardOut.println(recipient);
735742
}
736743
}
737744

0 commit comments

Comments
 (0)