Class: SmartFox::Client

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

Defined Under Namespace

Classes: ApiIncompatibleError, ConnectionFailureError, TransportTimeoutError

Constant Summary collapse

CLIENT_VERSION =
"1.5.8"
CONNECTION_TIMEOUT =
5
TRANSPORTS =
[ SmartFox::Socket::Connection, SmartFox::BlueBox::Connection ]
EVENTS =
[ :connected, :logged_in, :rooms_updated ]
HEADER_SYSTEM =
'sys'
HEADER_EXTENDED =
'xt'
ACTION_VERSION_CHECK =
'verChk'
ACTION_API_OK =
'apiOK'
ACTION_API_OBSOLETE =
'apiKO'
ACTION_LOGIN =
'login'
ACTION_LOGIN_OK =
'logOK'
ACTION_AUTO_JOIN =
'autoJoin'
ACTION_JOIN_ROOM =
'joinRoom'
ACTION_UPDATE_ROOMS =
'getRmList'
ACTION_ROOM_LIST =
'rmList'
ACTION_JOIN_OK =
'joinOK'
ACTION_JOIN_FAIL =
'joinKO'
EXTENDED_RESPONSE =
'xtRes'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smartfox/client.rb', line 37

def initialize(options = {})
  @room_list = {}
  @connected = false
  @buddy_list = []
  @user_id = options[:user_id]
  @user_name = options[:user_name]
  @server = options[:server] || 'localhost'
  @port = options[:port]
  @events = {}
  @rooms = {}
  @users = {}
end

Instance Attribute Details

#buddy_listObject (readonly)

Returns the value of attribute buddy_list.



10
11
12
# File 'lib/smartfox/client.rb', line 10

def buddy_list
  @buddy_list
end

#connectedObject (readonly) Also known as: connected?

Returns the value of attribute connected.



10
11
12
# File 'lib/smartfox/client.rb', line 10

def connected
  @connected
end

#current_roomObject (readonly)

Returns the value of attribute current_room.



11
12
13
# File 'lib/smartfox/client.rb', line 11

def current_room
  @current_room
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/smartfox/client.rb', line 10

def port
  @port
end

#room_listObject (readonly)

Returns the value of attribute room_list.



10
11
12
# File 'lib/smartfox/client.rb', line 10

def room_list
  @room_list
end

#serverObject (readonly)

Returns the value of attribute server.



10
11
12
# File 'lib/smartfox/client.rb', line 10

def server
  @server
end

#user_idObject

Returns the value of attribute user_id.



13
14
15
# File 'lib/smartfox/client.rb', line 13

def user_id
  @user_id
end

#user_nameObject

Returns the value of attribute user_name.



13
14
15
# File 'lib/smartfox/client.rb', line 13

def user_name
  @user_name
end

#usersObject (readonly)

Returns the value of attribute users.



11
12
13
# File 'lib/smartfox/client.rb', line 11

def users
  @users
end

Instance Method Details

#add_handler(event, &proc) ⇒ Object



86
87
88
89
# File 'lib/smartfox/client.rb', line 86

def add_handler(event, &proc)
  @events[event.to_sym] = [] unless @events[event.to_sym]
  @events[event.to_sym] << proc
end

#auto_joinObject



99
100
101
# File 'lib/smartfox/client.rb', line 99

def auto_join
  send_packet(HEADER_SYSTEM, ACTION_AUTO_JOIN)
end

#connectObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/smartfox/client.rb', line 50

def connect()
  unless @connected
    TRANSPORTS.each do |transport_class|
      begin
        started_at = Time.now
        @transport = transport_class.new(self)
        @transport.connect
        
        while not connected? and Time.now <= started_at + CONNECTION_TIMEOUT
          Thread.pass
        end
        
        return @transport if connected?
      rescue
      end
    end
    raise ConnectionFailureError.new "Could not negotiate any transport with server."
  end
end

#connect_succeededObject



95
96
97
# File 'lib/smartfox/client.rb', line 95

def connect_succeeded
  send_packet(HEADER_SYSTEM, ACTION_VERSION_CHECK, 0) { |x| x.ver(:v => CLIENT_VERSION.delete('.')) }
end

#disconnectObject



70
71
72
73
74
75
# File 'lib/smartfox/client.rb', line 70

def disconnect
  if @connected
    @transport.disconnect
    @connected = false
  end
end

#extended_command(action, data = nil, room = -1,, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/smartfox/client.rb', line 77

def extended_command(action, data = nil, room = -1, options = {})
  options[:format] ||= :xml
  
  case options[:format]
    when :xml
    send_packet(HEADER_EXTENDED, action, room) { |x| x.cdata!(data || '') }
  end
end

#join_room(room) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/smartfox/client.rb', line 103

def join_room(room)
  #CHECK: activeRoomId == -1 no room has already been entered
  if(@current_room == -1 || @current_room == nil)       
    leave_current_room = "0"
    room_to_leave = -1                
  else 
    leave_current_room = "1"
    room_to_leave = @current_room
  end
  send_packet(HEADER_SYSTEM, ACTION_JOIN_ROOM, room.id) { |x| 
    x.room(:id=>room.id,:pwd=>'',:spec=>'0',:leave=>leave_current_room,:old=>room_to_leave) 
  }
end

#login(zone, username, password = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/smartfox/client.rb', line 137

def (zone, username, password = nil)
  send_packet(HEADER_SYSTEM, ACTION_LOGIN) do |packet|
    packet.(:z => zone) do ||
      .nick { |nick| nick.cdata! username }
      .pword { |pword| pword.cdata! password || '' }
    end
  end
  Thread.pass
end

#packet_recieved(data) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/smartfox/client.rb', line 117

def packet_recieved(data)
  begin
    SmartFox::Logger.debug "SmartFox::Client#packet_recieved('#{data}')"
    packet = SmartFox::Packet.parse(data)     
    case packet.header
      when HEADER_SYSTEM
      if @connected
        handle_system_packet(packet)
      else
        (packet)
      end
      when HEADER_EXTENDED
      raise_event :extended_response, self, packet.data
    end
  rescue => e
    SmartFox::Logger.error "In SmartFox::Client#packet_received"
    SmartFox::Logger.exception e
  end
end

#parse_user(node) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/smartfox/client.rb', line 161

def parse_user(node)
  if user = @users[node['i'].to_i]
    user.parse(node)
  else
    @users[node['i'].to_i] = SmartFox::User.parse(node)
  end
end

#refresh_roomsObject



91
92
93
# File 'lib/smartfox/client.rb', line 91

def refresh_rooms
  send_packet(HEADER_SYSTEM, ACTION_UPDATE_ROOMS)
end

#send_extended(extension, action, options) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/smartfox/client.rb', line 147

def send_extended(extension, action, options)
  SmartFox::Logger.debug "send_extended #{extension},#{action},#{options}"    
  options[:format] ||= :xml
  options[:room] ||= (@current_room ? @current_room.id : 0)
  options[:parameters] ||= {}
  
  case options[:format]
    when :xml
    
  when :json
    send_extended_json_packet(extension, action, options[:room], options[:parameters])
  end
end