| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.ByteArrayOutputStream; |
| 4 |
import java.io.IOException; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.HashMap; |
| 8 |
|
| 9 |
import barcode.DemuxServer.Pair; |
| 10 |
import fileIO.ByteStreamWriter; |
| 11 |
import server.ServerTools; |
| 12 |
import shared.KillSwitch; |
| 13 |
import shared.LineParser2; |
| 14 |
import shared.LineParserS1; |
| 15 |
import shared.LineParserS2; |
| 16 |
import shared.Shared; |
| 17 |
import sketch.Sketch; |
| 18 |
import structures.ByteBuilder; |
| 19 |
import structures.StringNum; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class DemuxClient { |
| 27 |
|
| 28 |
public static void main(String[] args) { |
| 29 |
|
| 30 |
int coding=Sketch.RAW; |
| 31 |
if(args.length>0 && args[0].equalsIgnoreCase("A48")) {coding=Sketch.A48;} |
| 32 |
|
| 33 |
ArrayList<DemuxServer.Pair> pairs=new ArrayList<DemuxServer.Pair>(); |
| 34 |
int len1, len2, delimiter; |
| 35 |
boolean dual=true; |
| 36 |
|
| 37 |
if(dual) { |
| 38 |
len1=10; |
| 39 |
len2=10; |
| 40 |
delimiter='+'; |
| 41 |
pairs.add(new Pair("ACGTACGTAC+CGAACGAACG", "ACGTACGTAC+CGAACGAACG")); |
| 42 |
pairs.add(new Pair("ACGTACGTAC+NNNNNNNNNN", "ACGTACGTAC+CGAACGAACG")); |
| 43 |
pairs.add(new Pair("GGGGGGGGGG+NNNNNNNNNN", "GGGGGGGGGG+AGAACGAACG")); |
| 44 |
pairs.add(new Pair("GGGGGGGGGG+GNNNNNNNNN", "GGGGGGGGGG+CGAACGAACG")); |
| 45 |
pairs.add(new Pair("GGGGGGGGGG+TCGTACGTTT", "GGGGGGGGGG+CGAACGAACG")); |
| 46 |
pairs.add(new Pair("NNNNNNNNNN+AAAAAAAAAA", "GGGGGTGGGG+AAAAAAAAAA")); |
| 47 |
}else { |
| 48 |
|
| 49 |
len1=10; |
| 50 |
len2=0; |
| 51 |
delimiter=0; |
| 52 |
pairs.add(new Pair("ACGTACGTAC", "ACGTACGTAC")); |
| 53 |
pairs.add(new Pair("CCGTACGTAC", "ACGTACGTAC")); |
| 54 |
pairs.add(new Pair("AGGGGGGGGG", "GGGGGGGGGG")); |
| 55 |
pairs.add(new Pair("TGGGGGGGGG", "GGGGGGGGGG")); |
| 56 |
pairs.add(new Pair("GGGGGGGGGG", "GGGGGGGGGG")); |
| 57 |
pairs.add(new Pair("NNNNNNNNNN", "GGGGGTGGGG")); |
| 58 |
} |
| 59 |
|
| 60 |
Collections.sort(pairs); |
| 61 |
System.err.println("Expected:\n"+pairs); |
| 62 |
System.err.println("\nWriting:"); |
| 63 |
|
| 64 |
ByteArrayOutputStream baos=new ByteArrayOutputStream(); |
| 65 |
DemuxServer.writeToStream(pairs, baos, coding, len1, len2); |
| 66 |
try { |
| 67 |
baos.close(); |
| 68 |
} catch (IOException e) { |
| 69 |
|
| 70 |
e.printStackTrace(); |
| 71 |
} |
| 72 |
String result=baos.toString(); |
| 73 |
System.err.println("Created:\n"+result); |
| 74 |
|
| 75 |
|
| 76 |
System.err.println("Decoding."); |
| 77 |
HashMap<String, String> map=null; |
| 78 |
if(coding==Sketch.RAW) { |
| 79 |
map=toMapRaw(result, true); |
| 80 |
}else { |
| 81 |
map=toMapA48(result.getBytes(), true, coding, len1, len2, (byte)delimiter); |
| 82 |
} |
| 83 |
ArrayList<DemuxServer.Pair> pairs2=DemuxServer.toPairList(map); |
| 84 |
System.err.println("Decoded:\n"+pairs2); |
| 85 |
} |
| 86 |
|
| 87 |
public DemuxClient() {} |
| 88 |
|
| 89 |
public DemuxClient(String address_) { |
| 90 |
if(address_!=null) {address=address_;} |
| 91 |
} |
| 92 |
|
| 93 |
HashMap<String, String> getMap(DemuxData dd, boolean verbose) { |
| 94 |
String response=sendAndReceive(dd, verbose); |
| 95 |
if(dd.coding==Sketch.A48) { |
| 96 |
return toMapA48(response.getBytes(), verbose, dd); |
| 97 |
}else { |
| 98 |
return toMapRaw(response, verbose); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private String sendAndReceive(DemuxData dd, boolean verbose) { |
| 103 |
if(verbose && false) { |
| 104 |
System.err.println("dd: type="+dd.type+", len1="+dd.length1+", len2="+dd.length2+ |
| 105 |
", delimiter="+dd.barcodeDelimiter+", hdistsum="+dd.hdistSum+ |
| 106 |
", expected="+dd.expectedList.size()+", counts="+dd.codeCounts.size()); |
| 107 |
} |
| 108 |
|
| 109 |
ArrayList<byte[]> chunks = dd.encode(); |
| 110 |
for(byte[] chunk : chunks) { |
| 111 |
assert(chunk[chunk.length-1]=='\n') : "'"+new String(chunk, chunk.length-60, 60)+"'"; |
| 112 |
} |
| 113 |
|
| 114 |
if(verbose) {System.err.println("Sending "+chunks.size()+" chunks.");} |
| 115 |
|
| 116 |
if(writeSent!=null) { |
| 117 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(writeSent, true, false, false); |
| 118 |
for(byte[] chunk : chunks) { |
| 119 |
bsw.print(chunk); |
| 120 |
} |
| 121 |
bsw.poisonAndWait(); |
| 122 |
} |
| 123 |
|
| 124 |
StringNum response=ServerTools.sendAndReceive(chunks, address+"demux", verbose); |
| 125 |
|
| 126 |
|
| 127 |
if(!ServerTools.suppressErrors && (response.n<200 || response.n>299)){ |
| 128 |
System.err.println("ERROR: Server returned code "+response.n+" and this message:\n"+response.s); |
| 129 |
KillSwitch.kill(); |
| 130 |
} |
| 131 |
boolean e=checkForError(response.s); |
| 132 |
errorState|=e; |
| 133 |
if(e) { |
| 134 |
System.err.println("An error was encountered: \n"+response.s); |
| 135 |
KillSwitch.kill(); |
| 136 |
} |
| 137 |
return response.s; |
| 138 |
} |
| 139 |
|
| 140 |
private static HashMap<String, String> toMapRaw(String response, boolean verbose){ |
| 141 |
|
| 142 |
HashMap<String, String> map=new HashMap<String, String>(); |
| 143 |
LineParserS2 responseParser=new LineParserS2('\n'); |
| 144 |
LineParserS1 tp=new LineParserS1('\t'); |
| 145 |
|
| 146 |
responseParser.set(response); |
| 147 |
while(responseParser.hasMore()) { |
| 148 |
responseParser.advance(); |
| 149 |
if(responseParser.currentFieldLength()>0) { |
| 150 |
|
| 151 |
String line=responseParser.parseStringFromCurrentField(); |
| 152 |
tp.set(line); |
| 153 |
String key=null, value=null; |
| 154 |
try { |
| 155 |
key=tp.parseString(0); |
| 156 |
value=tp.parseString(1); |
| 157 |
} catch (Throwable t) { |
| 158 |
t.printStackTrace(); |
| 159 |
System.err.println("'"+new String(line)+"'; "+tp.terms()); |
| 160 |
} |
| 161 |
assert(!map.containsKey(key)); |
| 162 |
map.put(key, value); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return map; |
| 167 |
} |
| 168 |
|
| 169 |
private static HashMap<String, String> toMapA48(byte[] response, boolean verbose, DemuxData dd){ |
| 170 |
return toMapA48(response, verbose, dd.coding, dd.length1, dd.length2, (byte)dd.barcodeDelimiter); |
| 171 |
} |
| 172 |
|
| 173 |
private static HashMap<String, String> toMapA48(byte[] response, boolean verbose, int coding, int len1, int len2, byte delimiter){ |
| 174 |
HashMap<String, String> map=new HashMap<String, String>(); |
| 175 |
LineParser2 responseParser=new LineParser2('\n'); |
| 176 |
LineParser2 tp=new LineParser2('\t'); |
| 177 |
final ByteBuilder bb=new ByteBuilder(32); |
| 178 |
|
| 179 |
byte[][] prev=new byte[4][]; |
| 180 |
byte[][] current=new byte[4][]; |
| 181 |
final int[] len=new int[] {len1, len2<1 ? len1 : len2, len1, len2}; |
| 182 |
responseParser.set(response); |
| 183 |
while(responseParser.hasMore()) { |
| 184 |
responseParser.advance(); |
| 185 |
if(responseParser.currentFieldLength()>0) { |
| 186 |
|
| 187 |
byte[] line=responseParser.parseByteArrayFromCurrentField(); |
| 188 |
tp.set(line); |
| 189 |
int term=0; |
| 190 |
while(tp.hasMore()) { |
| 191 |
tp.advance(); |
| 192 |
if(tp.currentFieldLength()<1) { |
| 193 |
current[term]=prev[term]; |
| 194 |
}else { |
| 195 |
current[term]=decodeA48(line, tp.a(), tp.b(), bb, len[term]); |
| 196 |
} |
| 197 |
term++; |
| 198 |
} |
| 199 |
|
| 200 |
assert(term==2 || term==4) : term+": '"+new String(line)+"'"; |
| 201 |
final String key, value; |
| 202 |
if(term==2) { |
| 203 |
key=new String(current[0]); |
| 204 |
value=new String(current[1]); |
| 205 |
}else if(delimiter>0){ |
| 206 |
key=bb.clear().append(current[0]).append(delimiter).append(current[1]).toString(); |
| 207 |
value=bb.clear().append(current[2]).append(delimiter).append(current[3]).toString(); |
| 208 |
}else { |
| 209 |
key=bb.clear().append(current[0]).append(current[1]).toString(); |
| 210 |
value=bb.clear().append(current[2]).append(current[3]).toString(); |
| 211 |
} |
| 212 |
assert(!map.containsKey(key)); |
| 213 |
map.put(key, value); |
| 214 |
} |
| 215 |
byte[][] temp=prev; |
| 216 |
prev=current; |
| 217 |
current=temp; |
| 218 |
} |
| 219 |
|
| 220 |
return map; |
| 221 |
} |
| 222 |
|
| 223 |
public static byte[] decodeA48(byte[] term, int from, int to, ByteBuilder bb, int len) { |
| 224 |
bb.clear(); |
| 225 |
long x=DemuxData.parseA48(term, from); |
| 226 |
DemuxData.decodeACGTN(x, bb, len); |
| 227 |
return bb.toBytes(); |
| 228 |
} |
| 229 |
|
| 230 |
private static boolean checkForError(String s){ |
| 231 |
if(s==null){return false;} |
| 232 |
return s.contains("java.io.IOException: Server returned HTTP response code:"); |
| 233 |
} |
| 234 |
|
| 235 |
public boolean errorState=false; |
| 236 |
public String address=Shared.demuxServer(); |
| 237 |
public static String writeSent=null; |
| 238 |
|
| 239 |
} |