I have a question. I'm trying to read a SocketChannel for the first time,
and I want to validate if I'm doing the reading part correctly. Any
comments are welcome.
I want to read the socket into a buffer(10) (it's for debugging purpose) and
loop until a find the string [eoq] or when the max buffer length is 50. If
the string is not found, I copy the line into a buffer and continue to read.
My socket is blocking. I want to create a sample with blocking and after
that non-blocking. (just to be able to understand how it works)
protected SocketChannel f_socketChannel;
protected int limitBB = 50;
protected CharsetDecoder asciiDecoder = Charset.forName
("ISO-8859-1").newDecoder();
protected ByteBuffer cumulatifBB = ByteBuffer.allocate(10);
protected ByteBuffer buf = ByteBuffer.allocateDirect(10);
public String getQuery(ByteBuffer buf) throws IOException {
while(f_socketChannel.isOpen()){
// Clear the buffer and read bytes from socket
buf.clear();
int numBytesRead = f_socketChannel.read(buf);
if (numBytesRead == -1) {
// No more bytes can be read from the channel
close();
} else {
// To read the bytes, flip the buffer
buf.flip();
if(buf.hasRemaining()){
// faut trouver le pattern [eoq].
System.out.println("Remaining=" + buf.remaining());
// on remplit le BB cumulatifBB avec le BB buf
cumulatifBB = getBB(cumulatifBB, buf);
// On lit le buffer cumulatif pour voir si on trouverait
la EOQ
ByteBuffer tmp = cumulatifBB.duplicate();
tmp.flip();
// on decode le buffer pour voir si nous avons recu le
EOQ
String msg = asciiDecoder.decode(tmp).toString();
System.out.println("msg=" + msg);
int index = msg.indexOf("[eoq]");
if(index>-1){
String query = msg.substring(0,index);
System.out.println("Query = " + query);
// FAUT CONSERVER CE QUI VIENT APRES LE EOQ
// on vide le buffer
cumulatifBB.clear();
cumulatifBB.put(msg.substring(index +
"[eoq]".length()).getBytes());
return query;
} else {
System.out.println("pas de EOQ dans cette
iteration");
}
}
// DEBUG
System.out.println(getName() + " is SLEEPING...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// nous avons un probleme
return null;
}
public ByteBuffer getBB(ByteBuffer cumulatifBB, ByteBuffer buf){
// on valide que le buffer n'explosera pas
if(cumulatifBB.position() + buf.remaining()>=cumulatifBB.capacity
()){
// on regarde si nous n'avons pas la valeur maximale pour le
buffer
if(cumulatifBB.capacity() + buf.remaining()<limitBB){
ByteBuffer bb = ByteBuffer.allocate(cumulatifBB.capacity() +
buf.capacity());
cumulatifBB.flip();
bb.put(cumulatifBB);
cumulatifBB = bb;
} else {
// on avons atteind le maximum, donc on ferme la connection
System.out.println("BUFFER MAX ATTEIND.. LE CLIENT ENVOYE DE
LA JUNK OU LE BUFFER EST TROP PETIT!");
}
}
cumulatifBB = cumulatifBB.put(buf);
return cumulatifBB;
}
public void run() {
while(f_socketChannel.isOpen()){
try {
String query = getQuery(buf);
System.out.println("QUERY RETURNED = " + query);
if(query!=null){
//processQuery(query); all the work will be done here.
}
} catch (IOException e) {
// Connection may have been closed
e.printStackTrace();
// CLOSE
}
}
System.out.println("QUIT LE CLIENT CONNECTION HANDLER");
}