示例#1
0
        public override async Task SaveResponseToLocalCache(string c8oCallRequestIdentifier, C8oLocalCacheResponse localCacheResponse)
        {
            C8oFullSyncDatabase fullSyncDatabase = await GetOrCreateFullSyncDatabase(C8o.LOCAL_CACHE_DATABASE_NAME);

            Document localCacheDocument = fullSyncDatabase.Database.GetDocument(c8oCallRequestIdentifier);

            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties[C8o.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE]      = localCacheResponse.Response;
            properties[C8o.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.ResponseType;
            if (localCacheResponse.ExpirationDate > 0)
            {
                properties[C8o.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.ExpirationDate;
            }

            SavedRevision currentRevision = localCacheDocument.CurrentRevision;

            if (currentRevision != null)
            {
                properties[FULL_SYNC__REV] = currentRevision.Id;
            }

            localCacheDocument.PutProperties(properties);
        }
示例#2
0
 /// <summary>
 /// Saves the c8o call response into the local cache.
 /// </summary>
 /// <param name="c8oCallRequestIdentifier"></param>
 /// <param name="responseString"></param>
 /// <param name="responseType"></param>
 /// <param name="localCacheTimeToLive"></param>
 public abstract Task SaveResponseToLocalCache(string c8oCallRequestIdentifier, C8oLocalCacheResponse localCacheResponse);
示例#3
0
        //public override Object GetResponseFromLocalCache(string c8oCallRequestIdentifier)
        //{
        //    Dictionary<string, object> localCacheDocument = HandleGetDocumentRequest(C8o.LOCAL_CACHE_DATABASE_NAME, c8oCallRequestIdentifier) as Dictionary<string, object>;

        //    if (localCacheDocument == null)
        //    {
        //        throw new C8oUnavailableLocalCacheException(C8oExceptionMessage.ToDo());
        //    }


        //    string responsestring = "" + localCacheDocument[C8o.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE];
        //    string responseTypestring = "" + localCacheDocument[C8o.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE];
        //    Object expirationDate = localCacheDocument[C8o.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE];

        //    long expirationDateLong;

        //    if (expirationDate != null)
        //    {
        //        if (expirationDate is long)
        //        {
        //            expirationDateLong = (long) expirationDate;
        //            double currentTime = C8oUtils.GetUnixEpochTime(DateTime.Now);
        //            if (expirationDateLong < currentTime)
        //            {
        //                throw new C8oUnavailableLocalCacheException(C8oExceptionMessage.timeToLiveExpired());
        //            }
        //        }
        //        else
        //        {
        //            throw new C8oUnavailableLocalCacheException(C8oExceptionMessage.invalidLocalCacheResponseInformation());
        //        }
        //    }

        //    if (responseTypeString.Equals(C8o.RESPONSE_TYPE_JSON))
        //    {
        //        return C8oTranslator.StringToJson(responseString);
        //    }
        //    else if (responseTypeString.Equals(C8o.RESPONSE_TYPE_XML))
        //    {
        //        return C8oTranslator.StringToXml(responseString);
        //    }
        //    else
        //    {
        //        throw new C8oException(C8oExceptionMessage.ToDo());
        //    }
        //}

        public override /*async*/ Task SaveResponseToLocalCache(string c8oCallRequestIdentifier, C8oLocalCacheResponse localCacheResponse)
        {
            return(null);
        }
示例#4
0
        async private Task <object> HandleRequest()
        {
            bool isFullSyncRequest = C8oFullSync.IsFullSyncRequest(parameters);

            if (isFullSyncRequest)
            {
                c8o.Log._Debug("Is FullSync request");

                var liveid = C8oUtils.GetParameterStringValue(parameters, C8o.FS_LIVE, false);
                if (liveid != null)
                {
                    var dbName = C8oUtils.GetParameterStringValue(parameters, C8o.ENGINE_PARAMETER_PROJECT, true).Substring(C8oFullSync.FULL_SYNC_PROJECT.Length);
                    c8o.AddLive(liveid, dbName, this);
                }

                // The result cannot be handled here because it can be different depending to the platform
                // But it can be useful bor debug
                try
                {
                    var fullSyncResult = await c8o.c8oFullSync.HandleFullSyncRequest(parameters, c8oResponseListener);

                    return(fullSyncResult);
                }
                catch (C8oException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw new C8oException(C8oExceptionMessage.FullSyncRequestFail(), e);
                }
            }
            else
            {
                string responseType;
                if (c8oResponseListener == null || c8oResponseListener is C8oResponseXmlListener)
                {
                    responseType = C8o.RESPONSE_TYPE_XML;
                }
                else if (c8oResponseListener is C8oResponseJsonListener)
                {
                    responseType = C8o.RESPONSE_TYPE_JSON;
                }
                else
                {
                    return(new C8oException("wrong listener"));
                }

                //*** Local cache ***//

                string c8oCallRequestIdentifier = null;

                // Allows to enable or disable the local cache on a Convertigo requestable, default value is true
                C8oLocalCache localCache        = C8oUtils.GetParameterObjectValue(parameters, C8oLocalCache.PARAM, false) as C8oLocalCache;
                bool          localCacheEnabled = false;

                // If the engine parameter for local cache is specified
                if (localCache != null)
                {
                    // Removes local cache parameters and build the c8o call request identifier
                    parameters.Remove(C8oLocalCache.PARAM);

                    if (localCacheEnabled = localCache.enabled)
                    {
                        c8oCallRequestIdentifier = C8oUtils.IdentifyC8oCallRequest(parameters, responseType);

                        if (localCache.priority.IsAvailable(c8o))
                        {
                            try
                            {
                                C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier);

                                if (!localCacheResponse.Expired)
                                {
                                    if (responseType == C8o.RESPONSE_TYPE_XML)
                                    {
                                        return(C8oTranslator.StringToXml(localCacheResponse.Response));
                                    }
                                    else if (responseType == C8o.RESPONSE_TYPE_JSON)
                                    {
                                        return(C8oTranslator.StringToJson(localCacheResponse.Response));
                                    }
                                }
                            }
                            catch (C8oUnavailableLocalCacheException)
                            {
                                // no entry
                            }
                        }
                    }
                }

                //*** Get response ***//

                parameters[C8o.ENGINE_PARAMETER_DEVICE_UUID] = c8o.DeviceUUID;


                // Build the c8o call URL
                c8oCallUrl = c8o.Endpoint + "/." + responseType;

                HttpWebResponse httpResponse = null;
                Exception       exception    = null;
                try
                {
                    httpResponse = await c8o.httpInterface.HandleC8oCallRequest(c8oCallUrl, parameters);
                }
                catch (Exception e)
                {
                    exception = e;
                }

                if (exception != null)
                {
                    if (localCacheEnabled)
                    {
                        try
                        {
                            C8oLocalCacheResponse localCacheResponse = await c8o.c8oFullSync.GetResponseFromLocalCache(c8oCallRequestIdentifier);

                            if (!localCacheResponse.Expired)
                            {
                                if (responseType == C8o.RESPONSE_TYPE_XML)
                                {
                                    return(C8oTranslator.StringToXml(localCacheResponse.Response));
                                }
                                else if (responseType == C8o.RESPONSE_TYPE_JSON)
                                {
                                    return(C8oTranslator.StringToJson(localCacheResponse.Response));
                                }
                            }
                        }
                        catch (C8oUnavailableLocalCacheException)
                        {
                            // no entry
                        }
                    }
                    return(new C8oException(C8oExceptionMessage.handleC8oCallRequest(), exception));
                }

                var responseStream = httpResponse.GetResponseStream();

                object response;
                string responseString = null;
                if (c8oResponseListener is C8oResponseXmlListener)
                {
                    response = C8oTranslator.StreamToXml(responseStream);
                    if (localCacheEnabled)
                    {
                        responseString = C8oTranslator.XmlToString(response as XDocument);
                    }
                }
                else if (c8oResponseListener is C8oResponseJsonListener)
                {
                    responseString = C8oTranslator.StreamToString(responseStream);
                    response       = C8oTranslator.StringToJson(responseString);
                }
                else
                {
                    return(new C8oException("wrong listener"));
                }

                if (localCacheEnabled)
                {
                    // String responseString = C8oTranslator.StreamToString(responseStream);
                    long expirationDate = -1;
                    if (localCache.ttl > 0)
                    {
                        expirationDate = localCache.ttl + C8oUtils.GetUnixEpochTime(DateTime.Now);
                    }
                    var localCacheResponse = new C8oLocalCacheResponse(responseString, responseType, expirationDate);
                    await c8o.c8oFullSync.SaveResponseToLocalCache(c8oCallRequestIdentifier, localCacheResponse);
                }

                return(response);
            }
        }