Class: IONIS::Netsoul

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

Instance Method Summary collapse

Constructor Details

#initializeNetsoul

Instance of Netsoul



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ionisnetsoul.rb', line 13

def initialize
  @tmpDir = File.join Dir.tmpdir, 'ionis_auth'
  Dir.mkdir @tmpDir if !File.directory? @tmpDir
  @tmpDir = File.join Dir.tmpdir, 'ionis_auth/nsUser'
  if !File.exist? @tmpDir or (File.mtime(@tmpDir).to_i + 300) < Time.now.to_i 
    begin
      hSock = TCPSocket.open('ns-server.epita.fr', 4242)
      line = hSock.gets
      if /salut (.*)/.match(line)
        hSock.write "list_users\n"
        @usrData = Hash.new
        while line = hSock.gets
          line.strip!
          break if line == 'rep 002 -- cmd end'
          line = line.split ' '
          @usrData[line[1]] = Hash.new
          @usrData[line[1]]['login'] = line[1]
          @usrData[line[1]]['ip'] = line[2]
          line[10] = line[10].split ':'
          @usrData[line[1]]['state'] = line[10][0]
          @usrData[line[1]]['since'] = line[10][1]
        end
      end
      hSock.close
    rescue SocketError
      raise IONIS::Exception::HostNotFound.new 405, "Can't resolve ns-server.epita.fr"
    rescue Errno::ECONNREFUSED
      raise IONIS::Exception::ConnectionRefused.new 403, "Connection refused to ns-server.epita.fr"
    end
    File.open(@tmpDir, "wb") {|f| Marshal.dump(@usrData, f)}
  else
    @usrData =  File.open(@tmpDir, "rb") {|f| Marshal.load(f)}
  end
end

Instance Method Details

#getAllOnlineUsersHash

Get informations about all connected user

Returns:

  • (Hash)

    Information about given user in Array. If no-one was connected, the returned array will be empty



64
65
66
67
# File 'lib/ionisnetsoul.rb', line 64

def getAllOnlineUsers
  return false if !@usrData
  @usrData[]
end

#getUser(login) ⇒ Hash

Get informations about given connected user

Parameters:

  • login (String)

    IONIS Login

Returns:

  • (Hash)

    Information about given user in Array. If User doesn’t exist, the returned array will be empty



51
52
53
54
# File 'lib/ionisnetsoul.rb', line 51

def getUser 
  return false if !@usrData
  @usrData[]
end

#online?(login) ⇒ Boolean

Check if user is connected

Parameters:

  • login (String)

    IONIS Login

Returns:

  • (Boolean)

    True if user is Online, otherwize: false



72
73
74
75
76
# File 'lib/ionisnetsoul.rb', line 72

def online? 
  return false if !@usrData
  return true if @usrData[]
  false
end

#sizefixnum

Get number of connected user(s)

Returns:

  • (fixnum)

    Number of connected user(s)



58
59
60
# File 'lib/ionisnetsoul.rb', line 58

def size
  return @usrData.size
end