Created
December 9, 2012 22:18
-
-
Save Gab-km/4247263 to your computer and use it in GitHub Desktop.
#127 「MessagePack InterLanguage」のサンプルコード
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
import std.stdio; | |
import std.stream; | |
import std.socket; | |
import std.typecons; | |
import msgpack; | |
void main() | |
{ | |
communicateWithPythonClient(); | |
communicateWithFSharpServer(); | |
} | |
void communicateWithPythonClient() | |
{ | |
// Python TCP クライアントとの接続 | |
auto sock = new TcpSocket(AddressFamily.INET); | |
auto sock4py = listen(sock, "localhost", 5001); | |
writeln("Connected to Python client."); | |
scope(exit) { | |
sock4py.close(); | |
sock.close(); | |
} | |
auto received = receive(sock4py, 6); | |
writeln(received); | |
} | |
void communicateWithFSharpServer() | |
{ | |
// F# TCP サーバとの接続 | |
auto sock4fs = new TcpSocket(AddressFamily.INET); | |
connect(sock4fs, "localhost", 5002); | |
writeln("Connected F# server."); | |
scope(exit) | |
sock4fs.close(); | |
auto result = sock4fs.send(pack(tuple(3, "Hello D!"))); | |
writeln(result); | |
} | |
Socket listen(Socket sock, string hostname, ushort port) | |
{ | |
sock.bind(new InternetAddress(hostname, port)); | |
sock.listen(1024); | |
writeln("Waiting for connection..."); | |
return sock.accept(); | |
} | |
void connect(Socket sock, string hostname, ushort port) | |
{ | |
sock.connect(new InternetAddress(hostname, port)); | |
} | |
char[] receive(Socket sock, size_t length) | |
{ | |
auto received = new byte[1024]; | |
auto size = sock.receive(received); | |
auto unpacked = new byte[1024]; | |
unpack(cast(ubyte[])received, unpacked); | |
auto stream = new MemoryStream(unpacked); | |
scope(exit) | |
stream.close(); | |
return stream.readString(length); | |
} |
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
open System.Net.Sockets | |
open System.Collections.Generic | |
open MsgPack.Serialization | |
let host = "localhost" | |
let printText = printfn "%s" | |
let createListener (host : string) (port : int) = | |
let ipAddr = System.Net.Dns.GetHostAddresses(host).[1] | |
new System.Net.Sockets.TcpListener(ipAddr, port) | |
let listen (listener : TcpListener) = | |
listener.Start() | |
printText "Listen start." | |
listener.AcceptTcpClient() | |
let send<'a> ns (value : 'a) = | |
let serializer = MessagePackSerializer.Create<'a>() | |
serializer.Pack(ns, value) | |
let receive<'a> ns = | |
let serializer = MessagePackSerializer.Create<'a>() | |
serializer.Unpack(ns) | |
let communicateWithPythonServer () = | |
// Python TCP サーバとの接続 | |
use tcp4py = new System.Net.Sockets.TcpClient(host, 5000) | |
use ns2py = tcp4py.GetStream() | |
send<int> ns2py 1 | |
printText "Sent to Python server." | |
let communicateWithDClient () = | |
// D TCP クライアントとの接続 | |
let listener = createListener host 5002 | |
try | |
use tcp4d = listen listener | |
use ns2d = tcp4d.GetStream() | |
let received = receive<int * string> ns2d | |
printfn "Received: %A" received | |
finally | |
listener.Stop() | |
[<EntryPoint>] | |
let main (args) = | |
communicateWithPythonServer() | |
communicateWithDClient() | |
0 |
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
# -*- coding: utf-8 *-* | |
import socket | |
import msgpack | |
def listen(host, port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
sock.bind((host, port)) | |
sock.listen(1) | |
print('Waiting for connections...') | |
conn, addr = sock.accept() | |
print('Connected to F# client.') | |
return sock, conn | |
def connect(host, port): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect((host, port)) | |
print('Connected to D server.') | |
return sock | |
def receive(conn): | |
received = conn.recv(1024) | |
print(received) | |
data = msgpack.unpackb(received) | |
print("receive: {0}".format(data)) | |
return data | |
def send(conn, data): | |
conn.send(msgpack.packb(data)) | |
print("send: {0}".format(data)) | |
def close_socket(sock): | |
if sock is not None: | |
sock.close() | |
if __name__ == '__main__': | |
sock = None | |
sock4fs = None | |
sock4D = None | |
try: | |
# F# TCP クライアントとの接続 | |
sock, sock4fs = listen('', 5000) | |
fromFSharp = receive(sock4fs) | |
# D TCP サーバとの接続 | |
sock4D = connect('localhost', 5001) | |
send(sock4D, "Python") | |
finally: | |
close_socket(sock) | |
close_socket(sock4fs) | |
close_socket(sock4D) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment