Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
259 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module EMBox | ||
|
||
module Client | ||
|
||
class Agent < BasicObject | ||
|
||
def initialize client | ||
@client = client | ||
end | ||
|
||
def think | ||
#$stderr.puts agent.look_around | ||
#$stderr.puts agent.move_to 1, 0 | ||
end | ||
|
||
def says text | ||
echo("client: #{text}") | ||
end | ||
|
||
def method_missing method, *args, &block | ||
$stderr.puts method, args | ||
@client.send_message self, method, *args, &block | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'fiber' | ||
|
||
require_relative 'server_connection' | ||
#require_relative '../sandbox/sandbox' | ||
|
||
module EMBox | ||
|
||
module Client | ||
|
||
class Base | ||
|
||
attr_reader :fiber, :agent, :connection | ||
|
||
def initialize | ||
$stderr.puts "agent #{self.class} initialized" | ||
start | ||
end | ||
|
||
def start | ||
$stderr.puts 'starting client' | ||
EM.run do | ||
begin | ||
@connection = EM::attach($stdin, ServerConnection) | ||
@connection.client = self | ||
|
||
@agent = Agent.new(self) # TODO create specific Agent | ||
#start_agent | ||
|
||
EM.add_timer(2) do | ||
EM.stop | ||
end | ||
rescue Exception => e | ||
$stderr.puts e.message | ||
@server.send_object :exception => e.message | ||
EM.stop | ||
end | ||
end | ||
end | ||
|
||
def start_agent | ||
@fiber = Fiber.new do | ||
begin | ||
agent.think | ||
rescue Exception => e | ||
$stderr.puts e.message | ||
$stderr.puts *e.backtrace.join("\n") | ||
end | ||
end | ||
@fiber.resume | ||
end | ||
|
||
def send_message caller, method, *args, &block | ||
if allowed? caller, method | ||
@connection.send_message method, *args | ||
$stderr.puts Fiber.current | ||
Fiber.yield rescue FiberError | ||
end | ||
end | ||
|
||
def allowed? caller, method | ||
true | ||
end | ||
|
||
def receive_message object | ||
if object['return_value'] | ||
@fiber.resume object['return_value'] | ||
else | ||
#raise self.inspect | ||
send object['message'], *object['arguments'] | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'fiber' | ||
|
||
require_relative 'base' | ||
|
||
require_relative 'server_connection' | ||
#require_relative '../sandbox/sandbox' | ||
|
||
module EMBox | ||
|
||
module Client | ||
|
||
class SynchronizedClient < Base | ||
|
||
attr_reader :fiber | ||
|
||
def start | ||
@fiber = Fiber.new do | ||
super | ||
end | ||
@fiber.resume | ||
end | ||
|
||
def receive_message object | ||
if object['return_value'] | ||
@fiber.resume object['return_value'] | ||
else | ||
super | ||
end | ||
end | ||
|
||
protected | ||
|
||
def send_to_connection method, *args | ||
super | ||
Fiber.yield | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Example | ||
module Echo | ||
class Agent < EMBox::Client::Agent | ||
|
||
# used for test without return value | ||
def says text | ||
answer "client: #{text}" | ||
end | ||
|
||
# used for test with return value | ||
def think | ||
answer = echo "client is thinking" | ||
echo answer | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Example | ||
module Echo | ||
class Server | ||
include EMBox::HasConnection | ||
include EMBox::HasClient | ||
|
||
delegates_to_client :says | ||
|
||
# used for test without return value | ||
def answer text | ||
puts "received #{text} from client" | ||
received_message text | ||
EM.stop | ||
end | ||
|
||
# used for test with return value | ||
def echo text | ||
puts "received #{text} from client" | ||
received_message text | ||
return "server echo: #{text}" | ||
end | ||
|
||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.