Class: LibSL::NetworkManager

Inherits:
Object
  • Object
show all
Defined in:
lib/network.rb

Constant Summary collapse

GRIDS =
{
	:agni => "https://login.agni.lindenlab.com/cgi-bin/login.cgi",
	:aditi => "https://login.aditi.lindenlab.com/cgi-bin/login.cgi"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ NetworkManager

Initialize the network manager

Parameters:

  • client (Client)

    The client



185
186
187
188
# File 'lib/network.rb', line 185

def initialize(client)
	@client = client
	@sims = []
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



179
180
181
# File 'lib/network.rb', line 179

def agent_id
  @agent_id
end

#circuit_codeObject (readonly)

Returns the value of attribute circuit_code.



179
180
181
# File 'lib/network.rb', line 179

def circuit_code
  @circuit_code
end

#clientObject

Returns the value of attribute client.



178
179
180
# File 'lib/network.rb', line 178

def client
  @client
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



179
180
181
# File 'lib/network.rb', line 179

def first_name
  @first_name
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



179
180
181
# File 'lib/network.rb', line 179

def last_name
  @last_name
end

#message_of_the_dayObject (readonly)

Returns the value of attribute message_of_the_day.



179
180
181
# File 'lib/network.rb', line 179

def message_of_the_day
  @message_of_the_day
end

#ready_handlerObject (readonly)

Returns the value of attribute ready_handler.



179
180
181
# File 'lib/network.rb', line 179

def ready_handler
  @ready_handler
end

#secure_session_idObject (readonly)

Returns the value of attribute secure_session_id.



179
180
181
# File 'lib/network.rb', line 179

def secure_session_id
  @secure_session_id
end

#session_idObject (readonly)

Returns the value of attribute session_id.



179
180
181
# File 'lib/network.rb', line 179

def session_id
  @session_id
end

#simsObject

Returns the value of attribute sims.



178
179
180
# File 'lib/network.rb', line 178

def sims
  @sims
end

Instance Method Details

#activate_simulator(sim) ⇒ Object

Activates a simulator as the main simulator

Parameters:

  • sim (Simulator)

    The simulator to activate



289
290
291
292
293
294
# File 'lib/network.rb', line 289

def activate_simulator(sim)
	@sims << sim unless @sims.include? sim
	sim.connect unless sim.connected?
	@active_sim = sim
	AgentManager.move_to_sim sim
end

#connect_to_simulator(ip, port) ⇒ Object

Connects to a simulator

Parameters:

  • ip (String)

    The simulator ip

  • port (Integer)

    The simulator port



281
282
283
284
285
# File 'lib/network.rb', line 281

def connect_to_simulator(ip, port)
	@sims << Simulator.new(@client, ip, port, @circuit_code, @session_id, @agent_id)
	@sims.last.connect
	@active_sim = @sims.last
end

#login(first, last, pass, start = "last", grid = :agni) ⇒ Object

Login to the grid

Parameters:

  • first (String)

    Avatar firstname or account username

  • last (String)

    (optional) Avatar lastname or “Resident”

  • pass (String)

    The password

  • start (String) (defaults to: "last")

    (optional) The starting location to connect to

  • grid (Symbol) (defaults to: :agni)

    (optional) The grid to connect to (:agni or :aditi)



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/network.rb', line 196

def (first, last, pass, start="last", grid=:agni)
	client = XMLRPC::Client.new2(GRIDS[grid])
	client.http_header_extra = {"Content-Type" => "text/xml"}
	
	os = "Win"
	os = "Mac" if RUBY_PLATFORM.downcase.include?("darwin")
	os = "Lin" if RUBY_PLATFORM.downcase.include?("linux")

	res = client.call("login_to_simulator", {
		"first" => first,
		"last" => last,
		"passwd" => "$1$" + Digest::MD5.hexdigest(pass),
		"start" => start,
		"channel" => "libsl",
		"version" => "1.0",
		"platform" => os,
		"mac" => 6.times.map{sprintf("%02x", rand(255))}.join(":"),
		"options" => [],
		"id0" => LLUUID.new.to_s,
		"agree_to_tos" => "true",
		"read_critical" => "true",
		"viewer_digest" => "00000000-0000-0000-0000-000000000000"})

	unless res["login"] == "true"
		case res["reason"]
		when "presence" then raise SessionNotTimedoutError, res["message"]
		when "update" then raise RequiredUpdateError, res["message"]
		when "optional" then raise OptionalUpdateError, res["message"]
		when "key" then raise LoginFailureError, res["message"]
		when "ban" then raise AccountBannedError, res["message"]
		else raise LoginError, res["message"]
		end
	end

	@circuit_code = LLU32.new(res["circuit_code"])
	@session_id = LLUUID.new res["session_id"]
	@secure_session_id = LLUUID.new res["secure_session_id"]
	@agent_id = LLUUID.new res["agent_id"]
	@first_name = res["first_name"]
	@last_name = res["last_name"]
	@message_of_the_day = res["message"]
	setup_handlers
	connect_to_simulator res["sim_ip"], res["sim_port"]
end

#logoutObject

Logout the client and disconnect from each connected simulator



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/network.rb', line 242

def logout
	EventManager::register_handler(EventHandler.new(Proc.new do |type, sim|
		sim.client.network_manager.sims.delete sim
	end, :disconnected))

	EventManager::register_handler(EventHandler.new(Proc.new do |type, packet, sim|
		sim.client.network_manager.sims.each { |sim|
			sim.disconnect
		}
		EventManager::fire_event(:logout)
	end, :LogoutReplyPacket_Received))
	packet = LogoutRequestPacket.new({
		:AgentData => {
			:AgentID => @agent_id,
			:SessionID => @session_id
		}
	})
	send_packet packet, true
end

#send_packet(packet, reliable = false, sim = nil) ⇒ Object

Send a packet to a specific simulator

Parameters:

  • packet (Packet)

    The packet to send

  • reliable (Bool) (defaults to: false)

    (optional) Whether to send the packet reliably

  • sim (Simulator) (defaults to: nil)

    (optional) The sim to send the packet to (or the active sim)



300
301
302
303
# File 'lib/network.rb', line 300

def send_packet(packet, reliable=false, sim=nil)
	sim = @active_sim if sim.nil?
	@active_sim.send_packet packet, reliable
end

#setup_handlersObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/network.rb', line 262

def setup_handlers()
	# PacketAck handler
	EventManager::register_handler(EventHandler.new(Proc.new do |type, packet, sim|
		packet.Packets.each{|b| sim.packets_sent_reliably.delete(b.ID.value)}
	end, :PacketAckPacket_Received))
	# Handle acks on received packets
	EventManager::register_handler(EventHandler.new(Proc.new do |type, packet, sim|
		packet.acks.each{|ack| sim.packets_sent_reliably.delete(ack)}
	end, :PacketReceived))
	# Ready handler
	@ready_handler = EventManager::register_handler(EventHandler.new(Proc.new do |type, sim|
		EventManager::remove_handler(sim.client.network_manager.ready_handler)
		EventManager::fire_event(:ready, sim.client)
	end, :movement_complete))
end