TradeBot/ServiceDaemon.cs

213 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using sharp.json.network;
using sharp.trading;
using sharp.tradebot;
using System.IO;
using System.Threading;
using sharp.json;
using sharp.tradebot.rpc;
using sharp.extensions;
using sharp.trading.bittrex;
namespace TradeBot
{
public static class ServiceDaemon
{
public static bool daemonShouldExit = false;
public static bool ShouldExit()
{
return daemonShouldExit;
}
public static BotManager botManager;
public static void Run(String[] args){
ArgumentReader ar = new ArgumentReader(args);
while (ar.MoveNext()){
switch (ar.Current)
{
case "--test":
TradingEnvironment.DataDirectory = "sharp.trading.data";
TradeBotApplication.jsonPort = 33554;
break;
case "--port":
ar.MoveNext();
TradeBotApplication.jsonPort = int.Parse(ar.Current);
return;
default:
throw new ArgumentException(String.Format("Unexpected Argument: {0}", ar.Current));
}
}
if (File.Exists(Path.Combine(TradingEnvironment.DataDirectory,"connector.json"))){
JSON connector = JSON.ReadFrom(Path.Combine(TradingEnvironment.DataDirectory, "connector.json"));
if (connector["class"] != null){
Type t = Type.GetType(connector["class"].String);
TradingConnection c = (TradingConnection)Activator.CreateInstance(t, connector["arguments"].To<string[]>());
TradingEnvironment.DefaultConnection = c;
}
} else {
TradingEnvironment.DefaultConnection = new BittrexConnector();
}
JSONTcpServer jsonServer = new JSONTcpServer(TradeBotApplication.jsonPort);
jsonServer.ClientConnected = JSONClientConnected;
jsonServer.Start();
botManager = new BotManager(TradingEnvironment.DataDirectory);
botManager.LoadAllBots();
foreach (TradingBotInstance bot in botManager.RegisteredBots){
bot.Prepare();
}
while (!ShouldExit())
{
Thread.Sleep(100);
}
Console.WriteLine("Servicedaemon shutting down");
botManager.BotEnvironment.Stop();
foreach (TradingBotInstance bot in botManager.RegisteredBots){
bot.Unprepare();
}
foreach (TradingBotInstance bot in botManager.RegisteredBots){
bot.Unload();
}
botManager.Save();
jsonServer.Stop();
}
private static void JSONClientConnected(JSONTcpClient client)
{
try
{
while (true)
{
JSON jrequest = client.Receive();
RPCRequest request = jrequest.To<RPCRequest>();
RPCResponse response = new RPCResponse();
if (request.Command.Equals("disconnect"))
{
break;
}
switch (request.Command)
{
case "newbot":
Console.WriteLine("request to create new bot: {0} from {1}", request.Arguments[0], request.Arguments[1]);
response.Message = "Bot created";
response.Success = true;
break;
case "shutdown":
response.Message = "Daemon shutting down";
response.Success = true;
daemonShouldExit = true;
break;
case "register":
try
{
botManager.RegisterBot(request.Arguments[0],request.Arguments[1]);
Console.WriteLine(botManager.ToString());
response.Message = "registered.";
response.Success = true;
}
catch (Exception e)
{
response.Message = String.Format("Exception: {0}", e);
response.Success = false;
}
break;
case "unregister":
try
{
botManager.UnregisterBot(Guid.Parse(request.Arguments[0]));
response.Message = "unregistered.";
response.Success = true;
}
catch (Exception e)
{
response.Message = String.Format("Exception: {0}", e);
response.Success = false;
}
break;
case "load":
try
{
botManager.LoadBotAndPrepare(Guid.Parse(request.Arguments[0]));
response.Message = "loaded.";
response.Success = true;
}
catch (Exception e)
{
response.Message = String.Format("Exception: {0}", e);
response.Success = false;
}
break;
case "unload":
try
{
botManager.UnloadBot(Guid.Parse(request.Arguments[0]));
response.Message = "unloaded.";
response.Success = true;
}
catch (Exception e)
{
response.Message = String.Format("Exception: {0}", e);
response.Success = false;
}
break;
case "reload":
try
{
botManager.UnloadBot(Guid.Parse(request.Arguments[0]));
botManager.LoadBotAndPrepare(Guid.Parse(request.Arguments[0]));
response.Message = "reloaded.";
response.Success = true;
}
catch (Exception e)
{
response.Message = String.Format("Exception: {0}", e);
response.Success = false;
}
break;
default:
response.Success = false;
response.Message = string.Format("Unknown command: {0}", request.Command);
break;
}
client.Send(JSON.From(response));
}
}
catch (Exception e)
{
Console.WriteLine("JSONClientConnection: Exception: " + e.ToString());
}
client.Close();
}
}
}