private string[] _GetPeers(Global global, string sService, string sVariable)
        {
            string[] sPeerAddresses = null;

            if (global.bP2PMaster)
            {
                // this is P2P master => call directly GetPeers
                sPeerAddresses = global.GetPeers(sService, sVariable);
            }
            else
            {
                // ask P2P master for all peers publishing variable
                try
                {
                    // initialize service proxy
                    HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService serviceProxy = new HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService();
                    serviceProxy.Url = "http://" + global.sP2PMaster + ":" + global.portNumber + global.sVirtRoot + global.sASMX;

                    // call service proxy
                    sPeerAddresses = serviceProxy.GetPeers(sService, sVariable);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp);
                }
            }

            return(sPeerAddresses);
        }
        public override HermesMiddleware.DataAcquisitionServiceServer.SensorData[] Query(string service, [System.Xml.Serialization.XmlElementAttribute("expressions")] HermesMiddleware.DataAcquisitionServiceServer.ExpressionType[] expressions, bool delegateCall)
        {
            ArrayList sensorDataArray = new ArrayList();

            if (expressions == null || expressions.Length == 0 || expressions[0].Items == null)
            {
                return(null);
            }

            // get global stuff (cross-application-domain)
            Global global = (Global)Global.GetObject(Global.sClassName, Global.port);

            // get addresses of peers publishing the variable
            string[] sPeerAddresses = _GetPeers(global, service, expressions[0].Items[0].variable);
            if (expressions[0].Items[0].variable == "x" || expressions[0].Items[0].variable == "y" || expressions[0].Items[0].variable == "z")
            {
                sPeerAddresses = _GetPeers(global, service, "Position");
            }

            if (sPeerAddresses != null)
            {
                foreach (string sPeerAddress in sPeerAddresses)
                {
                    // get first part of name (e.g. PCC085C.us008.siemens.net)
                    string[] sParts = sPeerAddress.Split('.');

                    if (String.Compare(sParts[0], Environment.MachineName, true) == 0)
                    {
                        // peer is this machine => perform query
                        global.Query(service, expressions, ref sensorDataArray);
                    }
                    else
                    {
                        if (delegateCall)
                        {
                            try
                            {
                                // peer is different machine => delegate it to its DataAcquisitionService

                                // initialize service proxy
                                HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService serviceProxy = new HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService();
                                serviceProxy.Url = "http://" + sPeerAddress + ":" + global.portNumber + global.sVirtRoot + global.sASMX;

                                HermesMiddleware.DataAcquisitionServiceProxy.ExpressionType[] tmpExpressions = new HermesMiddleware.DataAcquisitionServiceProxy.ExpressionType[expressions.Length];
                                for (int i = 0; i < expressions.Length; i++)
                                {
                                    tmpExpressions[i] = ConvertServer2Proxy(expressions[i]);
                                }

                                // call query (false = do not delegate further)
                                HermesMiddleware.DataAcquisitionServiceProxy.SensorData[]  sensorDataProxy = serviceProxy.Query(service, tmpExpressions, false);
                                HermesMiddleware.DataAcquisitionServiceServer.SensorData[] tmpSensorData   = this.ConvertProxy2Server(sensorDataProxy);

                                if (tmpSensorData != null && tmpSensorData.Length > 0)
                                {
                                    // add data to array
                                    foreach (HermesMiddleware.DataAcquisitionServiceServer.SensorData data in tmpSensorData)
                                    {
                                        sensorDataArray.Add(data);
                                    }
                                }
                            }
                            catch (Exception exp)
                            {
                                Console.WriteLine(exp);
                            }
                        }
                    }
                }
            }

            // copy result to simple array
            HermesMiddleware.DataAcquisitionServiceServer.SensorData[] sensorData = new HermesMiddleware.DataAcquisitionServiceServer.SensorData[sensorDataArray.Count];
            for (int i = 0; i < sensorDataArray.Count; i++)
            {
                sensorData[i] = (HermesMiddleware.DataAcquisitionServiceServer.SensorData)sensorDataArray[i];
            }

            return(sensorData);
        }
        public override void Unsubscribe(string service, [System.Xml.Serialization.XmlElementAttribute("subscriptions")] HermesMiddleware.DataAcquisitionServiceServer.Subscription[] subscriptions, bool delegateCall)
        {
            // get global stuff (cross-application-domain)
            Global global = (Global)Global.GetObject(Global.sClassName, Global.port);

            // get addresses of peers (variable is null => we want all peers for given service)
            string[] sPeerAddresses = _GetPeers(global, service, null);

            if (sPeerAddresses != null)
            {
                foreach (string sPeerAddress in sPeerAddresses)
                {
                    // get first part of name (e.g. PCC085C.us008.siemens.net)
                    string[] sParts = sPeerAddress.Split('.');

                    if (String.Compare(sParts[0], Environment.MachineName, true) == 0)
                    {
                        // peer is this machine => remove subscriptions
                        foreach (HermesMiddleware.DataAcquisitionServiceServer.Subscription subscription in subscriptions)
                        {
                            // remove subscriber
                            global.RemoveSubscriber(subscription.GUID);
                        }
                    }
                    else
                    {
                        if (delegateCall)
                        {
                            try
                            {
                                // peer is different machine => delegate it to its DataAcquisitionService

                                // initialize service proxy
                                HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService serviceProxy = new HermesMiddleware.DataAcquisitionServiceProxy.DataAcquisitionService();
                                serviceProxy.Url = "http://" + sPeerAddress + ":" + global.portNumber + global.sVirtRoot + global.sASMX;

                                HermesMiddleware.DataAcquisitionServiceProxy.Subscription[] tmpSubscriptions = new HermesMiddleware.DataAcquisitionServiceProxy.Subscription[subscriptions.Length];
                                for (int i = 0; i < subscriptions.Length; i++)
                                {
                                    tmpSubscriptions[i] = ConvertServer2Proxy(subscriptions[i]);
                                }

                                // unsubscribe
                                serviceProxy.Unsubscribe(service, tmpSubscriptions, false);
                            }
                            catch (Exception exp)
                            {
                                Console.WriteLine(exp);
                            }
                        }
                    }
                }
            }
        }