merona 1.1.6 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ require 'win32api'
2
+
3
+ $t = Win32API.new("kernel32", "GetTickCount", nil, 'L')
4
+ $t.call()
5
+
6
+ class PingHandler < Handler
7
+ def recv(server,connection,packet)
8
+ case packet.id
9
+ when Protocol::PING
10
+ connection.send packet
11
+ end
12
+ end
13
+ end
@@ -10,3 +10,7 @@
10
10
  [2013:2:27:8:27:58] [LOG] : quit by user
11
11
  [2013:2:27:8:29:30] [LOG] : Merona engine started
12
12
  [2013:2:27:8:29:31] [LOG] : quit by user
13
+ [2013:2:27:18:44:23] [LOG] : Merona engine started
14
+ [2013:2:27:18:44:25] [LOG] : quit by user
15
+ [2013:2:27:18:44:40] [LOG] : Merona engine started
16
+ [2013:2:27:18:44:42] [LOG] : quit by user
@@ -1,6 +1,9 @@
1
1
  # Merona main
2
2
 
3
3
  import 'merona'
4
+
5
+ import 'handler/*'
6
+ import 'protocol/*'
4
7
  import 'config/*'
5
8
 
6
9
  begin
@@ -19,6 +22,7 @@ begin
19
22
  test.add_handler ChatHandler
20
23
  test.add_handler PubsubHandler
21
24
  test.add_handler SyncHandler
25
+ test.add_handler PingHandler
22
26
  end
23
27
 
24
28
  rescue
@@ -1,20 +1,22 @@
1
1
  # Merona entry
2
2
 
3
3
  require 'rubygems'
4
+ require 'fileutils'
4
5
 
5
- meorna_path = Gem::bin_path('merona', 'merona')
6
- meorna_path.reverse!
7
- meorna_path = path.split("/", 3)[2]
8
- meorna_path.reverse!
9
- meorna_path += "/lib"
10
-
11
- $LOAD_PATH << meorna_path
12
- $LOAD_PATH << File.dirname(File.expand_path(__FILE__))
6
+ FileUtils.cd(File.dirname(File.expand_path(__FILE__)), :verbose => true)
13
7
 
8
+ $merona_path = Gem::bin_path('merona', 'merona')
9
+ $merona_path.reverse!
10
+ $merona_path = $merona_path.split("/", 3)[2]
11
+ $merona_path.reverse!
12
+ $merona_path += "/lib"
13
+
14
+ $LOAD_PATH << $merona_path
15
+ $LOAD_PATH << "."
14
16
 
15
17
  def import(_path)
16
18
  if _path[_path.length-1] == '*'
17
- path = meorna_path + _path
19
+ path = $merona_path + "/" + _path
18
20
  Dir[path[0..path.length-1] + "*.rb"].each {|file| require file }
19
21
  path = "./" + _path
20
22
  Dir[path[0..path.length-1] + "*.rb"].each {|file| require file }
@@ -0,0 +1,4 @@
1
+ # 1400 ~ 1499
2
+ class Protocol
3
+ PING = 1
4
+ end
@@ -6,10 +6,10 @@ class Channel
6
6
  end
7
7
 
8
8
  def subscribe(client)
9
- @clients.push name
9
+ @clients.push client
10
10
  end
11
11
  def unsubscribe(client)
12
- @clients.delete name
12
+ @clients.delete client
13
13
 
14
14
  if @clients.size == 0
15
15
  return nil
@@ -5,6 +5,12 @@ class ChannelPool
5
5
  def dispose
6
6
  end
7
7
 
8
+ # ä�� �̸��� ��ūȭ�ؼ� �ֻ��� ������ ä�ο� ������ �� ����
9
+ # �ڵ� ������ �����Ų��.
10
+ #
11
+ # ex ) "ch.map.party"
12
+ # "ch.map"
13
+ # "ch"
8
14
  def tokenize(name, &block)
9
15
  token = name.split(".")
10
16
  if token.size > 1
@@ -18,17 +24,20 @@ class ChannelPool
18
24
  end
19
25
  end
20
26
 
27
+ # ������ �̸��� ä���� �����Ѵ�
21
28
  def subscribe(name, client)
22
29
  tokenize(name) do |t|
23
30
  subscribe t, client
24
31
  end
25
32
 
26
33
  if @channel[name] == nil
27
- @channel[name] = Array.new
34
+ @channel[name] = Channel.new
28
35
  end
29
36
 
30
- @channel[name].push client
37
+ @channel[name].subscribe client
31
38
  end
39
+
40
+ # ������ �̸��� ä���� ������ �����Ѵ�
32
41
  def unsubscribe(name, client)
33
42
  tokenize(name) do |t|
34
43
  unsubscribe t, client
@@ -36,21 +45,25 @@ class ChannelPool
36
45
 
37
46
  return if @channel[name] == nil
38
47
 
