Last active
December 16, 2015 05:19
-
-
Save dkuppitz/5383947 to your computer and use it in GitHub Desktop.
Simple C# Rexster Client
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
namespace TitanTest | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net.Sockets; | |
using MsgPack; | |
using MsgPack.Serialization; | |
using ObjectPrinter; | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var meta = new Dictionary<string, object> | |
{ | |
{ "channel", 2 }, | |
{ "graphName", "graph" } | |
}; | |
var message = new ScriptRequest | |
{ | |
Session = Guid.Empty.ToByteArray(), | |
Request = Guid.NewGuid().ToByteArray(), | |
Meta = meta, | |
LanguageName = "groovy", | |
//Script = "g.V.count()", // test scalar value | |
//Script = "g.V[0..max]", // test single vertex | |
Script = "g.V[0..max]", // test vertex collection and binding | |
Bindings = new Dictionary<string, object> | |
{ | |
{ "max", 5 } | |
} | |
}; | |
//var serializer = MsgPack.Serialization.MessagePackSerializer.Create<ScriptResponse<long>>(); | |
//var serializer = MsgPack.Serialization.MessagePackSerializer.Create<ScriptResponse<Vertex<TestNode>>>(); | |
var serializer = MessagePackSerializer.Create<ScriptResponse<Vertex<TestNode>[]>>(); | |
var tcpClient = new TcpClient("192.168.2.105", 8184); | |
using (var stream = tcpClient.GetStream()) | |
{ | |
using (var packer = Packer.Create(stream, false)) | |
{ | |
byte[] messageBytes; | |
packer.Pack((byte) 0); | |
packer.Pack((byte) 3); | |
using (var messageStream = new MemoryStream()) | |
using (var messagePacker = Packer.Create(messageStream)) | |
{ | |
messagePacker.Pack(message); | |
messageBytes = messageStream.ToArray(); | |
} | |
var lengthBytes = BitConverter.GetBytes(messageBytes.Length); | |
stream.WriteByte(lengthBytes[3]); | |
stream.WriteByte(lengthBytes[2]); | |
stream.WriteByte(lengthBytes[1]); | |
stream.WriteByte(lengthBytes[0]); | |
stream.Write(messageBytes, 0, messageBytes.Length); | |
using (var unpacker = Unpacker.Create(stream, false)) | |
{ | |
byte protocolVersion, messageType; | |
unpacker.ReadByte(out protocolVersion); | |
unpacker.ReadByte(out messageType); | |
// skip message length bytes | |
// don't use unpacker as it throws an exception for some lengths (don't know why) | |
stream.Skip(4); | |
var result = serializer.Unpack(stream); | |
result.DumpTo(Console.Out); // nuget package: ObjectPrinter | |
Debugger.Break(); | |
} | |
} | |
} | |
} | |
} | |
} |
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
namespace TitanTest | |
{ | |
using System.Collections.Generic; | |
using MsgPack.Serialization; | |
public class ScriptRequest | |
{ | |
[MessagePackMember(0)] | |
public byte[] Session { get; set; } | |
[MessagePackMember(1)] | |
public byte[] Request { get; set; } | |
[MessagePackMember(2)] | |
public Dictionary<string, object> Meta { get; set; } | |
[MessagePackMember(3)] | |
public string LanguageName { get; set; } | |
[MessagePackMember(4)] | |
public string Script { get; set; } | |
[MessagePackMember(5)] | |
public Dictionary<string, object> Bindings { get; set; } | |
} | |
} |
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
namespace TitanTest | |
{ | |
using System.Collections.Generic; | |
using MsgPack.Serialization; | |
public class ScriptResponse<T> | |
{ | |
[MessagePackMember(0)] | |
public byte[] Session { get; set; } | |
[MessagePackMember(1)] | |
public byte[] Request { get; set; } | |
[MessagePackMember(2)] | |
public Dictionary<string, object> Meta { get; set; } | |
[MessagePackMember(3)] | |
public T Result { get; set; } | |
[MessagePackMember(4)] | |
public Dictionary<string, object> Bindings { get; set; } | |
} | |
} |
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
namespace TitanTest | |
{ | |
using System; | |
using System.IO; | |
internal static class StreamExtensions | |
{ | |
public static void Skip(this Stream stream, long bytes) | |
{ | |
var bytesSkipped = 0L; | |
var bufferSize = (int)Math.Min(bytes, 8 * 1024 * 1024); | |
var buffer = new byte[bufferSize]; | |
while (bytesSkipped < bytes) | |
{ | |
bytesSkipped += stream.Read(buffer, 0, bufferSize); | |
} | |
} | |
} | |
} |
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
namespace TitanTest | |
{ | |
using System.Runtime.Serialization; | |
[DataContract] | |
public class TestNode | |
{ | |
[DataMember(Name = "name")] | |
public string Name { get; set; } | |
[DataMember(Name = "type")] | |
public string Type { get; set; } | |
[DataMember(Name = "key")] | |
public string Key { get; set; } | |
} | |
} |
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
namespace TitanTest | |
{ | |
using System.Runtime.Serialization; | |
[DataContract] | |
public class Vertex<T> | |
{ | |
[DataMember(Name = "_id")] | |
public string Id { get; set; } | |
[DataMember(Name = "_properties")] | |
public T Data { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment