Esempio n. 1
0
        protected long SendMethodRequest(QMFObject obj, Broker aBroker, string name, List <object> args, bool synchronous, int timeToLive)
        {
            SchemaMethod method = obj.Schema.GetMethod(name);

            if (args == null)
            {
                args = new List <object>();
            }

            long seq = 0;

            if (method != null)
            {
                KeyValuePair <SchemaMethod, bool> pair = new KeyValuePair <SchemaMethod, bool>(method, synchronous);
                seq = SequenceManager.Reserve(pair);
                IEncoder enc = aBroker.CreateEncoder('M', seq);
                obj.ObjectID.encode(enc);
                obj.Schema.Key.encode(enc);
                enc.WriteStr8(name);

                if (args.Count < method.InputArgCount)
                {
                    throw new Exception(String.Format("Incorrect number of arguments: expected {0}, got{1}", method.InputArgCount, args.Count));
                }

                int argIndex = 0;
                foreach (SchemaArgument arg in method.Arguments)
                {
                    if (arg.IsInput())
                    {
                        ;
                        this.EncodeValue(enc, arg.Type, args[argIndex]);
                        argIndex += 1;
                    }
                }

                Message msg = aBroker.CreateMessage(enc, obj.RoutingKey(), timeToLive);

                if (synchronous)
                {
                    aBroker.SetSyncInFlight(true);
                }
                aBroker.Send(msg);
            }
            return(seq);
        }
Esempio n. 2
0
		protected long SendMethodRequest(QMFObject obj, Broker aBroker, string name, List<object> args, bool synchronous, int timeToLive) {
			
			SchemaMethod method = obj.Schema.GetMethod(name) ;
			if (args == null) {
				args = new List<object>() ;
			}
			
			long seq = 0 ;
			if (method != null) {
				KeyValuePair<SchemaMethod, bool> pair = new KeyValuePair<SchemaMethod, bool>(method, synchronous) ;
				seq = SequenceManager.Reserve(pair) ;
				IEncoder enc = aBroker.CreateEncoder('M', seq) ;
				obj.ObjectID.encode(enc) ;
				obj.Schema.Key.encode(enc) ;
				enc.WriteStr8(name) ;
				
				if (args.Count < method.InputArgCount) {
					throw new Exception(String.Format("Incorrect number of arguments: expected {0}, got{1}", method.InputArgCount, args.Count)) ;
				}
				
				int argIndex = 0 ;
				foreach (SchemaArgument arg in method.Arguments) {
					if (arg.IsInput()) {;						
						this.EncodeValue(enc, arg.Type, args[argIndex]) ;
						argIndex += 1 ;
					} 
				}
				
				Message msg = aBroker.CreateMessage(enc,obj.RoutingKey(),timeToLive) ;
				
				if (synchronous) {
					aBroker.SetSyncInFlight(true) ;
				}
				aBroker.Send(msg) ;
			}
			return seq ;
		}
Esempio n. 3
0
        public List <QMFObject> GetObjects(Dictionary <string, object> args)
        {
            List <Broker> brokerList = null;
            List <Agent>  agentList  = new List <Agent>();

            if (args.ContainsKey("_broker"))
            {
                brokerList = new List <Broker>();
                brokerList.Add((Broker)args["_broker"]);
            }
            else
            {
                brokerList = this.Brokers;
            }

            foreach (Broker broker in brokerList)
            {
                broker.WaitForStable();
            }

            if (args.ContainsKey("_agent"))
            {
                Agent agent = (Agent)args["_agent"];
                if (brokerList.Contains(agent.Broker))
                {
                    agentList.Add(agent);
                }
                else
                {
                    throw new Exception("Agent is not managed by this console or the supplied broker");
                }
            }
            else
            {
                if (args.ContainsKey("_objectId"))
                {
                    ObjectID oid = (ObjectID)args["_objectId"];
                    foreach (Broker broker in Brokers)
                    {
                        foreach (Agent agent in broker.Agents.Values)
                        {
                            if ((agent.AgentBank == oid.AgentBank()) && (agent.BrokerBank == oid.BrokerBank()))
                            {
                                agentList.Add(agent);
                            }
                        }
                    }
                }
                else
                {
                    foreach (Broker broker in brokerList)
                    {
                        foreach (Agent agent in broker.Agents.Values)
                        {
                            if (agent.Broker.IsConnected())
                            {
                                agentList.Add(agent);
                            }
                        }
                    }
                }
            }

            GetResult = new List <QMFObject>();

            if (agentList.Count > 0)
            {
                //FIXME Add a bunch of other suff too
                foreach (Agent agent in agentList)
                {
                    Dictionary <string, object> getParameters = new Dictionary <string, object>();
                    Broker broker = agent.Broker;
                    long   seq    = -1;
                    lock (LockObject) {
                        seq = SequenceManager.Reserve(Session.CONTEXT_MULTIGET);
                        SyncSequenceList.Add(seq);
                    }
                    object packageName = null;
                    object className   = null;
                    object key         = null;
                    object sClass      = null;
                    object oid         = null;
                    object hash        = null;

                    args.TryGetValue("_schema", out sClass);
                    args.TryGetValue("_key", out key);
                    args.TryGetValue("_class", out className);
                    args.TryGetValue("_package", out packageName);
                    args.TryGetValue("_objectID", out oid);
                    args.TryGetValue("_hash", out hash);

                    if ((className == null) && (oid == null) && (oid == null))
                    {
                        throw new Exception("No class supplied, use '_schema', '_key', '_class', or '_objectId' argument");
                    }

                    if (oid != null)
                    {
                        getParameters.Add("_objectID", oid);
                    }
                    else
                    {
                        if (sClass != null)
                        {
                            key = key ?? ((SchemaClass)sClass).Key;
                        }
                        if (key != null)
                        {
                            ClassKey cKey = (ClassKey)key;
                            className   = className ?? cKey.ClassName;
                            packageName = packageName ?? cKey.PackageName;
                            hash        = hash ?? cKey.Hash;
                        }

                        if (packageName != null)
                        {
                            getParameters.Add("_package", packageName);
                        }
                        if (className != null)
                        {
                            getParameters.Add("_class", className);
                        }
                        if (hash != null)
                        {
                            getParameters.Add("_hash", hash);
                        }
                        foreach (KeyValuePair <string, object> pair in args)
                        {
                            if (!pair.Key.StartsWith("_"))
                            {
                                getParameters.Add(pair.Key, pair.Value);
                            }
                        }
                    }

                    IEncoder enc = broker.CreateEncoder('G', seq);
                    enc.WriteMap(getParameters);
                    string  routingKey = String.Format("agent.{0}.{1}", agent.BrokerBank, agent.AgentBank);
                    Message msg        = broker.CreateMessage(enc, routingKey);
                    log.Debug("Get Object Keys: ");
                    foreach (string pKey in getParameters.Keys)
                    {
                        log.Debug(String.Format("\tKey: '{0}' Value: '{1}'", pKey, getParameters[pKey]));
                    }
                    broker.Send(msg);
                }

                int  waittime = DEFAULT_GET_WAIT_TIME;
                bool timeout  = false;
                if (args.ContainsKey("_timeout"))
                {
                    waittime = (int)args["_timeout"];
                }
                DateTime start = DateTime.Now;
                lock (LockObject) {
                    // FIXME ERROR
                    while (SyncSequenceList.Count > 0)
                    {
                        Monitor.Wait(LockObject, waittime);
                        TimeSpan duration = DateTime.Now - start;
                        if (duration.TotalMilliseconds > waittime)
                        {
                            foreach (long pendingSeq in SyncSequenceList)
                            {
                                SequenceManager.Release(pendingSeq);
                            }
                            SyncSequenceList.Clear();
                            timeout = true;
                        }
                    }
                }

                //FIXME Add the error logic
                if ((GetResult.Count == 0) && timeout)
                {
                    throw new Exception("Get Request timed out");
                }
            }
            return(GetResult);
        }