Esempio n. 1
0
    public static Mutex AcquireMutex(NodeUri nodeUri, TimeSpan timeout)
    {
      Debug.Print("Acquire Mutex for Node URI '{0}'", nodeUri);
      var mutexName = GetMutexName(nodeUri);

      Debug.Print("Acquire Mutex name: '{0}'", mutexName);
      bool mutexCreated;
      var mutex = new Mutex(false, mutexName, out mutexCreated);

      Debug.Print("Mutex instance resolved - instance created: {0}", mutexCreated);
      try
      {
        Debug.Print("Try to aquire lock on mutex using timeout {0}", timeout);
        if (!mutex.WaitOne(timeout))
        {
          throw new TimeoutException(string.Format("Cannot acquire Mutex for URI '{0}' - there is another Node already exists for the same URI", nodeUri));
        }

        Debug.Print("Mutex successfully acquired!");
        return mutex;
      }
      catch (AbandonedMutexException abandonedMutexException)
      {
        Debug.Print("Mutex Abandoned Exception: {0}", abandonedMutexException.Message);

        mutex.ReleaseMutex();
        return AcquireMutex(nodeUri, timeout);
      }
    }
Esempio n. 2
0
    private static async Task<INode> LoadRootNode(NodeUri rootUri, INodeRuntime runtime, NodeConfigurationData configuration)
    {
      Debug.Indent();
      Debug.Print("Load Node Descriptors Loaders");

      //var nodeFactoryLoaders = configuration["nodeFactoryLoaders"].GetValue<IEnumerable<string>>()
      //  .Select(x => runtime.TypeLoader.LoadType(x))
      //  .Select(x => runtime.ObjectLoader.CreateInstance<INodeFactoryDescriptorLoader>(x))
      //  .ToArray();

      var nodeFactoryLoaders = new[] {new NodeFactoryDescriptorByTypeAttributeLoader(runtime)};

      Debug.Print("Create Node loader");
      var nodeLoader = new NodeFactoryLoader(nodeFactoryLoaders);

      Debug.Print("Create root Node locator builder");
      var nodeLocatorBuilder = new NodeLocatorBuilder();

      Debug.Print("Load root Node factory");
      var nodeFactory = nodeLoader.LoadNodeFactory(nodeLocatorBuilder, configuration);
      Debug.Assert(nodeFactory != null, "Unable to load root Node factory");

      Debug.Print($"Build root Node locator @'{rootUri}'");
      var locator = await nodeLocatorBuilder.BuildNodeLocator(rootUri);
      Debug.Assert(locator != null, "Unable to build root Node locator");

      Debug.Print($"Create root Node instance @'{rootUri}'");
      var node = await nodeFactory(rootUri, locator);
      Debug.Assert(node != null, "Unable to create root Node instance");

      Debug.Unindent();
      return node;
    }
Esempio n. 3
0
    public Node(NodeUri nodeUri)
    {
      if (nodeUri == null)
      {
        throw new ArgumentNullException("nodeUri");
      }

      NodeUri = nodeUri;
    }
    public Task<INode> GetNode(NodeUri nodeUri)
    {
      NodeInstanceContainer nodeInstanceContainer;
      if (_nodes.TryGetValue(nodeUri, out nodeInstanceContainer))
      {
        return nodeInstanceContainer.GetNode();
      }

      return Task.FromResult<INode>(null);
    }
Esempio n. 5
0
    public static Task<INode> Start(string[] arguments, NodeUri nodeUri = null)
    {
      Debug.Print("Parse command line arguments");
      var settings = CommandLineParser.Parse<NodeHostSettings>(arguments);

      Debug.Print("Create Hosting Runtime instance");
      var runtime = FullFrameworkNodeRuntime.CreateFromCurrentAppDomain();

      Debug.Print("Load configuration data");
      var config = LoadConfigurationData(runtime, settings);
      
      Debug.Print("Load root Node");
      return LoadRootNode(new NodeUri(nodeUri ?? NodeUri.LocalHost, settings.InstanceId ?? "/"), runtime, config);
    }
    public async Task<INode> GetNode(NodeUri nodeUri)
    {
      Debug.Indent();
      Debug.Print($"Node locator @'{NodeUri}' - get Node instance @'{nodeUri}'");
      var node = await _nodeLocator.GetNode(nodeUri);
      if (node != null)
      {
        Debug.Print($"Node locator @'{NodeUri}' - Node instance @'{nodeUri}' found");
        return node;
      }

      Debug.Print($"Node locator @'{NodeUri}' - Node instance @'{nodeUri}' NOT FOUND");
      Debug.Print($"Node locator @'{NodeUri}' - Get or wait for linked Node locator");
      var linkedLocator = await _linkedNodeLocatorTask;

      Debug.Print($"Acquired linked Node locator @'{linkedLocator.NodeUri}'");
      node = await linkedLocator.GetNode(nodeUri);

      Debug.Unindent();
      return node;
    }
    public Task<INodeLocator> BuildNodeLocator(NodeUri nodeUri)
    {
      Debug.Indent();
      Debug.Print($"Build NodeLocator @'{nodeUri}'");
      var defaultLocator = new DefaultNodeLocator(nodeUri);

      var definedNodes = new KeyValuePair<string, NodeInstanceContainerBuilder>[_nodes.Count];
      _nodes.CopyTo(definedNodes, 0);

      var nodeInstanceContainers = new LinkedList<NodeInstanceContainer>();
      foreach (var nodeBuilder in definedNodes)
      {
        var container = nodeBuilder.Value.BuildContainer(new NodeUri(nodeUri, nodeBuilder.Key), defaultLocator);
        nodeInstanceContainers.AddLast(container);
      }

      var locator = new StaticNodeLocator(nodeUri, nodeInstanceContainers);

      Debug.Print($"NodeLocator @'{nodeUri}' built successfully");
      _nodeLocatorCompletionSource.SetResult(locator);

      Debug.Unindent();
      return Task.FromResult<INodeLocator>(locator);
    }
Esempio n. 8
0
    public static string GetMutexName(NodeUri nodeUri)
    {
      if (nodeUri.IsAbsoluteUri) return nodeUri.AbsoluteUri;

      return new NodeUri(NodeUri.CreateLocalNodeUri(), nodeUri.LocalPath).AbsoluteUri;
    }
 public NodeInstanceContainer BuildContainer(NodeUri nodeUri, INodeLocator nodeLocator)
 {
   return new NodeInstanceContainer(nodeUri, _nodeFactory(nodeUri, nodeLocator));
 }
Esempio n. 10
0
 public StaticNodeLocator(NodeUri nodeUri, IEnumerable<NodeInstanceContainer> nodes) : base(nodeUri)
 {
   _nodes = nodes.ToDictionary(x => x.NodeUri, x => x);
 }
Esempio n. 11
0
 public Task<INode> GetNode(NodeUri nodeUri)
 {
   return Task.FromResult<INode>(null);
 }
Esempio n. 12
0
 public DefaultNodeLocator(NodeUri nodeUri) : base(nodeUri)
 { }
 public NodeInstanceContainer(NodeUri nodeUri, Task<INode> nodeInstanceTask)
 {
   NodeUri = nodeUri;
   _nodeInstanceTask = nodeInstanceTask;
 }