39
- @channel[name].delete client
40
-
41
- if @channel[name].size == 0
42
- @channel[name] = nil
48
+ if not @channel[name].unsubscribe(client)
49
+ @channel[name].delete name
43
50
  end
44
51
  end
45
- def publish(name, packet)
46
- tokenize(name) do |t|
47
- publish t, packet
52
+
53
+ # ������ �̸��� ä�ο� ��Ŷ�� �����Ѵ�
54
+ #
55
+ # to_super ���ڰ� true�� ��� ���� ä�� ��ο��� ��Ŷ�� ����
56
+ def publish(name, packet, to_super = false)
57
+ if to_super == true
58
+ tokenize(name) do |t|
59
+ publish t, packet
60
+ end
48
61
  end
49
62
 
50
63
  return if @channel[name] == nil
51
64
 
52
65
  @channel[name].each do |client|
53
- client.send packet
66
+ client.publish packet
54
67
  end
55
68
  end
56
69
  end
@@ -4,15 +4,16 @@ class Packet
4
4
  attr_accessor :data
5
5
  attr_accessor :ch
6
6
 
7
- def initialize
7
+ def initialize(id = 0, ch = "*")
8
8
  @data = {}
9
9
 
10
- @id = -1
11
- @ch = "*"
10
+ @id = id
11
+ @ch = ch
12
12
  end
13
13
  def dispose
14
14
  end
15
15
 
16
+ # ��Ŷ�� �����͸� �ִ´�
16
17
  def push(name,data)
17
18
  if name == nil
18
19
  return
@@ -20,6 +21,8 @@ class Packet
20
21
 
21
22
  @data[name] = data
22
23
  end
24
+
25
+ # ��Ŷ���� �����͸� �����´�
23
26
  def get(name)
24
27
  if name == nil
25
28
  return
@@ -28,24 +31,23 @@ class Packet
28
31
  @data[name]
29
32
  end
30
33
 
34
+ # ��Ŷ�� �����͸� �ִ´�
31
35
  def []=(name,data)
32
36
  push(name, data)
33
37
  end
38
+
39
+ # ��Ŷ���� �����͸� �����´�
34
40
  def [](name)
35
41
  get(name)
36
42
  end
37
43
 
44
+ # ��Ŷ�� ������ ä���� �����Ѵ�
38
45
  def ch=(ch)
39
46
  @ch = ch
40
47
  end
48
+
49
+ # ��Ŷ�� ä�� ���� �����´�
41
50
  def ch
42
51
  @ch
43
52
  end
44
-
45
- def type=(type)
46
- @data["type"] = type
47
- end
48
- def type
49
- @data["type"]
50
- end
51
53
  end
@@ -28,9 +28,9 @@ class ProcessPool
28
28
  handler.recv(@server, item.sender, item.packet)
29
29
  end
30
30
  end
31
-
32
- @worker.push thread
33
31
  end
32
+
33
+ @worker.push thread
34
34
  end
35
35
  end
36
36
  def kill
@@ -6,6 +6,7 @@ class Connection < EM::Connection
6
6
  attr_accessor :id
7
7
  attr_reader :ip, :port
8
8
 
9
+ # ������ �ʱ�ȭ�� �� �ҷ����� �޼ҵ�
9
10
  def initialize(*args)
10
11
  @port, @ip = Socket.unpack_sockaddr_in(get_peername)
11
12
  @server = args[0]
@@ -18,6 +19,7 @@ class Connection < EM::Connection
18
19
 
19
20
  set_sock_opt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, CONNECTION_NODELAY)
20
21
  end
22
+ # ������ ������ �� �ҷ����� �޼ҵ�
21
23
  def unbind
22
24
  @server.clients.delete self
23
25
  @db.dispose
@@ -26,17 +28,26 @@ class Connection < EM::Connection
26
28
  disconnect
27
29
  end
28
30
 
31
+ # ���� Ŭ���̾�Ʈ�� ��Ŷ�� �����Ѵ�
32
+ #
33
+ # packet : ���� ��Ŷ
29
34
  def send(packet)
30
35
  send_object packet
31
36
  end
37
+
38
+ # ���� Ŭ���̾�Ʈ�� ��Ŷ�� �޾��� �� �ҷ����� �޼ҵ�
39
+ #
40
+ # obj : ���ŵ� ��Ŷ
32
41
  def receive_object(obj)
33
- p obj
34
42
  @server.process.enqueue(self, obj)
35
43
  end
36
44
 
45
+
46
+ # ������ �����Ǿ��� �� �ҷ����� �޼ҵ�
37
47
  def connect
38
48
  Log.output("new connection from " + @ip)
39
49
  end
50
+ # ������ ����� �� �ҷ����� �޼ҵ�
40
51
  def disconnect
41
52
  Log.output("lost connection " + @ip)
42
53
  end
@@ -8,6 +8,11 @@ class Server
8
8
 
9
9
  attr_accessor :clients
10
10
 
