Search code examples
javaandroidgrowl

Growl forwarding to Java server


I have a java server which is using TCP and sockets to connect to an Android application (the client) and sends strings (currently taken in from a scanner object) which are then displayed as notifications by the client.

heres the Server code without all the imports.

public class Server {

// define our Main method
public static void main(String[] args) throws IOException {
    // set up our Server Socket, set to null for the moment.
    ServerSocket serverSocket = null;
             boolean isConnected = false;

    // Lets try and instantiate our server and define a port number .
    try {
        serverSocket = new ServerSocket(6789);
                isConnected = true;
        System.out.println("***  I am the Server  ***\n");
        // make sure to always catch any exceptions that may occur.
    } catch (IOException e) {
        // always print error to "System.err"
        System.err.println("Could not listen on port: 6789.");
        System.exit(1);
    }

    // We always want to check for connection from Clients, so lets define
    // a for ever loop.
    for (;;) {
        // define a local client socket
        Socket clientSocket = null;
        // lets see if there are any connections and if so, accept it.
        try {
            clientSocket = serverSocket.accept();
            // don't forget to catch your exceptions
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        // Prepare the input & output streams via the client socket.

        // fancy way of saying we want to be able to read and write data to 
        // the client socket that is connected.

        BufferedReader inFromClient = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
        PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(),
                true);

              while (isConnected) { 
        // read a sentence from client
        String hiFromClient = inFromClient.readLine();


                    // Set up the logging system and timestamp and log message.

                     Calendar currentDate = Calendar.getInstance();
         SimpleDateFormat formatter= 
         new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
         String dateNow = formatter.format(currentDate.getTime());


        try{
            // Create file 
            File fstream = new File("log.txt");
                        FileWriter out = new FileWriter(fstream);
                               out.write(hiFromClient + " " + dateNow);

                               //Close the output stream
                               out.close();
                    } catch (Exception e){//Catch exception if any
                           System.err.println("Error: " + e.getMessage());
                    }


        // Print the client sentence to the screen
        System.out.println("The Client said: "+hiFromClient);

        // Reply to the client

                    Scanner scanner = new Scanner(System.in);
                    String sentence = scanner.nextLine();

        outToClient.println(sentence );
                    System.out.println("The Server said: " + sentence);

        }     
        // always remember to close all connections.


        inFromClient.close(); // the reader
        outToClient.close(); // the writer
        clientSocket.close(); // and the client socket
    }
}}

Growl uses port 23053 for notification forwarding. What i am hoping to do is to listen in on 23053 and send anything in from that as a string to the client connected at 6789. Sadly Growl binds the port number so a new Socket connection cant be made.

Any one have any ideas on how i could get notifications from the port number growl uses or even just use growl as the server for the client itself (the client's code is very similar to the servers by the way just using Socket instead of ServerSocket)

Any help on this would be greatly appreciated, its wrecking my brain

All the best,

Patrick.


Solution

  • There is a round-about way you could do it. If you are desperate, read on:

    Growl can forward any notifications it receives to another machine running Growl (configured on the Network tab). Growl uses the GNTP protocol (a TCP-based protocol: http://www.growlforwindows.com/gfw/help/gntp.aspx) to forward the messages. The trick is that that 'other machine running Growl' doesnt have to really be another machine OR running Growl per se, it just needs to appear to Growl that it is. Growl (on the Mac, which is what I assume you are using) will automatically detect any other machines on the network running Growl (using Bonjour and looking for the _gntp._tcp service name), so if your server advertises itself as supporting the GNTP protocol, Growl should show it in the list of available destinations. (Growl for Windows also lets you manually add a hostname/port to forward to, but I dont believe the Mac version currently allows that).

    So then you could configure Growl to forward notifications to your server using its already-built-in capabilities. You would have to implement code in your server to receive the GNTP packets (the format is very similar to HTTP headers) and parse them. Then you could forward the notifications using your current server code.

    Still with me? I did say it was round-about, but it is not only technically possible, but I have built a Growl-impersonating daemon before so that I could have notifications forwarded from Growl to my custom code. Not suggesting it as the best idea, but just an alternative since you asked.