Module: Msn::Protocol

Includes:
EventMachine::Protocols::LineText2
Included in:
NotificationServer, Switchboard
Defined in:
lib/msn/protocol.rb

Overview

:nodoc:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



110
111
112
# File 'lib/msn/protocol.rb', line 110

def method_missing(name, *args)
  send_command name.upcase, *args
end

Instance Method Details

#answer_challenge(challenge_string) ⇒ Object



67
68
69
# File 'lib/msn/protocol.rb', line 67

def answer_challenge(challenge_string)
  send_payload_command "QRY", Msn::Challenge::ProductId, Msn::Challenge.challenge(challenge_string)
end

#clear_event(kind) ⇒ Object



106
107
108
# File 'lib/msn/protocol.rb', line 106

def clear_event(kind)
  @event_handlers.delete kind
end

#handle_event(header, data = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/msn/protocol.rb', line 56

def handle_event(header, data = nil)
  return unless @event_handlers

  handler = @event_handlers[header[0]]
  if handler
    Fiber.new do
      handler.call header, data
    end.resume
  end
end

#handle_normal_command(pieces) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/msn/protocol.rb', line 35

def handle_normal_command(pieces)
  if fiber = @command_fibers.delete(pieces[1].to_i)
    fiber.resume pieces
  else
    handle_event pieces
  end
end

#handle_payload_command(pieces) ⇒ Object



43
44
45
46
47
48
# File 'lib/msn/protocol.rb', line 43

def handle_payload_command(pieces)
  @header = pieces

  size = pieces.last.to_i
  set_binary_mode size
end

#on_event(kind, &block) ⇒ Object



101
102
103
104
# File 'lib/msn/protocol.rb', line 101

def on_event(kind, &block)
  @event_handlers ||= {}
  @event_handlers[kind] = block
end

#post_initObject



5
6
7
8
# File 'lib/msn/protocol.rb', line 5

def post_init
  @trid = 1
  @command_fibers = {}
end

#receive_binary_data(data) ⇒ Object



50
51
52
53
54
# File 'lib/msn/protocol.rb', line 50

def receive_binary_data(data)
  Msn::Messenger.log_info "<<* #{data}"

  handle_event @header, data
end

#receive_line(line) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/msn/protocol.rb', line 10

def receive_line(line)
  Msn::Messenger.log_info "<< #{line}"

  pieces = line.split(' ')

  case pieces[0]
  when 'CHL'
    answer_challenge pieces[2]
  when 'RNG'
    handle_event pieces
  when 'MSG', 'NOT', 'GCF', 'UBX'
    handle_payload_command pieces
  when 'QRY'
    # ignore
  when 'ADL', 'RML'
    if pieces[2] == 'OK'
      handle_normal_command pieces
    else
      handle_payload_command pieces
    end
  else
    handle_normal_command pieces
  end
end

#send_command(command, *args) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/msn/protocol.rb', line 71

def send_command(command, *args)
  @command_fibers[@trid] = Fiber.current

  text = "#{command} #{@trid} #{args.join ' '}\r\n"
  send_command_internal text

  Fiber.yield
end

#send_command_internal(text) ⇒ Object



94
95
96
97
98
99
# File 'lib/msn/protocol.rb', line 94

def send_command_internal(text)
  Msn::Messenger.log_info ">> #{text}"
  send_data text
  @trid += 1
  @trid - 1
end

#send_payload_command(command, *args) ⇒ Object



80
81
82
83
84
# File 'lib/msn/protocol.rb', line 80

def send_payload_command(command, *args)
  payload = args.pop
  args.push payload.length
  send_command_internal "#{command} #{@trid} #{args.join ' '}\r\n#{payload}"
end

#send_payload_command_and_wait(command, *args) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/msn/protocol.rb', line 86

def send_payload_command_and_wait(command, *args)
  @command_fibers[@trid] = Fiber.current

  send_payload_command command, *args

  Fiber.yield
end