reminder-client 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6b36225cc57fa35c8e9dd456c6c489bc8e03ecf
4
+ data.tar.gz: 82c8267b349cfe3f15521bc0b42819b764a40836
5
+ SHA512:
6
+ metadata.gz: 5e67bc877e9de398c3b53bc6f6d6de4d0f0c025f20a5228075644ffb00d4d49eafe1a3eaf2c19e1a275c422773ce221e505d89a8994c945c0488208acd24545d
7
+ data.tar.gz: 860550792871e6991ecfa43d29caeeb4438982e9577a21967774a31b80a8529207ce13bbdc2d4814c9bc6b48592d5517e5520865e332a2cf9bc877fa1109aff4
@@ -0,0 +1,8 @@
1
+ require 'reminder-client'
2
+ require 'reminder-client/configcenter'
3
+
4
+ class ReminderClient < Zoo
5
+ def initialize(options = {})
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,101 @@
1
+ #!/usr/math/bin/ruby
2
+
3
+ require 'zookeeper'
4
+ require 'eventmachine'
5
+ require 'zk'
6
+
7
+ class Zoo
8
+ def initialize(options)
9
+ p "start connect to #{options[:zk_address]}"
10
+ @zk = ZK.new(options[:zk_address])
11
+ # @config_center_backend = options[:config_center_backend]
12
+
13
+ # @path = "/rbtest"
14
+ # default paramters
15
+ # @server_found_base_path = "/xserver"
16
+ # @server_config_base_path = "/xconfig"
17
+ # @server_config_sub_path= "/ctest"
18
+ # paramters to set
19
+ # @server_found_base_path = options[:zk_server_found_base_path]
20
+ # @server_config_base_path = options[:zk_server_config_base_path]
21
+ # @server_config_sub_path= options[:zk_server_config_sub_path]
22
+ puts "init succ #{@zk}"
23
+ end
24
+
25
+ # server found server part
26
+ # option temporarily has one param ip_port .eg "192.168.3.1:2222"
27
+ def server_found_server(option, child = 'default')
28
+ #fork do
29
+ # @zk.reopen()
30
+ re = @zk.create(option[:server_found_base_path] + "/" + child, option[:ip_port], :ephemeral => true, :sequence => true)
31
+ puts "create ephemeral path #{re} with data #{option} succ !!!"
32
+ #end
33
+ end
34
+
35
+ # server found client part
36
+ def server_found_client(server_path, &block)
37
+ puts "zk server_found start watching on #{server_path}"
38
+ relist = @zk.children(server_path, :watch => true)
39
+ # relist = @zk.children(server_path)
40
+ load_balance = @zk.get(server_path, :watch => true)[0]
41
+
42
+ puts "get children #{relist} and get load_balance #{load_balance} for the first time"
43
+
44
+ #context[:serverlist => relist]
45
+ block.call(get_ip_from_server(server_path, relist),load_balance)
46
+
47
+ node_subscription = @zk.register(server_path) do |event|
48
+ puts "server found event is #{event}"
49
+ # get children and re-set watch
50
+ relist = @zk.children(server_path, :watch => true)
51
+ # relist = @zk.children(server_path)
52
+ load_balance = @zk.get(server_path, :watch => true)[0]
53
+ if event.node_child? || event.node_changed?
54
+ #puts relist
55
+ block.call(get_ip_from_server(server_path, relist), load_balance)
56
+ end
57
+ end
58
+ end
59
+
60
+ def get_ip_from_server(server_path, relist)
61
+ list = []
62
+ relist.each { |child|
63
+ #puts "get child #{child}"
64
+ str = @zk.get(server_path + "/" + child)[0]
65
+ p str
66
+ list << str if str and !str.empty?
67
+ #p "lalalala -> #{@zk.get(server_path + "/" + child)}"
68
+ }
69
+ list.uniq
70
+ end
71
+
72
+ def server_config_client(config_path, service, group, &block)
73
+ puts "zk server_config start watching on #{config_path}"
74
+
75
+ # get server config for the first time
76
+ version = @zk.get(config_path, :watch => true)[0]
77
+ version = -1 if version.empty?
78
+ version = version.to_i
79
+
80
+ # call hessian to get the config data
81
+ configdata = REMINDER_BACKEND_SERVICE.getMapdata(version, service, group)
82
+ puts "get version for the first time configdata is #{configdata}"
83
+ block.call(configdata)
84
+
85
+
86
+ # register the config path
87
+ @zk.register(config_path) do |event|
88
+ puts "serve config event is #{event}"
89
+ version = @zk.get(config_path, :watch => true)[0]
90
+ if event.node_changed?
91
+ version = -1 if version.empty?
92
+ version = version.to_i
93
+ puts "server config version is #{version}"
94
+
95
+ # get the config data by version and service through hessian
96
+ configdata = REMINDER_BACKEND_SERVICE.getMapdata(version, service, group)
97
+ block.call(configdata)
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,57 @@
1
+ #require 'rubygems'
2
+ #require 'bundler'
3
+ #require 'zookeeper'
4
+ #require 'eventmachine'
5
+ require 'sinatra/base'
6
+
7
+
8
+ #get '/' do
9
+ #'Hello World11111!'
10
+ #end
11
+ #module App
12
+ class HelloApp < Sinatra::Base
13
+ # threaded - False: Will take requests on the reactor thread
14
+ # True: Will queue request for background thread
15
+ configure do
16
+ set :threaded, false
17
+ end
18
+
19
+ @@context = {}
20
+ @@list = [9,8,7]
21
+ #def initialize
22
+ #@@list = nil
23
+ #puts "initial1111"
24
+ #end
25
+
26
+ # Request runs on the reactor thread (with threaded set to false)
27
+ get '/hello' do
28
+ # "Hello World list -> #{@@list}"
29
+ "server_list -> #{$global_server_list}
30
+ <br>
31
+ <br>
32
+ config -> #{$global_idservice_thrift_config}
33
+ <br>
34
+ <br>
35
+ load_balance -> #{$load_balance}
36
+ "
37
+ #p self
38
+ end
39
+
40
+ # Request runs on the reactor thread (with threaded set to false)``
41
+ # and returns immediately. The deferred task does not delay the
42
+ # response from the web-service.
43
+ get '/delayed-hello' do
44
+ EM.defer do
45
+ sleep 5
46
+ end
47
+ p 'I\'m doing work in the background, but I am still free to take requests'
48
+ end
49
+
50
+ #def HelloApp.set_list1(param)
51
+ #p "call app set def -> #{param}"
52
+ #@@list = param
53
+
54
+ #p 'do callback'
55
+ #end
56
+ end
57
+ #end
@@ -0,0 +1,40 @@
1
+ #require 'rubygems'
2
+ #require 'bundler'
3
+ #require 'zookeeper'
4
+ #require 'eventmachine'
5
+ require 'sinatra/base'
6
+
7
+
8
+ class HelloApp < Sinatra::Base
9
+ # threaded - False: Will take requests on the reactor thread
10
+ # True: Will queue request for background thread
11
+ configure do
12
+ set :threaded, false
13
+ end
14
+
15
+ #def initialize
16
+ #@@list = nil
17
+ #puts "initial1111"
18
+ #end
19
+
20
+ # Request runs on the reactor thread (with threaded set to false)
21
+ get '/hello' do
22
+ # "Hello World list -> #{@@list}"
23
+ "server_list -> #{$global_server_list}
24
+ <br>
25
+ <br>
26
+ config -> #{$global_idservice_thrift_config}
27
+ "
28
+ #p self
29
+ end
30
+
31
+ # Request runs on the reactor thread (with threaded set to false)``
32
+ # and returns immediately. The deferred task does not delay the
33
+ # response from the web-service.
34
+ get '/delayed-hello' do
35
+ EM.defer do
36
+ sleep 5
37
+ end
38
+ p 'I\'m doing work in the background, but I am still free to take requests'
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ #require 'rubygems'
2
+ #require 'bundler'
3
+ #require 'zookeeper'
4
+ #require 'eventmachine'
5
+ require 'sinatra/base'
6
+
7
+
8
+ class HelloApp < Sinatra::Base
9
+
10
+
11
+ #def initialize
12
+ #@@list = nil
13
+ #puts "initial1111"
14
+ #end
15
+
16
+ get '/' do
17
+ "server_list -> #{$global_server_list}
18
+ <br>
19
+ <br>
20
+ config -> #{$global_idservice_thrift_config}
21
+ "
22
+ end
23
+
24
+ # Request runs on the reactor thread (with threaded set to false)
25
+ get '/hello' do
26
+ # "Hello World list -> #{@@list}"
27
+ "server_list -> #{$global_server_list}
28
+ <br>
29
+ <br>
30
+ config -> #{$global_idservice_thrift_config}
31
+ "
32
+ #p self
33
+ end
34
+
35
+ # Request runs on the reactor thread (with threaded set to false)``
36
+ # and returns immediately. The deferred task does not delay the
37
+ # response from the web-service.
38
+
39
+ end
@@ -0,0 +1,97 @@
1
+ require 'zookeeper'
2
+ require 'eventmachine'
3
+ require 'thin'
4
+ require 'zk'
5
+
6
+ require File.expand_path("../application",__FILE__)
7
+
8
+ class Zoo
9
+ #include EM::Deferrable
10
+ def initialize
11
+ @zk = ZK.new("localhost:2181")
12
+ @path = "/rbtest"
13
+ @server_found_base_path = "/xserver"
14
+ #@
15
+ puts "init succ #{@zk}"
16
+ end
17
+
18
+ # server found server part
19
+ # option temporarily has one param ip_port .eg "192.168.3.1:2222"
20
+ def serverfound_server(option, child = 'default')
21
+ #fork do
22
+ # @zk.reopen()
23
+ #end
24
+ Thread.new{
25
+ 1000.times do
26
+ re = @zk.create(@server_found_base_path + "/" + child, "192.168.2.2:#{rand(9999)}", :ephemeral => true, :sequence => true)
27
+ puts "create ephemeral path #{re} "
28
+ sleep(rand(5))
29
+ end
30
+ }
31
+ Thread.new{
32
+ 1000.times do
33
+ relist = @zk.children(@server_found_base_path)
34
+ re = @zk.delete(@server_found_base_path+"/"+relist.pop) if !relist.empty?
35
+ puts "delete ephemeral path #{re}"
36
+ sleep(rand(10))
37
+ end
38
+ }
39
+ end
40
+
41
+ # server found client part
42
+ def serverfound_client(conetext, &block)
43
+ puts "zk start watching on #{@server_found_base_path}"
44
+ relist = @zk.children(@server_found_base_path, :watch => true)
45
+ puts "get children #{relist} for the first time"
46
+
47
+ context[:serverlist => relist]
48
+
49
+ node_subscription = @zk.register(@server_found_base_path) do |event|
50
+ puts "server found event is #{event}"
51
+ if event.node_child?
52
+ # get children and re-set watch
53
+ relist = @zk.children(@server_found_base_path, :watch => true)
54
+ puts relist
55
+ end
56
+ end
57
+ # register watch (won't work!!!)
58
+ #@zk.stat(@server_found_base_path, watch: true)
59
+ end
60
+
61
+ def run
62
+ #p @zk.methods.sort
63
+ #@queue.pop
64
+ #p "lalala #{@queue}"
65
+
66
+ @zk.create(@path, "data", :sequence => true, :ephemeral => true)
67
+
68
+ node_subscription = @zk.register(@path) do |event|
69
+ p event
70
+ #p event.methods.sort
71
+ if event.node_deleted?
72
+ p ''
73
+ end
74
+
75
+ # re-register
76
+ @zk.stat(@path, :watch => true)
77
+ end
78
+
79
+ @zk.stat(@path, :watch => true)
80
+ end
81
+ end
82
+
83
+ #EM.run do
84
+ #EM.schedule do
85
+ zoo = Zoo.new
86
+ #if $zdo == '1'
87
+ rand(3).times do
88
+ zoo.serverfound_server(:ip_port => "192.168.2.2:#{rand(9999)}")
89
+ end
90
+ zoo.serverfound_server(:ip_port => nil)
91
+ #end
92
+ #if $zdo == '2'
93
+ #zoo.serverfound_client
94
+ #end
95
+ # end
96
+ #end
97
+ sleep(1000000)
@@ -0,0 +1,95 @@
1
+
2
+ list = ["22","22",nil,""]
3
+ list1 = []
4
+ list.each{|x|
5
+ list1 << x if x and !x.empty?
6
+ #p x if x
7
+ }
8
+ #p list1.uniq
9
+ #p list1
10
+
11
+ # symbols
12
+ #config = { :foo => 123, :bar => 456}
13
+ #p config[:foo]
14
+ #config[:foo] = 1
15
+ #p config[:foo]
16
+
17
+ # hash
18
+ #config = {"foo" => 123, "bar" => 456}
19
+ #p config["foo"]
20
+ #config["foo"] = 1
21
+ #p config["foo"]
22
+
23
+ # regular expressions
24
+ def test(xxx)
25
+ "Hi! "+xxx
26
+ end
27
+
28
+ p test("cr")
29
+
30
+ class Person
31
+ @@name = "cr"
32
+ def self.say
33
+ p @@name
34
+ end
35
+ def Person.say1
36
+ p @@name
37
+ end
38
+ end
39
+
40
+ Person.say1
41
+
42
+
43
+ #require 'hessian2'
44
+ # zoo_SERVICE = Hessian2::Client.new(config['hessian']['id_service'])
45
+ #zoo_SERVICE = Hessian2::Client.new("http://192.168.5.100:8080/reminder-backend/hessian/requestconfigdataservice")
46
+ #p zoo_SERVICE.getMapdata('server','version')
47
+ # http://192.168.5.100:8080/reminder-backend/hessian/requestconfigdataservice
48
+
49
+ class T
50
+ def getMapdata(str,s1)
51
+ {"xx_server" => "192.168.2.2"}
52
+ end
53
+ end
54
+ HES = T.new
55
+
56
+ p HES.getMapdata(1,2)
57
+
58
+ def my_sum(*val)
59
+ val.inject(0){ |sum,v| sum + v}
60
+ end
61
+
62
+ #p my_sum(1,2,3,4)
63
+
64
+ def my_hash(options)
65
+ options[:x]
66
+ end
67
+
68
+ p my_hash(:x => 1,:y => 2)
69
+
70
+ p '---------------'
71
+
72
+ # class DM
73
+ # ['a','b','c'].each do |x|
74
+ # define_method(x) {p x}
75
+ # end
76
+
77
+ # define_method :dme do |a = false|
78
+ # p a
79
+ # end
80
+ # end
81
+
82
+ # DM.new.dme
83
+
84
+
85
+ # require 'rubygems'
86
+ # require 'nokogirl'
87
+ # require 'open-uri'
88
+
89
+ # html = open("www.baidu.com").read
90
+ # p html
91
+
92
+ require 'hessian2'
93
+ Z_SERVICE = Hessian2::Client.new("http://192.168.5.100:8080/reminder-backend/hessian/requestconfigdataservice")
94
+
95
+ p Z_SERVICE.getMapdata(1, "xservice", "0")
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reminder-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - caorong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.9.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.9.3
27
+ description: reminder client
28
+ email: caorong@ximalaya.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/reminder-client.rb
34
+ - lib/reminder-client/configcenter.rb
35
+ - test/application.rb
36
+ - test/application1.rb
37
+ - test/application2.rb
38
+ - test/servertest.rb
39
+ - test/test.rb
40
+ homepage: http://www.ximalaya.com
41
+ licenses:
42
+ - MIT2.0
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.2.0
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A zookeeper client wrapper provide serverfound and serverconfig.
64
+ test_files:
65
+ - test/application1.rb
66
+ - test/test.rb
67
+ - test/servertest.rb
68
+ - test/application2.rb
69
+ - test/application.rb