søndag 8. november 2009

Using the XBMC event server with rTorrent.

So i stumbled upon the xbmc's event server and its capabilities when doing a clean install of xbmc on my home theater pc solution. Basically what the event server does, is that it allows you to turn any device with network capability into a xbmc remote. anything that you can do with a mouse or a keyboard you can pass along to the event server. However a feature that isn't very well described is that the event server also supports notifications.

I've always been frustrated that i don't always have full overview of whats going on with my rTorrent server. I either have to use a ssh shell and connect to a screen running rtorrent to check my current downloads or use my own developed program nTorrent on a client machine before watching any given downloaded media. Now however, i can create a c++/java/python/c# (or whatever) notification program, that i can hook into with rtorrent's onstart and onfinished events to actually display the state of my current downloads right on my television set! sweet eyh!?

So i started having some fun with a little c++ code, and after an hour or so (yes i am not very driven in c++)  i ended up with this piece of code:

//============================================================================
// Name        : SimpleXBMCNotification.cpp
// Author      : Kim Eik
// Version     : 0.1
// Copyright   : GPLv3
// Description : Sends a notification to an xbmc event server
//============================================================================

#include <stdio.h>
#include <xbmc/xbmcclient.h>
#include <sys/socket.h>
#include <string.h>
#include <iostream>
#include <stdlib.h>



int main(int argc, char *argv[]) {

 if(argc <= 3){
  std::cout << "Invalid arguments.\n" << argv[0] << "<title> <message> <address> [<port>]\n";
  return EXIT_FAILURE;
 }

 const char *title = argv[1];
 const char *message = argv[2];
 const char *address = argv[3];
 int port;

 if(argc < 5){
  port = STD_PORT;
 }else{
  port = atoi(argv[4]);
 }

 CAddress addressObj = CAddress(address,port);
 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

 if (sockfd < 0){
  std::cout << "Error creating socket\n";
  return EXIT_FAILURE;
 }

 addressObj.Bind(sockfd);

 CPacketHELO HeloPackage("Notification", ICON_NONE);
 HeloPackage.Send(sockfd, addressObj);

 CPacketNOTIFICATION packet(title,message,ICON_NONE);
 packet.Send(sockfd, addressObj);

 CPacketBYE bye;
 bye.Send(sockfd, addressObj);

 return EXIT_SUCCESS;
}

And voila! it works!

Now that i can send notifications to my xbmc i can start configuring rtorrent to call the SimpleXBMCNotification application i just created.

I set about reading the rtorrent documentation for available events and came up with the following additions to the rtorrent.rc config file (most of the lines are commented out as you can see).

#SimpleXBMCNotification
system.method.set_key = event.download.finished,notify_finished,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent finished\",$d.get_name=,10.0.0.12"
system.method.set_key = event.download.erased,notify_erased,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent erased\",$d.get_name=,10.0.0.12"
system.method.set_key = event.download.inserted_new,notify_inserted_new,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent inserted (new)\",$d.get_name=,10.0.0.12"

#Other available events.
#system.method.set_key = event.download.inserted_session,notify_inserted_session,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent inserted (session)\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.inserted,notify_inserted,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent inserted\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.opened,notify_opened,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent opened\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.closed,notify_closed,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent closed\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.resumed,notify_resumed,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent started\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.paused,notify_stop,"execute=/usr/local/bin/SimpleXBMCNotification,\"Torrent stopped\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.hash_queued,notify_hash_queued,"execute=/usr/local/bin/SimpleXBMCNotification,\"Hash check queued\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.hash_removed,notify_hash_removed,"execute=/usr/local/bin/SimpleXBMCNotification,\"Hash check removed\",$d.get_name=,10.0.0.12"
#system.method.set_key = event.download.hash_done,notify_hash_done,"execute=/usr/local/bin/SimpleXBMCNotification,\"Hash check completed\",$d.get_name=,10.0.0.12"

So after i added theese lines to my rtorrent.rc config file, notifications started to pop up on my television set! Here is some proof!




So that's that, please feel free to copy the code, and make your own changes too it. An obvious change would be to add your own icons to the notification application. I skipped all the details as i just wanted to see if it really was possible. Anyways happy hacking.

4 kommentarer:

  1. Segmentation fault on line
    HeloPackage.Send(sockfd, addressObj);

    SvarSlett
  2. you probably dont have the xbmc header files.

    SvarSlett
  3. http://netbl0g.blogspot.com/2010/02/using-xbmc-eventserver-natively.html

    SvarSlett