126 lines
2.8 KiB
C#
126 lines
2.8 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using sharp.trading.logging;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
namespace sharp.tradebot
|
|
{
|
|
public class TradeBotLoader : MarshalByRefObject
|
|
{
|
|
public String DllPath { get; set; }
|
|
public String ClassName { get; set; }
|
|
public Guid UID { get; set; }
|
|
|
|
private AppDomain _appDomain;
|
|
private TradingBot _instance;
|
|
|
|
TradingBotEnvironment environment;
|
|
|
|
Dictionary<Guid, pworker> pWorkers;
|
|
|
|
struct pworker {
|
|
public PeriodicWorker worker;
|
|
|
|
public pworker(PeriodicWorker worker){
|
|
this.worker = worker;
|
|
}
|
|
|
|
public void call(){
|
|
worker();
|
|
}
|
|
}
|
|
|
|
public TradeBotLoader()
|
|
{
|
|
pWorkers = new Dictionary<Guid, pworker>();
|
|
}
|
|
|
|
public void RegisterPeriodic(PeriodicWorker worker, int timeout)
|
|
{
|
|
environment.RegisterPeriodic(worker,timeout);
|
|
}
|
|
public void UnregisterPeriodic(PeriodicWorker worker)
|
|
{
|
|
environment.UnregisterPeriodic(worker);
|
|
}
|
|
|
|
/*
|
|
public void RegisterPeriodic(PeriodicWorker worker, int timeout) {
|
|
Guid guid = Guid.NewGuid();
|
|
pworker pw = new pworker(worker);
|
|
pWorkers.Add(guid, pw );
|
|
environment.RegisterPeriodic( pWorkers[guid].call, timeout );
|
|
}
|
|
public void UnregisterPeriodic(PeriodicWorker worker){
|
|
foreach (Guid guid in pWorkers.Keys){
|
|
if (pWorkers[guid].worker == worker){
|
|
environment.UnregisterPeriodic(pWorkers[guid].call);
|
|
pWorkers.Remove(guid);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
|
|
public void Load(TradingBotEnvironment environment)
|
|
{
|
|
this.environment = environment;
|
|
|
|
AppDomainSetup ads = new AppDomainSetup();
|
|
// ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
|
this._appDomain = AppDomain.CreateDomain(String.Format("{0}:{1}:{2}:{3}", DllPath, ClassName, UID, Guid.NewGuid().ToString()), AppDomain.CurrentDomain.Evidence, ads);
|
|
/*
|
|
Assembly assembly = this._appDomain.Load(File.ReadAllBytes(DllPath));
|
|
Type type = assembly.GetType(ClassName);
|
|
this._instance = (TradingBot)Activator.CreateInstance(type);
|
|
*/
|
|
this._instance = (TradingBot)this._appDomain.CreateInstanceFromAndUnwrap(DllPath, ClassName);
|
|
this._instance.AfterLoad(environment, UID, new FileLogger(Path.Combine(environment.BaseDataDirectory, "logs", String.Format("{0}.log", UID.ToString()))),this);
|
|
}
|
|
|
|
public void Unload()
|
|
{
|
|
if (this._instance != null)
|
|
{
|
|
lock (this._instance)
|
|
{
|
|
if (this._instance.IsPrepared)
|
|
{
|
|
this._instance.Unprepare();
|
|
}
|
|
this._instance.Logger.Close();
|
|
}
|
|
this._instance = null;
|
|
AppDomain.Unload(this._appDomain);
|
|
this._appDomain = null;
|
|
}
|
|
}
|
|
|
|
public void Prepare()
|
|
{
|
|
if (this._instance != null)
|
|
{
|
|
lock (this._instance)
|
|
{
|
|
this._instance.Prepare();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Unprepare()
|
|
{
|
|
if (this._instance != null)
|
|
{
|
|
lock (this._instance)
|
|
{
|
|
this._instance.Unprepare();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|