This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RPC ruleset for cross-language integration
- Loading branch information
Showing
5 changed files
with
220 additions
and
3 deletions.
There are no files selected for viewing
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,58 @@ | ||
<?php | ||
// This implementation executes a echo service in gochatbot and PHP. | ||
$rpcServer = getenv('GOCHATBOT_RPC_BIND'); | ||
|
||
function botPop($rpcServer) { | ||
$raw = file_get_contents(sprintf('http://%s/pop', $rpcServer)); | ||
|
||
$ret = json_decode($raw, true); | ||
$err = json_last_error(); | ||
if (JSON_ERROR_NONE != $err) { | ||
die(json_last_error_msg()); | ||
} | ||
|
||
return $ret; | ||
} | ||
|
||
function botSend($rpcServer, $msg) { | ||
$url = sprintf('http://%s/send', $rpcServer); | ||
|
||
$json = json_encode($msg); | ||
$err = json_last_error(); | ||
if (JSON_ERROR_NONE != $err) { | ||
die(json_last_error_msg()); | ||
} | ||
|
||
$options = [ | ||
'http' => [ | ||
'header' => "Content-type: application/json\r\n", | ||
'method' => 'POST', | ||
'content' => $json, | ||
], | ||
]; | ||
$context = stream_context_create($options); | ||
return file_get_contents($url, false, $context); | ||
} | ||
|
||
while (true) { | ||
$msg = botPop($rpcServer); | ||
if (empty($msg['Message'])) { | ||
continue; | ||
} | ||
echo 'Got:', PHP_EOL; | ||
print_r($msg); | ||
|
||
$newMsg = [ | ||
'Room' => $msg['Room'], | ||
'FromUserID' => $msg['ToUserID'], | ||
'FromUserName' => $msg['ToUserName'], | ||
'ToUserID' => $msg['FromUserID'], | ||
'ToUserName' => $msg['FromUserName'], | ||
'Message' => 'echo: ' . $msg['Message'], | ||
]; | ||
|
||
echo 'Sending:', PHP_EOL; | ||
print_r($newMsg); | ||
|
||
botSend($rpcServer, $newMsg); | ||
} |
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,46 @@ | ||
package rpc // import "cirello.io/gochatbot/rules/rpc" | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"cirello.io/gochatbot/messages" | ||
) | ||
|
||
func (r *rpcRuleset) httpPop(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
var msg messages.Message | ||
if len(r.inbox) > 1 { | ||
msg, r.inbox = r.inbox[0], r.inbox[1:] | ||
} else if len(r.inbox) == 1 { | ||
msg = r.inbox[0] | ||
r.inbox = []messages.Message{} | ||
} else if len(r.inbox) == 0 { | ||
fmt.Fprint(w, "{}") | ||
return | ||
} | ||
|
||
if err := json.NewEncoder(w).Encode(&msg); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func (r *rpcRuleset) httpSend(w http.ResponseWriter, req *http.Request) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
var msg messages.Message | ||
if err := json.NewDecoder(req.Body).Decode(&msg); err != nil { | ||
log.Fatal(err) | ||
} | ||
defer req.Body.Close() | ||
|
||
go func(m messages.Message) { | ||
r.outCh <- m | ||
}(msg) | ||
fmt.Fprintln(w, "OK") | ||
} |
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,59 @@ | ||
package rpc // import "cirello.io/gochatbot/rules/rpc" | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"sync" | ||
|
||
"cirello.io/gochatbot/bot" | ||
"cirello.io/gochatbot/messages" | ||
) | ||
|
||
type rpcRuleset struct { | ||
mux *http.ServeMux | ||
|
||
bindAddr string | ||
outCh chan messages.Message | ||
|
||
mu sync.Mutex | ||
inbox []messages.Message | ||
} | ||
|
||
// Name returns this rules name - meant for debugging. | ||
func (r *rpcRuleset) Name() string { | ||
return "RPC Ruleset" | ||
} | ||
|
||
// Boot runs preparatory steps for ruleset execution | ||
func (r *rpcRuleset) Boot(self *bot.Self) { | ||
r.mux.HandleFunc("/pop", r.httpPop) | ||
r.mux.HandleFunc("/send", r.httpSend) | ||
log.Println("rpc: listening", r.bindAddr) | ||
go http.ListenAndServe(r.bindAddr, r.mux) | ||
} | ||
|
||
func (r rpcRuleset) HelpMessage(self bot.Self) string { | ||
return fmt.Sprintln("RPC listens to", r.bindAddr, "for RPC calls") | ||
} | ||
|
||
func (r *rpcRuleset) ParseMessage(self bot.Self, in messages.Message) []messages.Message { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
r.inbox = append(r.inbox, in) | ||
|
||
return []messages.Message{} | ||
} | ||
|
||
// New returns a RPC ruleset | ||
func New(bindAddr string) *rpcRuleset { | ||
return &rpcRuleset{ | ||
mux: http.NewServeMux(), | ||
bindAddr: bindAddr, | ||
} | ||
} | ||
|
||
func (r *rpcRuleset) SetOutgoingChannel(outCh chan messages.Message) { | ||
r.outCh = outCh | ||
} |