-
Notifications
You must be signed in to change notification settings - Fork 7
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
0 parents
commit 8f59e92
Showing
7 changed files
with
166 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/_build | ||
/deps | ||
*.errlog | ||
erl_crash.dump | ||
*.ez | ||
*_data | ||
*.config | ||
*.swp | ||
.DS_Store | ||
.idea | ||
*gz | ||
*nugs | ||
|
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,83 @@ | ||
Exos | ||
==== | ||
|
||
Exos is a simple Port Wrapper : a GenServer which forwards cast and call to a | ||
linked Port. Requests and responses are converted using binary erlang term | ||
external representation. | ||
|
||
You can use [clojure-erlastic](http://github.com/awetzel/clojure-erlastic), | ||
[python-erlastic](http://github.com/awetzel/python-erlastic), etc. | ||
|
||
## Exemple usage : a clojure calculator ## | ||
|
||
Using [clojure-erlastic](http://github.com/awetzel/clojure-erlastic), your can easily create | ||
a port and communicate with it. | ||
|
||
> mix new myproj | ||
> cd myproj ; mkdir -p priv/calculator | ||
> vim project.clj | ||
```clojure | ||
(defproject calculator "0.0.1" | ||
:dependencies [[clojure-erlastic "0.2.3"] | ||
[org.clojure/core.match "0.2.1"]]) | ||
``` | ||
|
||
> lein uberjar | ||
> vim calculator.clj | ||
```clojure | ||
(require '[clojure-erlastic.core :refer [run-server]]) | ||
(use '[clojure.core.match :only (match)]) | ||
(run-server | ||
(fn [term count] (match term | ||
[:add n] [:noreply (+ count n)] | ||
[:rem n] [:noreply (- count n)] | ||
:get [:reply count count]))) | ||
``` | ||
|
||
Then in your project, you can use Exos.Proc GenServer as a proxy to the clojure | ||
port. | ||
|
||
```elixir | ||
defmodule Calculator do | ||
def start_link(ini), do: GenServer.start_link(Exos.Proc,{"#{:code.priv_dir(:myproj)}/calculator","java -cp 'target/*' clojure.main calculator.clj",ini},name: __MODULE__) | ||
def add(v), do: GenServer.cast(__MODULE__,{:add,v}) | ||
def rem(v), do: GenServer.cast(__MODULE__,{:rem,v}) | ||
def get, do: GenServer.call(__MODULE__,:get,:infinity) | ||
end | ||
|
||
defmodule MyProj.App do | ||
use Application | ||
def start(_,_), do: MyProj.App.Sup.start_link | ||
|
||
defmodule Sup do | ||
use Supervisor | ||
def start_link, do: Supervisor.start_link(__MODULE__,[]) | ||
def init([]), do: supervise([ | ||
worker(Calculator,0) | ||
], strategy: :one_for_one) | ||
end | ||
end | ||
``` | ||
|
||
> vim mix.exs | ||
```elixir | ||
def application do | ||
[mod: { MyProj.App, [] }] | ||
end | ||
``` | ||
|
||
Then you can use the calculator | ||
|
||
> iex -S mix | ||
```elixir | ||
Calculator.add(5) | ||
Calculator.rem(1) | ||
4 == Calculator.get | ||
``` |
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,24 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
use Mix.Config | ||
|
||
# This configuration is loaded before any dependency and is restricted | ||
# to this project. If another project depends on this project, this | ||
# file won't be loaded nor affect the parent project. For this reason, | ||
# if you want to provide default values for your application for third- | ||
# party users, it should be done in your mix.exs file. | ||
|
||
# Sample configuration: | ||
# | ||
# config :logger, :console, | ||
# level: :info, | ||
# format: "$date $time [$level] $metadata$message\n", | ||
# metadata: [:user_id] | ||
|
||
# It is also possible to import configuration files, relative to this | ||
# directory. For example, you can emulate configuration per environment | ||
# by uncommenting the line below and defining dev.exs, test.exs and such. | ||
# Configuration from the imported file will override the ones defined | ||
# here (which is why it is important to import them last). | ||
# | ||
# import_config "#{Mix.env}.exs" |
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,24 @@ | ||
defmodule Exos.Proc do | ||
use GenServer | ||
|
||
def init({cd,cmd,initarg}) do | ||
port = Port.open({:spawn,'#{cmd}'}, [:binary,:exit_status, packet: 4, cd: '#{cd}']) | ||
send(port,{self,{:command,:erlang.term_to_binary(initarg)}}) | ||
{:ok,port} | ||
end | ||
|
||
def handle_info({port,{:exit_status,0}},port), do: {:stop,:normal,port} | ||
def handle_info({port,{:exit_status,_}},port), do: {:stop,:port_terminated,port} | ||
def handle_info(_,port), do: {:noreply,port} | ||
|
||
def handle_cast(term,port) do | ||
send(port,{self,{:command,:erlang.term_to_binary(term)}}) | ||
{:noreply,port} | ||
end | ||
|
||
def handle_call(term,_,port) do | ||
send(port,{self,{:command,:erlang.term_to_binary(term)}}) | ||
result = receive do {^port,{:data,b}}->:erlang.binary_to_term(b) end | ||
{:reply,result,port} | ||
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,14 @@ | ||
defmodule Exos.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[app: :exos, | ||
version: "0.0.1", | ||
elixir: "~> 1.0.0", | ||
deps: []] | ||
end | ||
|
||
def application do | ||
[applications: [:logger]] | ||
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,7 @@ | ||
defmodule ExosTest do | ||
use ExUnit.Case | ||
|
||
test "the truth" do | ||
assert 1 + 1 == 2 | ||
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 @@ | ||
ExUnit.start() |