Exemple #1
0
        public async Task <RobotRaconteurNode.MonitorLock> MonitorEnter(object obj, int timeout, CancellationToken cancel = default(CancellationToken))
        {
            bool        iserror = true;
            IDisposable lock_   = null;

            try
            {
                if (!(obj is ServiceStub))
                {
                    throw new InvalidOperationException("Can only unlock object opened through Robot Raconteur");
                }
                ServiceStub s = (ServiceStub)obj;
                lock_ = await s.rr_async_mutex.Lock();

                bool         keep_trying = true;
                MessageEntry m           = new MessageEntry(MessageEntryType.ClientSessionOpReq, "MonitorEnter");
                m.ServicePath = s.RRServicePath;
                m.AddElement("timeout", timeout);

                MessageEntry ret = await ProcessRequest(m, cancel);

                string retcode = ret.FindElement("return").CastData <string>();

                if (retcode == "OK")
                {
                    iserror = false;
                    return(new RobotRaconteurNode.MonitorLock
                    {
                        lock_ = lock_,
                        stub = s
                    });
                }
                if (retcode == "Continue")
                {
                    while (keep_trying)
                    {
                        MessageEntry m1 = new MessageEntry(MessageEntryType.ClientSessionOpReq, "MonitorContinueEnter");
                        m1.ServicePath = s.RRServicePath;

                        MessageEntry ret1 = await ProcessRequest(m1, cancel);

                        string retcode1 = ret1.FindElement("return").CastData <string>();
                        if (retcode1 == "OK")
                        {
                            iserror = false;
                            return(new RobotRaconteurNode.MonitorLock
                            {
                                lock_ = lock_,
                                stub = s
                            });
                        }
                        if (retcode1 != "Continue")
                        {
                            throw new ProtocolException("Unknown return code");
                        }
                    }
                }
                else
                {
                    throw new ProtocolException("Unknown return code");
                }
            }
            finally
            {
                if (iserror)
                {
                    if (lock_ != null)
                    {
                        try
                        {
                            lock_.Dispose();
                        }
                        catch { }
                    }
                    //Monitor.Exit(obj);
                }
            }

            throw new ProtocolException("Unknown return code");
        }
 protected GeneratorClientBase(string membername, ServiceStub stub, int id)
 {
     this.stub       = stub;
     this.membername = membername;
     this.id         = id;
 }
Exemple #3
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;
             * }*/
        }
 public WireClient(string name, ServiceStub stub)
 {
     m_MemberName = name;
     this.stub    = stub;
     stub.RRContext.ClientServiceListener += ClientContextListener;
 }