package rpc.bson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;

import net.sf.json.JSONObject;

import org.bson.BSONObject;
import org.bson.BSONEncoder;
import org.bson.BSONDecoder;
import org.bson.BasicBSONObject;
import org.bson.BasicBSONEncoder;
import org.bson.BasicBSONDecoder;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println("Demo how to write a raw Java client side according to bson-rpc protocol.");

				try {
					Socket socket = new Socket("127.0.0.1", 8181);

					OutputStream outputStream = socket.getOutputStream();
					BSONObject out = new BasicBSONObject();
					BSONEncoder encoder = new BasicBSONEncoder();
					out.put("fn", "__stats__");
					byte[] bin = encoder.encode(out);
					outputStream.write(bin);
					outputStream.flush();

					InputStream inputStream = socket.getInputStream();
					BSONDecoder decoder = new BasicBSONDecoder();
					BSONObject in = decoder.readObject(inputStream); // read exactly a single BSON Object

					Map m = in.toMap();
					JSONObject json = JSONObject.fromObject(m);
					System.out.println(json.toString());

					// to keep the persisten connection, don't close them
					//outputStream.close(); 
					//inputStream.close();
					//socket.close();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

    }
}

@@ -702,23 +702,32 @@
 );%0A%0A
-%09%09%09%09try %7B%0A%09%09%09%09%09
+        try %7B%0A          
 Sock
@@ -770,21 +770,26 @@
 8181);%0A%0A
-%09%09%09%09%09
+          
 OutputSt
@@ -830,29 +830,34 @@
 utStream();%0A
-%09%09%09%09%09
+          
 BSONObject o
@@ -880,29 +880,34 @@
 ONObject();%0A
-%09%09%09%09%09
+          
 BSONEncoder 
@@ -940,21 +940,26 @@
 oder();%0A
-%09%09%09%09%09
+          
 out.put(
@@ -978,21 +978,26 @@
 ts__%22);%0A
-%09%09%09%09%09
+          
 byte%5B%5D b
@@ -1022,21 +1022,26 @@
 e(out);%0A
-%09%09%09%09%09
+          
 outputSt
@@ -1057,21 +1057,26 @@
 e(bin);%0A
-%09%09%09%09%09
+          
 outputSt
@@ -1090,21 +1090,26 @@
 ush();%0A%0A
-%09%09%09%09%09
+          
 InputStr
@@ -1147,29 +1147,34 @@
 utStream();%0A
-%09%09%09%09%09
+          
 BSONDecoder 
@@ -1207,21 +1207,26 @@
 oder();%0A
-%09%09%09%09%09
+          
 BSONObje
@@ -1304,21 +1304,26 @@
 Object%0A%0A
-%09%09%09%09%09
+          
 Map m = 
@@ -1334,21 +1334,26 @@
 oMap();%0A
-%09%09%09%09%09
+          
 JSONObje
@@ -1388,21 +1388,26 @@
 ect(m);%0A
-%09%09%09%09%09
+          
 System.o
@@ -1436,21 +1436,26 @@
 ng());%0A%0A
-%09%09%09%09%09
+          
 // to ke
@@ -1500,21 +1500,26 @@
 se them%0A
-%09%09%09%09%09
+          
 //output
@@ -1535,21 +1535,26 @@
 ose(); %0A
-%09%09%09%09%09
+          
 //inputS
@@ -1568,21 +1568,26 @@
 lose();%0A
-%09%09%09%09%09
+          
 //socket
@@ -1592,28 +1592,32 @@
 et.close();%0A
-%09%09%09%09
+        
 %7D catch (Unk
@@ -1631,37 +1631,42 @@
 tException e) %7B%0A
-%09%09%09%09%09
+          
 e.printStackTrac
@@ -1670,20 +1670,24 @@
 race();%0A
-%09%09%09%09
+        
 %7D catch 
@@ -1708,13 +1708,18 @@
 ) %7B%0A
-%09%09%09%09%09
+          
 e.pr
@@ -1739,12 +1739,16 @@
 ();%0A
-%09%09%09%09
+        
 %7D%0A%0A 

fix tabstop