public void Start()
        {
            lock (this)
            {
                if (IsRunning)
                {
                    throw new InvalidOperationException("Already running");
                }
                start_time       = node.UtcNow;
                last_time        = start_time;
                actual_last_time = last_time;

                cancel = new CancellationTokenSource();
                if (!oneshot)
                {
                    var noop = PeriodicTask.Run(periodic_handler, TimeSpan.FromMilliseconds(Period), cancel.Token);
                }
                else
                {
                    RunOne().IgnoreResult();
                }
            }
        }
Exemple #2
0
        public async Task <object> ConnectService(Transport c, string url, string username = null, object credentials = null, string objecttype = null, CancellationToken cancel = default(CancellationToken))
        {
            this.connecturl       = url;
            this.connecttransport = c;
            var u = TransportUtil.ParseConnectionUrl(url);

            m_RemoteNodeID = u.nodeid;

            m_RemoteNodeName = u.nodename;
            m_ServiceName    = u.service;

            //ProgramName = ProgramName1;

            /* try
             * {*/
            //if (RobotRaconteurService.s.transports[c] == null) return null;
            if (!c.CanConnectService(url))
            {
                throw new ServiceException("Invalid transport");
            }

            TransportConnection = await c.CreateTransportConnection(url, this, cancel);

            m_Connected = true;
            try
            {
                transport        = c.TransportID;
                m_RemoteEndpoint = 0;

                ServiceDefinition[] d = await PullServiceDefinitionAndImports(null, cancel : cancel);

                lock (pulled_service_defs)
                {
                    foreach (var d2 in d)
                    {
                        if (!pulled_service_defs.ContainsKey(d2.Name))
                        {
                            pulled_service_defs.Add(d2.Name, d2);
                        }
                    }
                }

                if (!UsePulledServiceTypes)
                {
                    m_ServiceDef = node.GetServiceType(d[0].Name);
                }
                else
                {
                    var f = DynamicServiceFactory_.CreateServiceFactories(d.Select(x => x.ToString()).ToArray(), this);
                    lock (pulled_service_defs)
                    {
                        foreach (var f2 in f)
                        {
                            if (!pulled_service_types.ContainsKey(f2.GetServiceName()))
                            {
                                pulled_service_types.Add(f2.GetServiceName(), f2);
                            }
                        }
                    }
                    m_ServiceDef = GetPulledServiceType(d[0].Name);
                }

                MessageEntry e = new MessageEntry(MessageEntryType.ObjectTypeName, "");
                //e.AddElement("servicepath", ServiceName);
                e.ServicePath = ServiceName;

                MessageEntry ret = await ProcessRequest(e, cancel);

                if (ret.Error != MessageErrorType.None)
                {
                    return(null);
                }
                string type = ret.FindElement("objecttype").CastData <string>();
                if (type == "")
                {
                    return(new ObjectNotFoundException("Could not find object type"));
                }
                ;


                if (objecttype != null)
                {
                    VerifyObjectImplements(type, objecttype);
                    type = objecttype;
                    await PullServiceDefinitionAndImports(ServiceDefinitionUtil.SplitQualifiedName(type).Item2, cancel);
                }


                MessageEntry e2 = new MessageEntry();
                e2.ServicePath = ServiceName;
                e2.MemberName  = "registerclient";
                e2.EntryType   = MessageEntryType.ConnectClient;
                await ProcessRequest(e2, cancel);

                if (username != null)
                {
                    await AuthenticateUser(username, credentials, cancel);
                }

                ServiceStub stub = ServiceDef.CreateStub(type, ServiceName, this);
                stubs.Add(ServiceName, stub);
                Task noop = PeriodicTask.Run(PeriodicCleanupTask, TimeSpan.FromSeconds(5), cancel_source.Token).IgnoreResult();

                return(stub);
            }
            catch (Exception e)
            {
                try
                {
                    TransportConnection.Close();
                }
                catch { }

                m_Connected = false;
                throw e;
            }


            /*}
             * catch {
             *  return null;
             * }*/
        }