douhuiyan2772 2015-04-23 08:07
浏览 109
已采纳

将Erlang-C端口示例转换为Erlang-Golang

I'm trying to write a Golang driver for Erlang, accesible via an Erlang port.

I've started with the Erlang C port example, which works fine:

http://www.erlang.org/doc/tutorial/c_port.html

Now I'm trying to port the C code to Golang; just trying to echo a simple 'Hello World ' message, using ' ' as the delimiter.

So my Golang code is as follows:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    bytes, _ := reader.ReadBytes('
')
    os.Stdout.Write(bytes)
}

And I can compile it and run it from the command line as follows:

justin@justin-ThinkPad-X240:~/work/erlang_golang_port$ go build -o tmp/echo echo.go
justin@justin-ThinkPad-X240:~/work/erlang_golang_port$ ./tmp/echo
Enter text: hello
hello

However when I try to call the driver from the Erlang side (Erlang code below) I get the following:

justin@justin-ThinkPad-X240:~/work/erlang_golang_port$ erl
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Eshell V5.10.4  (abort with ^G)
1> c(complex1).
{ok,complex1}
2> complex1:start("./tmp/echo").
<0.41.0>
3> complex1:ping().

=ERROR REPORT==== 23-Apr-2015::08:56:47 ===
Bad value on output port './tmp/echo'

I have the sense that the message is being passed OK to the driver, but that I am somehow returning the response incorrectly.

TIA.

Erlang port code:

-module(complex1).

-export([start/1, stop/0, init/1]).

-export([ping/0]).

-define(HELLO_WORLD, [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 46]).

start(ExtPrg) ->
    spawn(?MODULE, init, [ExtPrg]).

stop() ->
    complex ! stop.

ping() ->
    call_port({ping, ?HELLO_WORLD++[10]}).

call_port(Msg) ->
    complex ! {call, self(), Msg},
    receive
    {complex, Result} ->
        Result
    end.

init(ExtPrg) ->
    register(complex, self()),
    process_flag(trap_exit, true),
    Port = open_port({spawn, ExtPrg}, [{packet, 2}]),
    loop(Port).

loop(Port) ->
    receive
    {call, Caller, Msg} ->
        Port ! {self(), {command, Msg}},
        receive
        {Port, {data, Data}} ->
            Caller ! {complex, Data}
        end,
        loop(Port);
    stop ->
        Port ! {self(), close},
        receive
        {Port, closed} ->
            exit(normal)
        end;
    {'EXIT', Port, _Reason} ->
        exit(port_terminated)
    end.
  • 写回答

1条回答 默认 最新

  • 普通网友 2016-01-22 12:44
    关注

    Posting this answer based on @Justin's follow-up question here, which contains a slightly different but working answer.

    echo.go:

    package main
    
    import (
        "bufio"
        "os"
    )
    
    func main() {
        for{
            reader := bufio.NewReader(os.Stdin)
            bytes, _ := reader.ReadBytes('
    ')
            os.Stdout.Write(bytes)
        }
    }
    

    complex1.erl:

    -module(complex1).
    -export([start/1, stop/0, init/1]).
    -export([send/1]).
    
    start(ExtPrg) ->
        spawn_link(?MODULE, init, [ExtPrg]).
    stop() ->
        complex ! stop.
    
    send(Y) -> call_port({msg, Y}).
    call_port({msg, Msg}) ->
        complex ! {call, self(), Msg},
        receive
        {complex, Result} ->
            Result
        end.
    
    init(ExtPrg) ->
        register(complex, self()),
        process_flag(trap_exit, true),
        Port = open_port({spawn, ExtPrg}, []),
        loop(Port).
    
    loop(Port) ->
        receive
        {call, Caller, Msg} ->
            Port ! {self(), {command, Msg++[10]}},
            Data = receive_all(Port, 100),
            Caller ! {complex, Data},
            loop(Port);
        stop ->
            Port ! {self(), close},
            receive {Port, closed} ->
                exit(normal)
            end;
        {'EXIT', Port, Reason} ->
            exit({port_terminated, Reason})
        end.
    
    receive_all(Port, Timeout) -> receive_all(Port, Timeout, []).
    receive_all(Port, Timeout, Buffer) ->
        receive
            {Port, {data, Data}} ->
                receive_all(Port, Timeout, [Data | Buffer])
            after Timeout ->
                lists:flatten(lists:reverse(Buffer))
        end.
    

    $ erl

    Erlang R16B02_basho8 (erts-5.10.3) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
    
    Eshell V5.10.3  (abort with ^G)
    1> c(complex1).
    {ok,complex1}
    2> complex1:start("go run echo.go").
    <0.40.0>
    3> complex1:send("asdhadlsjahdslahjdlhd").
    "asdhadlsjahdslahjdlhd"
    4> complex1:send("aksdghjakdsgalkdgaldsagdlkagdlkadg").
    "aksdghjakdsgalkdgaldsagdlkagdlkadg"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能