Permalink
Browse files

Added examples

  • Loading branch information...
Dmitry Vasiliev
Dmitry Vasiliev committed Jan 2, 2010
1 parent 0d1c090 commit bf5c1bf8b3d95d12a5578b36bd40fc7eb920c7ec
Showing with 102 additions and 1 deletion.
  1. +2 −1 .gitignore
  2. +18 −0 examples/generator.erl
  3. +18 −0 examples/generator.py
  4. +20 −0 examples/handler.erl
  5. +20 −0 examples/handler.py
  6. +12 −0 examples/hello.erl
  7. +12 −0 examples/hello.py
View
@@ -1,5 +1,6 @@
*.swp
-*.pyc
+*.py[co]
+*.beam
/src/*.egg-info
/build
/dist
View
@@ -0,0 +1,18 @@
+-module(generator).
+-export([generate/1]).
+
+generate(N) when N >= 0 ->
+ Port = open_port({spawn, "python handler.py"},
+ [{packet, 1}, nouse_stdio, binary, {env, [{"PYTHONPATH", "../src"}]}]),
+ generate(Port, N),
+ receive
+ {Port, {data, Data}} ->
+ port_close(Port),
+ binary_to_term(Data)
+ end.
+
+generate(Port, 0) ->
+ port_command(Port, term_to_binary(stop));
+generate(Port, N) ->
+ port_command(Port, term_to_binary(now())),
+ generate(Port, N - 1).
View
@@ -0,0 +1,18 @@
+import time
+
+from erlport import Port, Protocol
+from erlport import Atom
+
+
+class EventGenerator(Protocol):
+
+ def handle(self, port, n):
+ while n > 0:
+ port.write(tuple(time.localtime()))
+ n -= 1
+ port.write(Atom("stop"))
+
+
+if __name__ == "__main__":
+ proto = EventGenerator()
+ proto.run(Port())
View
@@ -0,0 +1,20 @@
+-module(handler).
+-export([handle/1]).
+
+handle(N) when N >= 0 ->
+ Port = open_port({spawn, "python generator.py"},
+ [{packet, 1}, nouse_stdio, binary, {env, [{"PYTHONPATH", "../src"}]}]),
+ port_command(Port, term_to_binary(N)),
+ handle(Port, []).
+
+handle(Port, Acc) ->
+ receive
+ {Port, {data, Data}} ->
+ case binary_to_term(Data) of
+ stop ->
+ port_close(Port),
+ lists:reverse(Acc);
+ Term ->
+ handle(Port, [Term | Acc])
+ end
+ end.
View
@@ -0,0 +1,20 @@
+from erlport import Port, Protocol
+from erlport import Atom
+
+
+class EventHandler(Protocol):
+
+ def __init__(self):
+ self.collected = []
+
+ def handle(self, port, message):
+ if message == Atom("stop"):
+ port.write(self.collected)
+ self.collected = []
+ else:
+ self.collected.append(message)
+
+
+if __name__ == "__main__":
+ proto = EventHandler()
+ proto.run(Port())
View
@@ -0,0 +1,12 @@
+-module(hello).
+-export([hello/1]).
+
+hello(Name) ->
+ Port = open_port({spawn, "python hello.py"},
+ [{packet, 1}, nouse_stdio, binary, {env, [{"PYTHONPATH", "../src"}]}]),
+ port_command(Port, term_to_binary({hello, Name})),
+ receive
+ {Port, {data, Data}} ->
+ port_close(Port),
+ binary_to_term(Data)
+ end.
View
@@ -0,0 +1,12 @@
+from erlport import Port, Protocol
+
+
+class HelloProtocol(Protocol):
+
+ def handle_hello(self, name):
+ return "Hello, %s" % name
+
+
+if __name__ == "__main__":
+ proto = HelloProtocol()
+ proto.run(Port())

0 comments on commit bf5c1bf

Please sign in to comment.