Skip to content

Instantly share code, notes, and snippets.

@aensidhe
Last active August 3, 2016 10:32
Show Gist options
  • Save aensidhe/b564b3e42d21bf9444b7bb6869c63059 to your computer and use it in GitHub Desktop.
Save aensidhe/b564b3e42d21bf9444b7bb6869c63059 to your computer and use it in GitHub Desktop.
Tarantool box info
using System;
using System.Net;
using System.Threading.Tasks;
using MsgPack.Light;
using Tarantool.Client;
using Tarantool.Client.Model;
using Tuple = Tarantool.Client.Model.Tuple;
namespace Tarantool.BoxInfo
{
class Program
{
static void Main()
{
try
{
MainAsync().GetAwaiter().GetResult();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadKey();
}
private static async Task MainAsync()
{
var options = new ConnectionOptions
{
EndPoint = new IPEndPoint(IPAddress.Parse("192.168.99.250"), 3301),
GuestMode = false,
UserName = "admin",
Password = "UGPfnEVbHVM8SscwenWY7LLndP95JEfA"
};
var boxInfoConverter = new BoxInfoConverter();
options.MsgPackContext.RegisterConverter<Replication>(boxInfoConverter);
options.MsgPackContext.RegisterConverter<Server>(boxInfoConverter);
options.MsgPackContext.RegisterConverter<BoxInfo>(boxInfoConverter);
var box = new Box(options);
await box.Connect();
// we need an overload without parameter because box.info is parameterless
var result = await box.Call<Tuple, BoxInfo>("box.info", Tuple.Create());
PrintBoxInfo(result.Data[0]);
}
private static void PrintBoxInfo(BoxInfo boxInfo)
{
Console.WriteLine("- server:");
Console.WriteLine($" lsn: {boxInfo.Server.Lsn}");
Console.WriteLine($" ro: {boxInfo.Server.ReadOnly}");
Console.WriteLine($" uuid: {boxInfo.Server.UUID}");
Console.WriteLine($" id: {boxInfo.Server.Id}");
Console.WriteLine($" version: {boxInfo.Version}");
Console.WriteLine($" pid: {boxInfo.Pid}");
Console.WriteLine($" status: {boxInfo.Status}");
Console.WriteLine(" replication:");
Console.WriteLine($" status: {boxInfo.Replication.Status}");
Console.WriteLine($" uptime: {boxInfo.Uptime}");
}
}
internal class BoxInfo : ITuple
{
public Server Server { get; set; }
public string Version { get; set; }
public int Pid { get; set; }
public string Status { get; set; }
public Replication Replication { get; set; }
public long Uptime { get; set; }
}
internal class Replication
{
public string Status { get; set; }
}
internal class Server
{
public long Lsn { get; set; }
public bool ReadOnly { get; set; }
public string UUID { get; set; }
public long Id { get; set; }
}
internal class BoxInfoConverter : IMsgPackConverter<BoxInfo>, IMsgPackConverter<Server>, IMsgPackConverter<Replication>
{
private IMsgPackConverter<string> stringConverter;
private IMsgPackConverter<int> intConverter;
private IMsgPackConverter<long> longConverter;
private IMsgPackConverter<bool> boolConverter;
public void Initialize(MsgPackContext context)
{
this.stringConverter = context.GetConverter<string>();
context.GetConverter<int[]>();
this.intConverter = context.GetConverter<int>();
this.longConverter = context.GetConverter<long>();
this.boolConverter = context.GetConverter<bool>();
}
public void Write(BoxInfo value, IMsgPackWriter writer)
{
throw new NotImplementedException();
}
public void Write(Server value, IMsgPackWriter writer)
{
throw new NotImplementedException();
}
Server IMsgPackConverter<Server>.Read(IMsgPackReader reader)
{
var length = reader.ReadMapLength();
if (length == null)
return null;
var result = new Server();
for (var i = 0; i < length.Value; i++)
{
var key = stringConverter.Read(reader);
switch (key)
{
case "lsn":
result.Lsn = this.longConverter.Read(reader);
break;
case "ro":
result.ReadOnly = this.boolConverter.Read(reader);
break;
case "uuid":
result.UUID = this.stringConverter.Read(reader);
break;
case "id":
result.Id = this.longConverter.Read(reader);
break;
default:
reader.SkipToken();
break;
}
}
return result;
}
public void Write(Replication value, IMsgPackWriter writer)
{
throw new NotImplementedException();
}
Replication IMsgPackConverter<Replication>.Read(IMsgPackReader reader)
{
var length = reader.ReadMapLength();
if (length == null)
return null;
var result = new Replication();
for (var i = 0; i < length.Value; i++)
{
var key = stringConverter.Read(reader);
switch (key)
{
case "status":
result.Status = this.stringConverter.Read(reader);
break;
default:
reader.SkipToken();
break;
}
}
return result;
}
BoxInfo IMsgPackConverter<BoxInfo>.Read(IMsgPackReader reader)
{
// some bug is going here, because if response is array, then array reading should be outside of this converter
var length = reader.ReadArrayLength();
if (length != 1)
return null;
length = reader.ReadMapLength();
if (length == null)
return null;
var result = new BoxInfo();
for (var i = 0; i < length.Value; i++)
{
var key = stringConverter.Read(reader);
switch (key)
{
case "server":
result.Server = ((IMsgPackConverter<Server>) this).Read(reader);
break;
case "version":
result.Version = this.stringConverter.Read(reader);
break;
case "pid":
result.Pid = this.intConverter.Read(reader);
break;
case "status":
result.Status = this.stringConverter.Read(reader);
break;
case "replication":
result.Replication = ((IMsgPackConverter<Replication>) this).Read(reader);
break;
case "uptime":
result.Uptime = this.longConverter.Read(reader);
break;
default:
reader.SkipToken();
break;
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment