private HermesMiddleware.DataAcquisitionServiceServer.SensorData[] ConvertProxy2Server(HermesMiddleware.DataAcquisitionServiceProxy.SensorData[] sourceSensorData)
        {
            HermesMiddleware.DataAcquisitionServiceServer.SensorData[] targetSensorData = null;

            if (sourceSensorData != null)
            {
                targetSensorData = new HermesMiddleware.DataAcquisitionServiceServer.SensorData[sourceSensorData.Length];
                for (int i = 0; i < sourceSensorData.Length; i++)
                {
                    targetSensorData[i]             = new HermesMiddleware.DataAcquisitionServiceServer.SensorData();
                    targetSensorData[i].baseStation = sourceSensorData[i].baseStation;
                    targetSensorData[i].sensorID    = sourceSensorData[i].sensorID;
                    targetSensorData[i].variable    = sourceSensorData[i].variable;

                    targetSensorData[i].dataArray = new HermesMiddleware.DataAcquisitionServiceServer.dataItem[sourceSensorData[i].dataArray.Length];

                    for (int j = 0; j < targetSensorData[i].dataArray.Length; j++)
                    {
                        targetSensorData[i].dataArray[j]           = new HermesMiddleware.DataAcquisitionServiceServer.dataItem();
                        targetSensorData[i].dataArray[j].value     = sourceSensorData[i].dataArray[j].value;
                        targetSensorData[i].dataArray[j].timestamp = sourceSensorData[i].dataArray[j].timestamp;
                    }
                }
            }

            return(targetSensorData);
        }
        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);
        }