livedata 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +6 -0
- data/lib/live_data/group.rb +4 -2
- data/lib/live_data/user.rb +45 -27
- data/lib/live_data/version.rb +1 -1
- data/lib/live_data.rb +0 -1
- data/test/live_data_test.rb +16 -0
- metadata +2 -2
data/CHANGELOG
CHANGED
data/lib/live_data/group.rb
CHANGED
@@ -50,8 +50,10 @@ module LiveData
|
|
50
50
|
|
51
51
|
# Add user to the group
|
52
52
|
def add_user( user )
|
53
|
-
|
54
|
-
|
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
|
data/lib/live_data/user.rb
CHANGED
@@ -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
|
12
|
-
|
13
|
-
|
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
|
-
|
32
|
-
|
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
|
-
|
45
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
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
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
data/lib/live_data/version.rb
CHANGED
data/lib/live_data.rb
CHANGED
data/test/live_data_test.rb
CHANGED
@@ -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.
|
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-
|
12
|
+
date: 2010-02-15 00:00:00 +05:30
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|