Skip to content

Commit

Permalink
Make remote configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
lafka committed Mar 1, 2016
1 parent 1479232 commit dba6b65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
3 changes: 3 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule Tinyconnect.Mixfile do
:poison,
:sockjs,
:crypto
],
env: [
remote: 'tcp.cloud-ng.tiny-mesh.com',
]
]
end
Expand Down
6 changes: 5 additions & 1 deletion src/tinyconnect_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ start_link(NID, PortID) ->

init([NID, PortID]) ->
Opts = [binary, {packet, raw}],
case gen_tcp:connect("tcp.cloud-ng.tiny-mesh.com", 7001, Opts) of
{Remote, Port} = case string:tokens(application:get_env(tinyconnect, remote, "tcp.cloud-ng.tiny-mesh.com:7001"), ":") of
[Rem] -> {Rem, 7001};
[Rem, PortStr] -> {Rem, list_to_integer(PortStr)}
end,
case gen_tcp:connect(Remote, Port, Opts) of
{ok, Socket} ->
io:format("conn[~p]: connected ~p~n", [self(), Socket]),
ok = maybe_create_pg2_group(NID),
Expand Down
38 changes: 21 additions & 17 deletions src/tinyconnect_tty.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,27 @@ init([Path]) ->
ok = maybe_create_pg2_group(<<"nid:", B64NID/binary>>),
ok = maybe_create_pg2_group(<<"port:", (atom_to_binary(PortID, utf8))/binary>>),

{ok, Conn} = tinyconnect_tcp:start_link(B64NID, PortID),

ok = tinyconnect_tty_ports:update(PortID, #{uart => true
, nid => B64NID
, sid => SID
, uid => UID}),

{ok, #{
port => Port
, id => PortID
, path => Path
, rest => <<>>
, nid => B64NID
, sid => SID
, uid => UID
, conn => Conn
}}
case tinyconnect_tcp:start_link(B64NID, PortID) of
{ok, Conn} ->
ok = tinyconnect_tty_ports:update(PortID, #{uart => true
, nid => B64NID
, sid => SID
, uid => UID}),

{ok, #{
port => Port
, id => PortID
, path => Path
, rest => <<>>
, nid => B64NID
, sid => SID
, uid => UID
, conn => Conn
}};

{error, _} ->
{error, remote}
end
end;

{error, {exit, {signal, 11}}} ->
Expand Down

0 comments on commit dba6b65

Please sign in to comment.