livedata 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -0,0 +1,6 @@
1
+ * v0.2.2
2
+ 1. New method set_read_time in User class.
3
+ 2. handle read & write locking in User class.
4
+ 3. write_json and read_json methods are removed from the User class.
5
+ 4. Default ReadTime is set to 30 seconds.
6
+ 5. handle the user list in group
@@ -50,8 +50,10 @@ module LiveData
50
50
 
51
51
  # Add user to the group
52
52
  def add_user( user )
53
- @users.push( user )
54
- user.groups.push( self )
53
+ if( !@users.include?(user))
54
+ @users.push( user )
55
+ user.groups.push( self )
56
+ end
55
57
  end
56
58
 
57
59
  # Delete user from the group
@@ -3,18 +3,24 @@ module LiveData
3
3
  class User
4
4
 
5
5
  IntegerPackCode = "I"
6
+ ReadTime = 30
6
7
 
7
8
  attr :groups, :name
8
9
 
9
10
  # Create a user object
10
11
  def initialize( name = nil, channel = nil )
11
- @name = name || self
12
- @channel = channel
13
- @lock = Mutex.new
12
+ @name = name || self
13
+ @channel = channel
14
+ @read_time = ReadTime
15
+ @lock = Mutex.new
14
16
  @read_pipe, @write_pipe = IO.pipe
15
17
  @groups = []
16
18
  end
17
19
 
20
+ def set_read_time( time )
21
+ @read_time = time
22
+ end
23
+
18
24
  # Reset the write pipe and read pipe
19
25
  def reset
20
26
  begin
@@ -28,33 +34,43 @@ module LiveData
28
34
  # Clean the Contain in the pipe
29
35
  def clean
30
36
  begin
31
- while( @read_pipe.read_nonblock( 10000 ) )
32
- end
37
+ @lock.synchronize {
38
+ while( @read_pipe.read_nonblock( 10000 ) )
39
+ end
40
+ }
33
41
  rescue => err
34
42
  end
35
43
  end
36
44
 
37
- # Read json contain
38
- def read_json
39
- @read_pipe.gets()
40
- end
41
-
42
45
  # Read yaml contain
43
46
  def read_yaml
44
- len, etc = @read_pipe.read(4).unpack( IntegerPackCode )
45
- @read_pipe.read( len )
47
+ if( ioarrays = IO.select( [@read_pipe], [], [], @read_time ) )
48
+ if( ioarrays[0].include? @read_pipe )
49
+ @lock.synchronize {
50
+ tcont = @read_pipe.read_nonblock(4)
51
+ if( tcont and tcont.size == 4 )
52
+ len, etc = tcont.unpack( IntegerPackCode )
53
+ return @read_pipe.read_nonblock( len )
54
+ else
55
+ return nil
56
+ end
57
+ }
58
+ else
59
+ return nil
60
+ end
61
+ else
62
+ return nil
63
+ end
46
64
  end
47
65
 
48
66
  # read a Object
49
67
  def read
50
- YAML.load( read_yaml )
51
- end
52
-
53
- # Write a string, which contain json format
54
- # ==== Parameters
55
- # * +json_data+ - json string
56
- def write_json( json_data )
57
- @write_pipe.write( json_data + "\n" )
68
+ cont = read_yaml
69
+ if( cont )
70
+ return YAML.load( cont )
71
+ else
72
+ return nil
73
+ end
58
74
  end
59
75
 
60
76
  # Write a string, which contain yam format
@@ -62,9 +78,11 @@ module LiveData
62
78
  # * +yaml_data+ - yaml string
63
79
  def write_yaml( yaml_data )
64
80
  return unless yaml_data and yaml_data.class == String
65
- len = [ yaml_data.length ].pack( IntegerPackCode )
66
- @write_pipe.write( len )
67
- @write_pipe.write( yaml_data )
81
+ len = [ yaml_data.length ].pack( IntegerPackCode )
82
+ @lock.synchronize {
83
+ @write_pipe.write( len )
84
+ @write_pipe.write( yaml_data )
85
+ }
68
86
  end
69
87
 
70
88
  # Write a Object
@@ -79,10 +97,10 @@ module LiveData
79
97
  @groups.dup.each{|grp|
80
98
  grp.remove_user( self )
81
99
  }
82
- if( @channel )
83
- @channel.users.delete( @name )
84
- @channel.user_in_groups.delete( @name )
85
- end
100
+ if( @channel )
101
+ @channel.users.delete( @name )
102
+ @channel.user_in_groups.delete( @name )
103
+ end
86
104
  begin
87
105
  @read_pipe.close
88
106
  @write_pipe.close
@@ -1,4 +1,4 @@
1
1
 
2
2
  module LiveData
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
data/lib/live_data.rb CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'thread'
4
4
  require 'yaml'
5
- require 'json'
6
5
 
7
6
  require 'live_data/version'
8
7
  require 'live_data/channel'
@@ -60,8 +60,14 @@ class LiveDataTest < Test::Unit::TestCase
60
60
  data = { :title => "Greating", :message => "Welcome you all" }
61
61
  data1 = { :title => "Greating1", :message => "Welcome you all" }
62
62
  data2 = { :title => "Greating2", :message => "Welcome you all" }
63
+
63
64
  grp.add_user( guest1 )
65
+ assert grp.users.size == 1
66
+ grp.add_user( guest1 ) # Duplicate user
67
+ assert grp.users.size == 1
68
+
64
69
  grp.add_user( guest2 )
70
+ assert grp.users.size == 2
65
71
 
66
72
  guest1.write( data )
67
73
  assert data == guest1.read
@@ -77,4 +83,14 @@ class LiveDataTest < Test::Unit::TestCase
77
83
  assert data2 == guest2.read
78
84
 
79
85
  end
86
+
87
+ def test_read_time
88
+ chat = LiveData.create_channel( 'chat' )
89
+ guest1 = chat.create_user( 'guest1' )
90
+ guest1.set_read_time( 2 )
91
+ assert guest1.read == nil
92
+
93
+ guest1.write( "hai" )
94
+ assert guest1.read == "hai"
95
+ end
80
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livedata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohammed Siddick. E
@@ -9,7 +9,7 @@ autorequire: live_data
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-13 00:00:00 +05:30
12
+ date: 2010-02-15 00:00:00 +05:30
13
13
  default_executable:
14
14
  dependencies: []
15
15