Class: LiveData::User

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

Constant Summary collapse

IntegerPackCode =
"I"
ReadTime =
25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, channel = nil) ⇒ User

Create a user object



12
13
14
15
16
17
18
19
# File 'lib/live_data/user.rb', line 12

def initialize( name = nil, channel = nil )
   @name      = name || self
   @channel   = channel
   @read_time = ReadTime
   @lock      = Mutex.new
   @read_pipe, @write_pipe = IO.pipe
   @groups       = []
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



8
9
10
# File 'lib/live_data/user.rb', line 8

def groups
  @groups
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/live_data/user.rb', line 9

def name
  @name
end

Instance Method Details

#cleanObject

Clean the Contain in the pipe



36
37
38
39
40
41
42
43
44
# File 'lib/live_data/user.rb', line 36

def clean
   begin
 @lock.synchronize {
    while( @read_pipe.read_nonblock( 10000 ) )
    end
 }
   rescue => err
   end
end

#destroyObject

Destroy the user



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/live_data/user.rb', line 97

def destroy
   @groups.dup.each{|grp|
      grp.remove_user( self )
   }
   if( @channel )
     @channel.users.delete( @name )
     @channel.user_in_groups.delete( @name )
   end
   begin
      @read_pipe.close
      @write_pipe.close
   rescue => err
   end
end

#readObject

read a Object



68
69
70
71
72
73
74
75
# File 'lib/live_data/user.rb', line 68

def read
   cont = read_yaml
   if( cont )
      return YAML.load( cont )
   else
      return nil
   end
end

#read_yamlObject

Read yaml contain



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/live_data/user.rb', line 47

def read_yaml
   if( ioarrays = IO.select( [@read_pipe], [], [], @read_time ) )
      if( ioarrays[0].include? @read_pipe )
         @lock.synchronize {
            tcont = @read_pipe.read_nonblock(4)
            if( tcont and tcont.size == 4 )
               len, etc = tcont.unpack( IntegerPackCode )
               return @read_pipe.read_nonblock( len )
            else
               return nil
            end
         }
      else
         return nil
      end
   else
      return nil
   end
end

#resetObject

Reset the write pipe and read pipe



26
27
28
29
30
31
32
33
# File 'lib/live_data/user.rb', line 26

def reset
   begin
      @write_pipe.close
      @read_pipe.close
   rescue => err
   end
   @read_pipe, @write_pipe = IO.pipe
end

#set_read_time(time) ⇒ Object



21
22
23
# File 'lib/live_data/user.rb', line 21

def set_read_time( time )
   @read_time = time
end

#write(data) ⇒ Object

Write a Object

Parameters

  • data - any Object



92
93
94
# File 'lib/live_data/user.rb', line 92

def write( data )
   write_yaml( data.to_yaml )
end

#write_yaml(yaml_data) ⇒ Object

Write a string, which contain yam format

Parameters

  • yaml_data - yaml string



80
81
82
83
84
85
86
87
# File 'lib/live_data/user.rb', line 80

def write_yaml( yaml_data )
   return unless yaml_data and yaml_data.class == String
   len = [ yaml_data.length ].pack( IntegerPackCode )
   @lock.synchronize {
      @write_pipe.write( len )
      @write_pipe.write( yaml_data )
   }
end