Bollocks!!!  Forgot the attachment!

On Sat, 26 Feb 2005 19:56:11 +1000, David Whyte <[EMAIL PROTECTED]> wrote:
> For the folks that asked me for the Java code that was developed to
> poke at the Mythbackend via Java Sockets, it is attached.
> 
> I have to say a big thanks to Bill Jackson as he did a *heap* of
> debugging to figure out why it wouldn't work.  It turned out that we
> were only sending one byte at a time, but we fixed that by using the
> PrintStream implementation of the OutputStream.
> 
> If you run the attached code, it will merely:
> - Check the protocol version,
> - Register with the backend and
> - Unregister from the backend.
> 
> Any questions, fire away,
> 
> Regards,
> Whytey
> 


-- 
GMAIL is 'da bomb baby....YEAH

I have GMail invites, if you want one, email me direct.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;


public class BackendTest {
    final static String HOSTNAME = "myth";
    final static int PORT = 6543;
    static boolean debug = true;
    Socket mythSocket;
    DataInputStream in;
    PrintStream out;
    String addr;
    int port;

    public BackendTest(String addr, int port) {
        mythSocket = null;
        in = null;
        out = null;
        this.addr = addr;
        this.port = port;
        init(addr, port);
    }

    static void trace(String msg) {
        if (debug) {
            System.err.println(msg);
        }
    }

    static void eprint(Exception e, String msg) {
        System.err.println("Error: " + msg);
        e.printStackTrace();
        System.exit(-1);
    }

    private void init(String addr, int port) {
        try {
            mythSocket = new Socket(addr, port);

            // mythSocket.setSoTimeout(25);
            InputStream is = mythSocket.getInputStream();
            in = new DataInputStream(is);

            OutputStream os = mythSocket.getOutputStream();
            out = new PrintStream(os);
        } catch (UnknownHostException e) {
            eprint(e, addr + ":" + port);
        } catch (IOException e) {
            eprint(e, addr + ":" + port);
        }
    }

    private void fini() {
        try {
            out.close();
            in.close();
            mythSocket.close();
        } catch (IOException e) {
            eprint(e, "closing socket");
        }
    }

    private String buildCommand(String command) {
        int len = command.length();
        String digits = "" + len;
        int width = digits.length();
        String pad = "        ".substring(0, 8 - width);
        String line = digits + pad + command;

        return line;
    }

    private String receiveResponse() {
        String ret = "";

        try {
            byte[] lbuf = new byte[8];
            in.readFully(lbuf);

            String len = new String(lbuf);
            int clen = Integer.parseInt(len.trim());
            trace("len: " + len);

            byte[] rbuf = new byte[clen];
            in.readFully(rbuf);

            String body = new String(rbuf);
            trace("body: " + body);

            ret = len + body;
        } catch (IOException e) {
            eprint(e, "response: " + ret);
        }

        return ret;
    }

    private String sendCommand(String cmd) {
        String command = buildCommand(cmd);
        trace(command);

     //   try {
            out.print(command);
            out.flush();
      //  } catch (IOException e) {
       //     eprint(e, "request: " + command);
       // }

        trace("accumulating response:");
        
        if (!cmd.equals("DONE"))
        {
          String ret = receiveResponse();
          trace(ret);
  
          return ret;
          
        } else 
        {
          trace("closed Socket");
        }
        
        return null;
        
    }

    public void hello() {
        String cmd1 = "MYTH_PROTO_VERSION 13";
        String ret = sendCommand(cmd1);

        String cmd2 = "ANN Playback " + "whytey" + " 0";
        ret = sendCommand(cmd2);
    }

    public void goodbye() {
        String cmd3 = "DONE";
        String ret = sendCommand(cmd3);
    }

    public static void main(String[] args) {
        String host = (args.length > 0) ? args[0] : HOSTNAME;
        int port = (args.length > 1) ? Integer.parseInt(args[1]) : PORT;
        trace("backend: " + host + ":" + port);

        BackendTest bet = new BackendTest(host, port);
        bet.hello();
        bet.goodbye();
    }
}
_______________________________________________
mythtv-dev mailing list
[email protected]
http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-dev

Reply via email to