11
+ # ������ �����Ѵ�
12
+ #
13
+ # name : ������ �̸�
14
+ # port : ������ ��Ʈ
15
+ # klass : Connection �ڵ鷯 Ŭ����
11
16
  def initialize(name,port,klass=Connection)
12
17
  EventMachine.start_server("localhost", port, klass, self)
13
18
 
@@ -23,13 +28,22 @@ class Server
23
28
 
24
29
  $server[@name] = self
25
30
  end
31
+
32
+ # ������ �ڿ��� �����Ѵ�
26
33
  def dispose
27
34
  $server.delete @name
28
35
  end
29
36
 
37
+ # ������ �ڵ鷯�� �߰��Ѵ�
38
+ #
39
+ # handler : �߰��� �ڵ鷯 Ŭ����
30
40
  def add_handler(handler)
31
41
  @handler.push handler.new
32
42
  end
43
+
44
+ # ������ ��ϵ� �ڵ鷯�� �����Ѵ�
45
+ #
46
+ # handler : ������ �ڵ鷯 Ŭ����
33
47
  def delete_handler(handler)
34
48
  @handler.each do |value|
35
49
  if value.class == handler
@@ -38,9 +52,17 @@ class Server
38
52
  end
39
53
  end
40
54
 
55
+ # �ش� ������ ������ Ŭ���̾�Ʈ ����� ���´�
56
+ #
57
+ # connection : ������ ����
41
58
  def exclude(connection)
42
59
  @clients.reject { |c| connection == c }
43
60
  end
61
+
62
+ # ��� Ŭ���̾�Ʈ���� ��Ŷ�� ��ε�ij�����Ѵ�
63
+ #
64
+ # packet : ��ε�ij������ ��Ŷ
65
+ # exclusion : ������ Ŭ���̾�Ʈ
44
66
  def broadcast(packet, exclusion = nil)
45
67
  (exclusion ? exclude(exclusion) : @clients).each do |client|
46
68
  client.send packet
@@ -0,0 +1,42 @@
1
+ require 'eventmachine'
2
+
3
+ load '../lib/packet/packet.rb'
4
+ load '../lib/protocol/pubsub_protocol.rb'
5
+ load '../lib/protocol/sync_protocol.rb'
6
+
7
+ load 'test_util.rb'
8
+
9
+ require 'Win32API'
10
+
11
+ $t = Win32API.new("kernel32", "GetTickCount", nil, 'L')
12
+ $t.call()
13
+
14
+ class Connection < EM::Connection
15
+ include EM::P::ObjectProtocol
16
+
17
+ def initialize(*args)
18
+ p = Packet.new
19
+ p.id = Protocol::SUBSCRIBE
20
+ send_object p
21
+
22
+ p.id = 1
23
+
24
+ 100.times do
25
+ p["ping"] = $t.call()
26
+ send_object p
27
+ p "send " + ($t.call() - p["ping"]).to_s
28
+ end
29
+ end
30
+ def unbind
31
+ end
32
+
33
+ def receive_object(obj)
34
+ p "reply " + ($t.call() - obj["ping"]).to_s
35
+ end
36
+ end
37
+
38
+ EM::run do
39
+ EM::connect "localhost", 9916, Connection
40
+ end
41
+
42
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merona
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -80,7 +80,6 @@ files:
80
80
  - lib/handler/sync_handler.rb
81
81
  - lib/handler/user_handler.rb
82
82
  - lib/merona.rb
83
- - lib/packet/chat_packet.rb
84
83
  - lib/packet/packet.rb
85
84
  - lib/process/process_pool.rb
86
85
  - lib/protocol/chat_protocol.rb
@@ -97,12 +96,15 @@ files:
97
96
  - frame/config/config_server.rb
98
97
  - frame/config/config_sharedmem.rb
99
98
  - frame/database/account.db
99
+ - frame/handler/ping_handler.rb
100
100
  - frame/log/2013.2.27.log
101
101
  - frame/main.rb
102
102
  - frame/merona.rb
103
+ - frame/protocol/ping_protocol.rb
103
104
  - frame/run.bat
104
105
  - tests/test_chat.rb
105
106
  - tests/test_connect.rb
107
+ - tests/test_ping.rb
106
108
  - tests/test_sqlite.rb
107
109
  - tests/test_util.rb
108
110
  - bin/merona
@@ -1,26 +0,0 @@
1
- class ChatPacket < Packet
2
- def initialize
3
- super
4
- @id = Protocol::CHAT
5
- end
6
-
7
- def msg=(msg)
8
- @data["msg"] = msg
9
- end
10
- def msg
11
- @data["msg"]
12
- end
13
-
14
- def sender=(id)
15
- @data["sender"] = id
16
- end
17
- def sender
18
- @data["sender"]
19
- end
20
- def receiver(id)
21
- @data["receiver"] = id
22
- end
23
- def receiver
24
- @data["receiver"]
25
- end
26
- end