In this first part, we will see how to communicate with an in-world object from a ruby on rails application thanks to XML-RPC. At the beginning, we define a second life structure (app/models/second_life_struct.rb) which represent the API format of second life :


class SecondLifeStruct < ActionWebService::Struct
  member "Channel", :string
  member "StringValue", :string
  member "IntValue", :int
end

Next, we create a new rails API (app/apis/second_life_api.rb) which use the structure :

class SecondLifeApi < ActionWebService::API::Base
  inflect_names false
  api_method "llRemoteData", :expects=>[SecondLifeStruct], :returns=>[SecondLifeStruct]
end

Before continuing, let's see the object LSL script which receive communication :


default
{
  state_entry()
    {
     llOpenRemoteDataChannel(); 
    }

    remote_data(integer type, key channel, key message_id, string sender, integer ival, string sval) 
    {
      if(type==REMOTE_DATA_CHANNEL) {
	llOwnerSay("Communication channel: "+(string)channel);
      } 

      if(type==REMOTE_DATA_REQUEST) {
        llOwnerSay(sval);
        llRemoteDataReply(channel,message_id,"Everything is alright",0);
      }
    }
}

Communication channel is an unique key which permit to communicate back with this object from the Linden's webservice. We must copy this key for loading our rails web page (/second_life/test_object?channel=key)

We just have to define the controller now:

class SecondLifeController < ApplicationController
  web_client_api :second_life, :xmlrpc, "http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi", :timeout=>90

  def test_object
    canal=params[:channel]
    begin
      resultat=second_life.llRemoteData(SecondLifeStruct.new("Channel"=>canal,"StringValue"=>"Hello World!","IntValue"=>"0"))
      render :text=>resultat["StringValue"]
    rescue
     render :text=>"Object doesn't respond"
    end
  end
end

That's all! When we load the page, second life object says "Hello World!" In the next post, we will see how to communicate in the other way : from in-world object to rails application.

More informations about second life XML-RPC format here.