Пример #1
0
        /// <summary>
        /// Fetches a single result for the specified <paramref name="topic"/> and <paramref name="partitionID"/>.
        /// </summary>
        /// <param name="router">Router used to fetch result.</param>
        /// <param name="topic">Topic to fetch result from.</param>
        /// <param name="partitionID">Partition ID to fetch result from.</param>
        /// <param name="offset">Offset of <paramref name="topic"/> to begin fetching result.</param>
        /// <param name="maxBytes">Defines maximum size of request that can be adjusted for large serializations.</param>
        /// <returns>A <see cref="FetchResponse"/> instance, if available; otherwise, <c>null</c>.</returns>
        public static FetchResponse Fetch(this BrokerRouter router, string topic, int partitionID = 0, long offset = 0, int maxBytes = 32768)
        {
            if ((object)router == null)
                throw new ArgumentNullException(nameof(router));

            if (string.IsNullOrWhiteSpace(topic))
                throw new ArgumentNullException(nameof(topic));

            // Create a fetch request with a single item to request
            FetchRequest request = new FetchRequest()
            {
                Fetches = new List<Fetch>(new[]
                {
                    new Fetch
                    {
                        Topic = topic,
                        PartitionId = partitionID,
                        Offset = offset,
                        MaxBytes = maxBytes
                    }
                })
            };

            return router.SelectBrokerRoute(topic, partitionID).Connection.SendAsync(request).Result.FirstOrDefault();
        }
Пример #2
0
      public rrd4n.DataAccess.Data.FetchData GetData(rrd4n.DataAccess.Data.FetchRequest request)
      {
         RrdDb rrdDb = null;
         try
         {
            string dataPath;
            if (DataPath.Contains("${APPDATA}"))
            {
               dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
               dataPath += DataPath.Substring(10);
            }
            else
               dataPath = DataPath;


            rrdDb = new RrdDb(dataPath + request.DatabasePath);
            FetchRequest coreRequest = new FetchRequest(rrdDb.getPath(), request.ConsolidateFunctionName, request.FetchStart, request.FetchEnd, request.Resolution);
            FetchData coreFetchData = rrdDb.fetchData(coreRequest);
            
            return new rrd4n.DataAccess.Data.FetchData(coreFetchData.getArcStep(), coreFetchData.getArcEndTime(), coreFetchData.getDsNames())
                                                           {
                                                              Values = coreFetchData.getValues(),
                                                              Timestamps = coreFetchData.getTimestamps()
                                                           };
         }
         finally
         {
            if (rrdDb != null)
               rrdDb.close();
         }
      }
        public async Task ProtocolGateway()
        {
            int partitionId = 0;
            var router = new BrokerRouter(Options);

            var producer = new Producer(router);
            string messge1 = Guid.NewGuid().ToString();
            var respose = await producer.SendMessageAsync(IntegrationConfig.IntegrationTopic, new[] { new Message(messge1) }, 1, null, MessageCodec.CodecNone, partitionId);
            var offset = respose.FirstOrDefault().Offset;

            ProtocolGateway protocolGateway = new ProtocolGateway(IntegrationConfig.IntegrationUri);
            var fetch = new Fetch
                         {
                             Topic = IntegrationConfig.IntegrationTopic,
                             PartitionId = partitionId,
                             Offset = offset,
                             MaxBytes = 32000,
                         };

            var fetches = new List<Fetch> { fetch };

            var fetchRequest = new FetchRequest
                {
                    MaxWaitTime = 1000,
                    MinBytes = 10,
                    Fetches = fetches
                };

            var r = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId);
            //  var r1 = await protocolGateway.SendProtocolRequest(fetchRequest, IntegrationConfig.IntegrationTopic, partitionId);
            Assert.IsTrue(r.Messages.FirstOrDefault().Value.ToUtf8String() == messge1);
        }
Пример #4
0
        public async Task ShouldTryToRefreshMataDataIfCanRecoverByRefreshMetadata(ErrorResponseCode code)
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = new TimeSpan(10);
            var router = routerProxy.Create();

            int partitionId = 0;

            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();
            bool sendExOnFirstTime = true;

            Func<Task<FetchResponse>> ShouldReturnErrorAndThenNoError = async () =>
            {
                Task.Delay(routerProxy._cacheExpiration).Wait();
                Task.Delay(1).Wait();
                if (sendExOnFirstTime)
                {
                    sendExOnFirstTime = false;
                    return new FetchResponse() { Error = (short)code };
                }
                return new FetchResponse() { Error = (short)ErrorResponseCode.NoError };
            };
            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnErrorAndThenNoError;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse;

            await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);

            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2));
            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(2));
        }
        private static void AddNexus(AccountSvc svc, int companyId)
        {
            FetchRequest nexusFetch = new FetchRequest();
            nexusFetch.Filters = "CompanyId=" + companyId;

            nexusFetch.Filters = "CompanyId=1,EndDate='12/31/9999'";//Gets master list of possible nexus
            NexusFetchResult databaseNexus = svc.NexusFetch(nexusFetch);

            Nexus[] to_add = {databaseNexus.Nexuses[2], databaseNexus.Nexuses[7], databaseNexus.Nexuses[10] }; //I've just selected some values to add at random, this would be informed by your user input matched against the master list to find the
                                                // correct jurisdiction. You should compare the Nexus.JurisName. You don't have to add US nexus to add states throught the API, it adds the country nexus automatically.

            foreach (Nexus n in to_add)
            {
                Nexus newNexus = new Nexus();
                newNexus = n;
                newNexus.CompanyId = companyId;
                newNexus.NexusId = 0; //Always set to 0 for a new nexus, otherwise use the fetched NexusId value to edit an existing nexus.
                newNexus.EffDate = DateTime.Today.AddYears(-1); //I'm setting the default start/end dates the same way the Admin Console will, user should specify.
                newNexus.EndDate = DateTime.MaxValue;
                newNexus.LocalNexusTypeId = LocalNexusTypeId.Selected; //This may vary based on the jurisdiction and needs of the client.
                NexusSaveResult res = svc.NexusSave(newNexus);
                if (res.ResultCode.Equals(SeverityLevel.Success))
                {
                    Console.WriteLine("Nexus saved for: " + newNexus.JurisName);
                }
                else{
                    Console.WriteLine("Nexus NOT saved for: " + newNexus.JurisName +
                        " //Error: " + res.Messages[0].Summary);
                }
            }
        }
Пример #6
0
 public async Task ShouldThrowFormatExceptionWhenTopicIsInvalid()
 {
     var routerProxy = new BrokerRouterProxy(_kernel);
     var router = routerProxy.Create();
     string invalidTopic = " ";
     var fetchRequest = new FetchRequest();
     ProtocolGateway protocolGateway = new ProtocolGateway(router);
     await protocolGateway.SendProtocolRequest(fetchRequest, invalidTopic, 0);
 }
        public void EnsureHeaderShouldPackCorrectByteLengths()
        {
            var encoder = new KafkaEncoder(new byte[14]);
            var request = new FetchRequest { ClientId = "test", CorrelationId = 123456789 };

            request.EncodeHeader(encoder);
            Assert.That(encoder.Offset, Is.EqualTo(14));
            Assert.That(encoder.Buffer, Is.EqualTo(new byte[] { 0, 1, 0, 0, 7, 91, 205, 21, 0, 4, 116, 101, 115, 116 }));
        }
Пример #8
0
		public FetchData(Archive matchingArchive, FetchRequest request)
		{
			this.matchingArchive = matchingArchive;
			arcStep = matchingArchive.TimeStep;
			arcEndTime = matchingArchive.GetEndTime();
			dsNames = request.GetFilter();
			if (dsNames == null)
			{
				dsNames = matchingArchive.ParentDb.DataSourceNames;
			}
			this.request = request;
		}
        static void loadBatch(BatchSvc svc, int m_CompanyID)
        {
            Batch oBatch = new Batch();
            oBatch.CompanyId = m_CompanyID;

            oBatch.BatchTypeId = "TransactionImport"; //This is required, will tell our service what you're importing.
            BatchFile file = new BatchFile();
            oBatch.Name = "test.csv";
            file.Name = oBatch.Name; //You need to set a Batch.Name and a Batch.BatchFile.Name, but they can be the same.
            string path = "C:\\Analysis\\test\\test1.csv"; //This is your filepath for the file you want to load
            file.ContentType = "application/CSV"; //You can only load CSV, XLS, or XLSX files.
            file.Ext = ".csv";
            file.FilePath = path;

            //Here I'm just loading in the data from the csv file to the BatchFile
            try
            {
                    FileStream oStream = new FileStream(path, FileMode.Open, FileAccess.Read);
                    BinaryReader oBinreader = new BinaryReader(oStream);
                    file.Size = (Int32)oStream.Length;
                    file.Content = oBinreader.ReadBytes(file.Size);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            oBatch.Files = new BatchFile[1];
            oBatch.Files[0] = file; //It looks like you can load an array of files, but you can really only save the batch with one file in the array.
            BatchSaveResult sres = new BatchSaveResult();
            sres = svc.BatchSave(oBatch); //Calling the service with the batch

            Console.WriteLine(sres.ResultCode.ToString());

            //fetching the batch we just loaded
            FetchRequest ofetchRequest = new FetchRequest();
            ofetchRequest.Fields = "Files";
            ofetchRequest.Filters = "BatchId = " + sres.BatchId.ToString();

            BatchFetchResult result = svc.BatchFetch(ofetchRequest); //this BatchFetch is to find the BatchFileId values

            ofetchRequest.Fields = "*,Content"; //since the BatchFileFetch also uses a fetchRequest, I recycled my variable.
            ofetchRequest.Filters = "BatchFileId = " + result.Batches[0].Files[1].BatchFileId.ToString(); //I just picked one of files from the result here.
            //There can be up to three files returned: input, result, and error.

            BatchFileFetchResult fetchresult = svc.BatchFileFetch(ofetchRequest);
            Console.WriteLine(Encoding.ASCII.GetString(fetchresult.BatchFiles[0].Content));//The file content is returned as a byte array, and I just wrote it to a console window to confirm that it looked OK.
        }
Пример #10
0
      public RemoteFetchData FetchData(string databaseName, long startTimeTick, long endTimeTick, string consolFunctionName, long resolution)
      {
         RrdDb database = null;
         try
         {
            log.DebugFormat("Read data from {0}", databaseName);

            var nameValueCollection = (NameValueCollection)ConfigurationManager.GetSection("rrdbfileserver");
            string dataBasePath = nameValueCollection["databasepath"];

            log.DebugFormat("Database path:{0}",dataBasePath + databaseName);
            database = new RrdDb(dataBasePath + databaseName, true);
            FetchRequest fetchRequest = new FetchRequest(null, consolFunctionName, startTimeTick, endTimeTick, resolution);
            FetchData data = database.fetchData(fetchRequest);
            database.close();

            RemoteFetchData remoteData = new RemoteFetchData();
            remoteData.Timestamps = data.getTimestamps();
            remoteData.Values = data.getValues();
            remoteData.ArchiveEndTimeTicks = data.getArcEndTime();
            remoteData.ArchiveSteps = data.getArcStep();
            remoteData.DatasourceNames = data.getDsNames();
            if (debugEnabled)
               log.DebugFormat("Read data from {0} to {1}.", rrd4n.Common.Util.getDate(startTimeTick), rrd4n.Common.Util.getDate(endTimeTick));
            return remoteData;
         }
         catch (Exception ex)
         {
            log.Error("Fetchdata exception", ex);
            throw;
         }
         finally
         {
            if (database != null)
               database.close();
         }
      }
Пример #11
0
        private FetchRequest CreateFetchRequest(long offset)
        {
            var fetch = new Fetch() {
                Offset = offset,
                PartitionId = _partitionId,
                Topic = _topic,
                MaxBytes = _maxSizeOfMessageSet
            };

            var request = new FetchRequest()
            {
                MaxWaitTime = MaxWaitTimeForKafka,
                MinBytes = 0,
                Fetches = new List<Fetch>() { fetch },
                ClientId = _clientId
            };

            return request;
        }
Пример #12
0
        public SimplePlaneStorageContext(double[] lats, double[] lons, double[] vals, FetchRequest request)
        {
            this.lats = lats;
            this.lons = lons;
            this.vals = vals;

            this.request = request;
            data.AddVariable <double>("lat", lats, "i");
            data.AddVariable <double>("lon", lons, "i");
            data.AddVariable <double>("val", vals, "i");
        }
Пример #13
0
        public ActionResult LogIn()
        {
            OpenIdRelyingParty openid = new OpenIdRelyingParty();

            openid.SecuritySettings.AllowDualPurposeIdentifiers = true;
            var response = openid.GetResponse();

            if (response == null)
            {
                IAuthenticationRequest request = openid.CreateRequest(
                    "https://www.e-contract.be/eid-idp/endpoints/openid/ident"
                    //"https://www.e-contract.be/eid-idp/endpoints/openid/auth"
                    );
                Debug.WriteLine("Sending to EID");

                // attribute query
                FetchRequest fetchRequest = new FetchRequest();
                fetchRequest.Attributes.AddRequired("http://axschema.org/namePerson");
                fetchRequest.Attributes.AddRequired("http://axschema.org/namePerson/middle");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/card-validity/end");
                fetchRequest.Attributes.AddRequired("http://axschema.org/person/gender");
                fetchRequest.Attributes.AddRequired("http://axschema.org/contact/postalAddress/home");
                fetchRequest.Attributes.AddRequired("http://axschema.org/namePerson/first");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/photo");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/card-validity/begin");
                fetchRequest.Attributes.AddRequired("http://axschema.org/contact/city/home");
                fetchRequest.Attributes.AddRequired("http://axschema.org/contact/postalCode/home");
                fetchRequest.Attributes.AddRequired("http://openid.net/schema/birthDate/birthYear");
                fetchRequest.Attributes.AddRequired("http://openid.net/schema/birthDate/birthMonth");
                fetchRequest.Attributes.AddRequired("http://openid.net/schema/birthDate/birthday");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/pob");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/card-number");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/nationality");
                fetchRequest.Attributes.AddRequired("http://axschema.org/namePerson/last");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/rrn");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/cert/auth");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/age");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/documentType");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/nobleCondition");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/card-delivery-municipality");
                fetchRequest.Attributes.AddRequired("http://axschema.org/eid/chip-number");
                request.AddExtension(fetchRequest);

                return(request.RedirectingResponse.AsActionResultMvc5());
            }
            else
            {
                switch (response.Status)
                {
                case AuthenticationStatus.Authenticated:
                    FetchResponse fetchResponse = response.GetExtension <FetchResponse>();
                    Debug.WriteLine(fetchResponse.Attributes["http://axschema.org/eid/photo"].Values[0]);
                    EidCard eid = new EidCard
                    {
                        ChipNumber               = fetchResponse.Attributes["http://axschema.org/eid/chip-number"].Values[0],
                        DocumentType             = fetchResponse.Attributes["http://axschema.org/eid/documentType"].Values[0],
                        NobleCondition           = fetchResponse.Attributes["http://axschema.org/eid/nobleCondition"].Values[0],
                        CardDeliveryMunicipality = fetchResponse.Attributes["http://axschema.org/eid/card-delivery-municipality"].Values[0],
                        AddressCity              = fetchResponse.Attributes["http://axschema.org/contact/city/home"].Values[0],
                        AddressStreet            = fetchResponse.Attributes["http://axschema.org/contact/postalAddress/home"].Values[0],
                        AddressPostal            = fetchResponse.Attributes["http://axschema.org/contact/postalCode/home"].Values[0],
                        Age             = Convert.ToInt32(fetchResponse.Attributes["http://axschema.org/eid/age"].Values[0]),
                        CardNumber      = fetchResponse.Attributes["http://axschema.org/eid/card-number"].Values[0],
                        CertificateAuth = fetchResponse.Attributes["http://axschema.org/eid/cert/auth"].Values[0],
                        Nationality     = fetchResponse.Attributes["http://axschema.org/eid/nationality"].Values[0],
                        LastName        = fetchResponse.Attributes["http://axschema.org/namePerson/last"].Values[0],
                        RNN             = fetchResponse.Attributes["http://axschema.org/eid/rrn"].Values[0],
                        //We receive a bad base64 encoded image from e-contract.be
                        //We need to replace "_" to "/" and "-" to "+"
                        Photo         = fetchResponse.Attributes["http://axschema.org/eid/photo"].Values[0].Replace('_', '/').Replace("-", "+"),
                        FirstName     = fetchResponse.Attributes["http://axschema.org/namePerson/first"].Values[0],
                        MiddleName    = fetchResponse.Attributes["http://axschema.org/namePerson/middle"].Values[0],
                        Gender        = fetchResponse.Attributes["http://axschema.org/person/gender"].Values[0],
                        POB           = fetchResponse.Attributes["http://axschema.org/eid/pob"].Values[0],
                        ValidityEnd   = DateTime.Parse(fetchResponse.Attributes["http://axschema.org/eid/card-validity/end"].Values[0]),
                        ValidityBegin = DateTime.Parse(fetchResponse.Attributes["http://axschema.org/eid/card-validity/begin"].Values[0]),
                        BirthDay      = new DateTime(
                            Convert.ToInt32(fetchResponse.Attributes["http://openid.net/schema/birthDate/birthYear"].Values[0]),
                            Convert.ToInt32(fetchResponse.Attributes["http://openid.net/schema/birthDate/birthMonth"].Values[0]),
                            Convert.ToInt32(fetchResponse.Attributes["http://openid.net/schema/birthDate/birthday"].Values[0]))
                    };
                    Session["eid"]       = eid;
                    Session["connected"] = true;
                    return(RedirectToAction("Index", "PS"));

                case AuthenticationStatus.Canceled:
                    ViewBag.Extra = Resources.Global.Error_log_in_cancel;
                    break;

                case AuthenticationStatus.Failed:
                    ViewBag.Extra = Resources.Global.Error_log_in_failed;
                    break;
                }
                return(View("Error"));
            }
        }
Пример #14
0
        public async Task ShouldRecoverFromFailerByUpdateMetadataOnceFullScenario1()
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(0);
            var router = routerProxy.Create();
            int partitionId = 0;
            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            CreateSuccessfulSendMock(routerProxy);

            //Send Successful Message
            await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);

            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount");
            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(1), "MetadataRequestCallCount");
            Assert.That(routerProxy.BrokerConn1.MetadataRequestCallCount, Is.EqualTo(0), "MetadataRequestCallCount");

            routerProxy.BrokerConn0.FetchResponseFunction = FailedInFirstMessageError(ErrorResponseCode.LeaderNotAvailable, TimeSpan.Zero);

            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithSingleBroker;
            routerProxy.BrokerConn1.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithSingleBroker;

            //Reset variables
            routerProxy.BrokerConn0.FetchRequestCallCount = 0;
            routerProxy.BrokerConn1.FetchRequestCallCount = 0;
            routerProxy.BrokerConn0.MetadataRequestCallCount = 0;
            routerProxy.BrokerConn1.MetadataRequestCallCount = 0;

            //Send Successful Message that was recover from exception
            await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);

            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount");
            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(1), "MetadataRequestCallCount");

            Assert.That(routerProxy.BrokerConn1.FetchRequestCallCount, Is.EqualTo(1), "FetchRequestCallCount");
            Assert.That(routerProxy.BrokerConn1.MetadataRequestCallCount, Is.EqualTo(0), "MetadataRequestCallCount");
        }
Пример #15
0
        public async Task SendProtocolRequestShouldThrowException(Type exceptionType)
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router = routerProxy.Create();

            int partitionId = 0;
            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            bool firstTime = true;
            Func<Task<FetchResponse>> ShouldReturnError = async () =>
            {
                if (firstTime)
                {
                    firstTime = !firstTime;
                    object[] args = new object[1];
                    args[0] = "error Test";
                    throw (Exception)Activator.CreateInstance(exceptionType, args);
                }
                return new FetchResponse() { Error = (short)ErrorResponseCode.NoError };
            };

            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse;
            try
            {
                await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);
                Assert.IsTrue(false, "Should throw exception");
            }
            catch (Exception ex)
            {
                Assert.That(ex.GetType(), Is.EqualTo(exceptionType));
            }
        }
Пример #16
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var openIdRelyingParty = new OpenIdRelyingParty();
            var openid_identifier  = ConfigurationManager.AppSettings["openid_identifier"];
            var returnUrl          = "~/OpenID/Connect";
            var response           = openIdRelyingParty.GetResponse();

            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(openid_identifier, out id))
                {
                    IAuthenticationRequest request = openIdRelyingParty.CreateRequest(openid_identifier);
                    FetchRequest           fetch   = new FetchRequest();
                    fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email));
                    fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.FullName));
                    fetch.Attributes.Add(new AttributeRequest("http://axschema.org/intuit/realmId"));
                    request.AddExtension(fetch);
                    request.RedirectToProvider();
                }
            }
            else
            {
                if (response.FriendlyIdentifierForDisplay == null)
                {
                    Response.Redirect("~/OpenID/Connect");
                }

                // Stage 3: OpenID Provider sending assertion response
                //Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                FetchResponse fetch = response.GetExtension <FetchResponse>();
                if (fetch != null)
                {
                    var openIdEmail    = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                    var openIdFullName = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
                    var openIdRealmId  = fetch.GetAttributeValue("http://axschema.org/intuit/realmId");

                    string userName = Membership.GetUserNameByEmail(openIdEmail);
                    if (userName == null)
                    {
                        Membership.CreateUser(openIdEmail, Guid.NewGuid().ToString(), openIdEmail);
                        FormsAuthentication.SetAuthCookie(openIdEmail, true);
                        if (Request.QueryString["Subscribe"] != null)
                        {
                            String csname           = "DirectConnectScript";
                            Type   cstype           = this.GetType();
                            ClientScriptManager csm = Page.ClientScript;

                            // Check to see if the startup script is already registered.
                            if (!csm.IsStartupScriptRegistered(cstype, csname))
                            {
                                StringBuilder cstext = new StringBuilder();
                                cstext.AppendLine("<script>");
                                cstext.AppendLine("$(document).ready(function () {");
                                cstext.AppendLine("intuit.ipp.anywhere.directConnectToIntuit();");
                                cstext.AppendLine("});");
                                cstext.AppendLine("</script>");
                                csm.RegisterStartupScript(cstype, csname, cstext.ToString());
                            }
                        }
                    }
                    else if (Request.QueryString["Disconnect"] != null)
                    {
                        RestHelper.clearProfile(RestProfile.GetRestProfile());
                        Response.Redirect("~/ManageConnection");
                    }
                    else if (userName != null)
                    {
                        FormsAuthentication.SetAuthCookie(userName, true);
                        if (!string.IsNullOrEmpty(returnUrl))
                        {
                            Response.Redirect("~/Default");
                        }
                    }
                }
            }
        }
        public ActionResult OpenIdLogOn(string returnUrl)
        {
            if (OAMSSetting.ByPassLogin && !Request.IsAuthenticated)
            {
                string username = repo.Create_ByPassLogin();
                this.IssueAuthTicket(username, true);

                if (string.IsNullOrEmpty(returnUrl))
                {
                    returnUrl = "~/";
                }

                return(Redirect(returnUrl));
            }
            else
            {
                var openid   = new OpenIdRelyingParty();
                var response = openid.GetResponse();
                if (response == null)  // Initial operation
                {
                    // Step 1 - Send the request to the OpenId provider server
                    string openid_identifier = "https://www.google.com/accounts/o8/id";
                    //Identifier id;


                    //if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                    //{
                    //    try
                    //    {
                    //        var req = openid.CreateRequest(Request.Form["openid_identifier"]);
                    //        return req.RedirectingResponse.AsActionResult();
                    //    }
                    //    catch (ProtocolException ex)
                    //    {
                    //        // display error by showing original LogOn view
                    //        //this.ErrorDisplay.ShowError("Unable to authenticate: " + ex.Message);
                    //        return View("Logon");
                    //    }
                    //}
                    //else
                    //{
                    //    // display error by showing original LogOn view
                    //    //this.ErrorDisplay.ShowError("Invalid identifier");
                    //    //return View("LogOn", this.ViewModel);
                    //    return View("LogOn");
                    //}


                    try
                    {
                        var req = openid.CreateRequest(openid_identifier);

                        var fetch = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);

                        req.AddExtension(fetch);


                        return(req.RedirectingResponse.AsActionResult());
                    }
                    catch (ProtocolException)
                    {
                        // display error by showing original LogOn view
                        //this.ErrorDisplay.ShowError("Unable to authenticate: " + ex.Message);
                        return(View("Logon"));
                    }
                }
                else  // OpenId redirection callback
                {
                    // Step 2: OpenID Provider sending assertion response
                    switch (response.Status)
                    {
                    case AuthenticationStatus.Authenticated:
                        string identifier = response.ClaimedIdentifier;

                        var    fetch    = response.GetExtension <FetchResponse>();
                        string email    = string.Empty;
                        string fullname = string.Empty;
                        if (fetch != null)
                        {
                            email    = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                            fullname = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
                        }

                        if (repo.Exist(email, identifier))
                        {
                        }
                        else
                        {
                            repo.Create(email, identifier);
                        }

                        // OpenId lookup fails - Id doesn't exist for login - login first
                        //if (busUser.ValidateUserOpenIdAndLoad(identifier) == null)
                        //{
                        //    //this.ErrorDisplay.HtmlEncodeMessage = false;
                        //    //this.ErrorDisplay.ShowError(busUser.ErrorMessage +
                        //    //        "Please <a href='" + WebUtils.ResolveUrl("~/Account/Register") +
                        //    //        "'>register</a> to create a new account or <a href='" +
                        //    //        WebUtils.ResolveUrl("~/Account/Register") +
                        //    //        "'>associate</a> an existing account with your OpenId");

                        //    //return View("LogOn", this.ViewModel);
                        //    return View("LogOn");
                        //}

                        // Capture user information for AuthTicket
                        // and issue Forms Auth token
                        //UserState userState = new UserState()
                        //{
                        //    Email = busUser.Entity.Email,
                        //    Name = busUser.Entity.Name,
                        //    UserId = busUser.Entity.Id,
                        //    IsAdmin = busUser.Entity.IsAdmin
                        //};
                        //this.IssueAuthTicket(userState, true);

                        this.IssueAuthTicket(email, true);

                        RoleRepository roleRepo = new RoleRepository();
                        if (roleRepo.GetRolesList(email).Count() == 0)
                        {
                            returnUrl = "~/Account/Guest";
                        }

                        if (string.IsNullOrEmpty(returnUrl))
                        {
                            returnUrl = "~/";
                        }

                        return(Redirect(returnUrl));

                    case AuthenticationStatus.Canceled:
                        //this.ErrorDisplay.ShowMessage("Canceled at provider");
                        //return View("LogOn", this.ViewModel);
                        return(View("LogOn"));

                    case AuthenticationStatus.Failed:
                        //this.ErrorDisplay.ShowError(response.Exception.Message);
                        //return View("LogOn", this.ViewModel);
                        return(View("LogOn"));
                    }
                }
            }
            return(new EmptyResult());
        }
Пример #18
0
        private ActionResult AuthenticateWithOpenId(string url, string returnUrl)
        {
            OpenIdRelyingParty party = new OpenIdRelyingParty();

            var response = party.GetResponse();

            if (response == null)
            {
                if (Identifier.TryParse(url, out Identifier id))
                {
                    try
                    {
                        var request = party.CreateRequest(url);
                        if (!returnUrl.IsNullOrEmpty())
                        {
                            request.AddCallbackArguments("returnUrl", returnUrl);
                        }
                        var fetch = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                        request.AddExtension(fetch);
                        return(request.RedirectingResponse.AsActionResultMvc5());
                    }
                    catch
                    {
                        return(View("Login"));
                    }
                }
                return(RedirectToAction("Login"));
            }

            switch (response.Status)
            {
            case AuthenticationStatus.Authenticated:
                var    fetch     = response.GetExtension <FetchResponse>();
                string firstName = "unknown";
                string lastName  = "unknown";
                string email     = "unknown";
                if (fetch != null)
                {
                    firstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First);
                    lastName  = fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
                    email     = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                }

                var lp       = LoginProvider.Internal;
                var provider = response.Provider.Uri.AbsoluteUri.ToLower();
                if (provider.Contains("google.com"))
                {
                    lp = LoginProvider.Google;
                }

                var user = userService.GetUser(response.ClaimedIdentifier, lp);
                if (user == null)
                {
                    user = new User
                    {
                        RemoteId      = response.ClaimedIdentifier,
                        UserName      = email,
                        Email         = email,
                        FirstName     = firstName,
                        LastName      = lastName,
                        LoginProvider = lp
                    };
                    user.Id = userService.InsertUser(user, () => Redis.AddUser(user));
                }

                FormsAuthentication.SetAuthCookie(user.Id.ToString(), createPersistentCookie: true);
                if (!returnUrl.IsNullOrEmpty())
                {
                    Response.Redirect(returnUrl, true);
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
                break;
            }
            return(RedirectToAction("Login"));
        }
Пример #19
0
        public async Task TestFetchingWorksOk()
        {
            using (var temporaryTopic = testCluster.CreateTemporaryTopic())
            using (var connection = await KafkaConnectionFactory.CreateSimpleKafkaConnectionAsync(testCluster.CreateBrokerUris()[0]))
            {
                var request = new FetchRequest
                {
                    MaxWaitTime = 0,
                    MinBytes = 1000,
                    Fetches = new List<Fetch>
                     {
                         new Fetch
                         {
                              Topic = temporaryTopic.Name,
                              PartitionId = 0,
                              MaxBytes = 1024,
                              Offset = 0
                         }
                     }
                };

                var response = await connection.SendRequestAsync(request, CancellationToken.None);
                Assert.That(response, Has.Count.EqualTo(1));
                var first = response.First();
                Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                Assert.That(first.Messages, Has.Count.EqualTo(0));
            }
        }
Пример #20
0
        public void TestSerializeFetchRequest()
        {
            var fetch = new FetchRequest
            {
                MaxWaitTime = 11111,
                MinBytes    = 222222,
                TopicsData  =
                    new[]
                {
                    new TopicData <FetchPartitionData>
                    {
                        TopicName      = "topic1",
                        PartitionsData =
                            new[] { new FetchPartitionData {
                                        FetchOffset = 1, MaxBytes = 333, Partition = 42
                                    } }
                    },
                    new TopicData <FetchPartitionData>
                    {
                        TopicName      = "topic2",
                        PartitionsData =
                            new[]
                        {
                            new FetchPartitionData {
                                FetchOffset = 1, MaxBytes = 333, Partition = 43
                            },
                            new FetchPartitionData {
                                FetchOffset = 2, MaxBytes = 89, Partition = 44
                            }
                        }
                    }
                }
            };

            using (var serialized = fetch.Serialize(new ReusableMemoryStream(null), 1234, ClientId, null))
            {
                CheckHeader(Basics.ApiKey.FetchRequest, 0, 1234, TheClientId, serialized);
                Assert.AreEqual(-1, BigEndianConverter.ReadInt32(serialized)); // ReplicaId
                Assert.AreEqual(fetch.MaxWaitTime, BigEndianConverter.ReadInt32(serialized));
                Assert.AreEqual(fetch.MinBytes, BigEndianConverter.ReadInt32(serialized));
                Assert.AreEqual(2, BigEndianConverter.ReadInt32(serialized)); // 2 elements
                var td = new TopicData <FetchPartitionData>();
                td.Deserialize(serialized, null);
                Assert.AreEqual("topic1", td.TopicName);
                Assert.AreEqual(1, td.PartitionsData.Count());
                var f = td.PartitionsData.First();
                Assert.AreEqual(1, f.FetchOffset);
                Assert.AreEqual(333, f.MaxBytes);
                Assert.AreEqual(42, f.Partition);

                td.Deserialize(serialized, null);
                Assert.AreEqual("topic2", td.TopicName);
                Assert.AreEqual(2, td.PartitionsData.Count());
                f = td.PartitionsData.First();
                Assert.AreEqual(1, f.FetchOffset);
                Assert.AreEqual(333, f.MaxBytes);
                Assert.AreEqual(43, f.Partition);

                f = td.PartitionsData.ElementAt(1);
                Assert.AreEqual(2, f.FetchOffset);
                Assert.AreEqual(89, f.MaxBytes);
                Assert.AreEqual(44, f.Partition);
            }
        }
Пример #21
0
        private void FixOffsetRangeError(FetchRequest fetchRequest, FetchResponse response)
        {
            var fetch = fetchRequest.Fetches[0];

            Func<OffsetTime, long> getOffset =
                offsetTime =>
                this.GetTopicOffsetAsync(response.Topic, 1, (int)offsetTime).Result.First(r => r.PartitionId == fetch.PartitionId).Offsets[0];

            var latest = getOffset(OffsetTime.Latest);
            var earliest = getOffset(OffsetTime.Earliest);

            var asked = fetch.Offset;

            if (asked < earliest) Offset = earliest;
            else if (asked > latest) Offset = latest;
        }
Пример #22
0
        private List<FetchResponse> FetchResponses(FetchRequest fetchRequest)
        {
            var route = this._options.Router.SelectBrokerRoute(Topic, PartitionId);

            return route.Connection.SendAsync(fetchRequest).Result;
        }
Пример #23
0
        private FetchRequest CreateFetchRequest()
        {
            var fetches = new List<Fetch>
                              {
                                  new Fetch
                                      {
                                          Topic = this.Topic,
                                          PartitionId = PartitionId,
                                          Offset = this.Offset,
                                          MaxBytes = FetchSize
                                      }
                              };

            var fetchRequest = new FetchRequest { Fetches = fetches };

            return fetchRequest;
        }
Пример #24
0
 public ValueTask <FetchResponse> Fetch(FetchRequest request)
 => SendRequest(ApiKey.Fetch, 11, request, new FetchRequestWriter(), new FetchResponseReader());
Пример #25
0
        public void ProducerSendsAndConsumerReceivesLotsOfMessagesManyFetchesAndOffsetsShouldBeCorrect()
        {
            var prodConfig     = this.SyncProducerConfig1;
            var consumerConfig = this.ConsumerConfig1;

            var  sourceMessage = new Message(Encoding.UTF8.GetBytes("test message"));
            long currentOffset = TestHelper.GetCurrentKafkaOffset(CurrentTestTopic, consumerConfig);

            int nrOfMessages = 1000;

            using (var producer = new SyncProducer(prodConfig))
            {
                for (int i = 0; i < nrOfMessages; i++)
                {
                    var producerRequest = new ProducerRequest(CurrentTestTopic, 0, new List <Message> {
                        sourceMessage
                    });
                    producer.Send(producerRequest);
                }
            }

            IConsumer consumer            = new Consumer(consumerConfig);
            int       messagesCounter     = 0;
            long      totalSizeDownloaded = 0;

            while (messagesCounter < nrOfMessages)
            {
                var request = new FetchRequest(CurrentTestTopic, 0, currentOffset);
                BufferedMessageSet response;
                int totalWaitTimeInMiliseconds = 0;
                int waitSingle = 100;
                while (true)
                {
                    Thread.Sleep(waitSingle);
                    response = consumer.Fetch(request);
                    if (response != null && response.Messages.Count() > 0)
                    {
                        break;
                    }

                    totalWaitTimeInMiliseconds += waitSingle;
                    if (totalWaitTimeInMiliseconds >= MaxTestWaitTimeInMiliseconds)
                    {
                        break;
                    }
                }

                Assert.NotNull(response);
                long currentCheckOffset = currentOffset + 4 + sourceMessage.Size;
                while (response.MoveNext())
                {
                    Assert.AreEqual(currentCheckOffset, response.Current.Offset);
                    currentCheckOffset += 4 + response.Current.Message.Size;
                    messagesCounter++;
                    currentOffset        = response.Current.Offset;
                    totalSizeDownloaded += response.Current.Message.Size + 4;
                }
            }

            Assert.AreEqual(nrOfMessages, messagesCounter);
            Assert.AreEqual(nrOfMessages * (4 + sourceMessage.Size), totalSizeDownloaded);
        }
Пример #26
0
        public virtual ActionResult Authenticate(string openid_provider)
        {
            var openid   = new OpenIdRelyingParty();
            var response = openid.GetResponse();

            Logger.InfoFormat("Trying to log-on user using OpenId Identifier: {0} .", openid_provider);

            if (response == null)
            {
                var request = openid.CreateRequest(openid_provider);

                var claim = new ClaimsRequest
                {
                    Email    = DemandLevel.Require,
                    Nickname = DemandLevel.Require,
                    FullName = DemandLevel.Request,
                };

                var fetch = new FetchRequest();
                fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
                fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);

                request.AddExtension(claim);
                request.AddExtension(fetch);

                return(request.RedirectingResponse.AsActionResult());
            }

            if (response.Status == AuthenticationStatus.Authenticated)
            {
                var claim = response.GetExtension <ClaimsResponse>();
                var fetch = response.GetExtension <FetchResponse>();
                var nick  = response.FriendlyIdentifierForDisplay;
                var email = string.Empty;

                if (claim != null)
                {
                    nick  = string.IsNullOrEmpty(claim.Nickname) ? claim.FullName : claim.Nickname;
                    email = claim.Email;
                }

                if (string.IsNullOrEmpty(nick) && fetch != null &&
                    fetch.Attributes.Contains(WellKnownAttributes.Name.First) &&
                    fetch.Attributes.Contains(WellKnownAttributes.Name.Last))
                {
                    nick = fetch.GetAttributeValue(WellKnownAttributes.Name.First) + " " +
                           fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
                }

                var user = string.Format("{0} <{1}>", nick, email);

                FormsService.SignIn(user);

                Logger.InfoFormat("User {0} authenticated successfully.", user);

                return(RedirectToAction("Index", "Home"));
            }

            Logger.Warn("User attempted login but password validation failed.");
            ModelState.AddModelError("", "Authentication failed.");

            return(View("LogOn"));
        }
Пример #27
0
        /// <summary>
        /// This request is used to fetch a batch of rows from a Statement previously created.
        /// </summary>
        public async Task<FetchResponse> FetchRequestAsync(string connectionId, uint statementId, ulong offset, int frameMaxSize, RequestOptions options)
        {
            FetchRequest req = new FetchRequest
            {
                ConnectionId = connectionId,
                StatementId = statementId,
                Offset = offset,
                FrameMaxSize = frameMaxSize
            };

            WireMessage msg = new WireMessage
            {
                Name = Constants.WireMessagePrefix + "FetchRequest",
                WrappedMessage = req.ToByteString()
            };

            using (Response webResponse = await PostRequestAsync(msg.ToByteArray(), options))
            {
                if (webResponse.WebResponse.StatusCode != HttpStatusCode.OK)
                {
                    WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    ErrorResponse res = ErrorResponse.Parser.ParseFrom(output.WrappedMessage);
                    throw new WebException(
                        string.Format(
                            "FetchRequestAsync failed! connectionId: {0}, Response code was: {1}, Response body was: {2}",
                            connectionId,
                            webResponse.WebResponse.StatusCode,
                            res.ToString()));
                }
                else
                {
                    WireMessage output = WireMessage.Parser.ParseFrom(webResponse.WebResponse.GetResponseStream());
                    FetchResponse res = FetchResponse.Parser.ParseFrom(output.WrappedMessage);
                    return res;
                }
            }
        }
Пример #28
0
        /// <summary>
        /// Action Results for Index, uses DotNetOpenAuth for creating OpenId Request with Intuit
        /// and handling response recieved.
        /// </summary>
        /// <param name="sender">Sender of th event.</param>
        /// <param name="e">Event Args.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            var openid_identifier = ConfigurationManager.AppSettings["openid_identifier"];
            var returnUrl         = "OpenIdHandler.aspx";
            var response          = openid.GetResponse();

            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(openid_identifier, out id))
                {
                    try
                    {
                        IAuthenticationRequest request = openid.CreateRequest(openid_identifier);
                        FetchRequest           fetch   = new FetchRequest();
                        fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email));
                        fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.FullName));
                        request.AddExtension(fetch);
                        request.RedirectToProvider();
                    }
                    catch (ProtocolException ex)
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                if (response.FriendlyIdentifierForDisplay == null)
                {
                    Response.Redirect("/OpenIdHandler.aspx");
                }

                // Stage 3: OpenID Provider sending assertion response
                Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                FetchResponse fetch = response.GetExtension <FetchResponse>();
                if (fetch != null)
                {
                    Session["OpenIdResponse"] = "True";
                    Session["FriendlyEmail"]  = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); // emailAddresses.Count > 0 ? emailAddresses[0] : null;
                    Session["FriendlyName"]   = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName); //fullNames.Count > 0 ? fullNames[0] : null;

                    //get the Oauth Access token for the user from OauthAccessTokenStorage.xml
                    OauthAccessTokenStorageHelper.GetOauthAccessTokenForUser(Session["FriendlyEmail"].ToString(), Page);
                }

                string query = Request.Url.Query;
                if (!string.IsNullOrWhiteSpace(query) && query.ToLower().Contains("disconnect=true"))
                {
                    Session["accessToken"]       = "dummyAccessToken";
                    Session["accessTokenSecret"] = "dummyAccessTokenSecret";
                    Session["Flag"] = true;
                    Response.Redirect("CleanupOnDisconnect.aspx");
                }

                if (!string.IsNullOrEmpty(returnUrl))
                {
                    Response.Redirect("default.aspx");
                }
            }
        }
Пример #29
0
        private Task ConsumeTopicPartitionAsync(string topic, int partitionId)
        {
            return Task.Factory.StartNew(() =>
            {
                try
                {
                    _options.Log.DebugFormat("Consumer: Creating polling task for topic: {0} on parition: {1}", topic, partitionId);
                    while (_disposeToken.IsCancellationRequested == false)
                    {
                        try
                        {
                            //get the current offset, or default to zero if not there.
                            long offset = 0;
                            _partitionOffsetIndex.AddOrUpdate(partitionId, i => offset, (i, currentOffset) => { offset = currentOffset; return currentOffset; });

                            //build a fetch request for partition at offset
                            var fetches = new List<Fetch>
                                    {
                                        new Fetch
                                            {
                                                Topic = topic,
                                                PartitionId = partitionId,
                                                Offset = offset
                                            }
                                    };

                            var fetchRequest = new FetchRequest
                                {
                                    Fetches = fetches
                                };

                            //make request and post to queue
                            var route = _options.Router.SelectBrokerRoute(topic, partitionId);
                            var responses = route.Connection.SendAsync(fetchRequest).Result;

                            if (responses.Count > 0)
                            {
                                var response = responses.FirstOrDefault(); //we only asked for one response
                                if (response != null && response.Messages.Count > 0)
                                {
                                    foreach (var message in response.Messages)
                                    {
                                        _fetchResponseQueue.Add(message, _disposeToken.Token);

                                        if (_disposeToken.IsCancellationRequested) return;
                                    }

                                    var nextOffset = response.Messages.Max(x => x.Offset) + 1;
                                    _partitionOffsetIndex.AddOrUpdate(partitionId, i => nextOffset, (i, l) => nextOffset);

                                    // sleep is not needed if responses were received
                                    continue;
                                }
                            }

                            //no message received from server wait a while before we try another long poll
                            Thread.Sleep(_options.BackoffInterval);
                        }
                        catch (Exception ex)
                        {
                            _options.Log.ErrorFormat("Exception occured while polling topic:{0} partition:{1}.  Polling will continue.  Exception={2}", topic, partitionId, ex);
                        }
                    }
                }
                finally
                {
                    _options.Log.DebugFormat("Consumer: Disabling polling task for topic: {0} on parition: {1}", topic, partitionId);
                    Task tempTask;
                    _partitionPollingIndex.TryRemove(partitionId, out tempTask);
                }
            });
        }
        static void FetchJob()
        {
            try
            {
                var client = new GoogleCloudPrintClient(provider, token);

                var listRequest = new ListRequest {
                    Proxy = proxy
                };
                var listResponse = client.ListPrinterAsync(listRequest).Result;
                if (listResponse.Printers != null && listResponse.Printers.Any())
                {
                    for (int i = 0; i < listResponse.Printers.Count(); i++)
                    {
                        Console.WriteLine($"{i + 1}. {listResponse.Printers.ElementAt(i).DisplayName}");
                    }

                    // Choose printer
                    Console.Write("Please choose a printer to fetch job: ");
                    int printerOption = -1;
                    if (int.TryParse(Console.ReadLine(), out printerOption))
                    {
                        var printer = listResponse.Printers.ElementAt(printerOption - 1);

                        var fetchRequest = new FetchRequest {
                            PrinterId = printer.Id
                        };
                        var fetchResponse = client.FetchJobAsync(fetchRequest).Result;
                        if (fetchResponse.Success && fetchResponse.Jobs.Any())
                        {
                            for (int i = 0; i < fetchResponse.Jobs.Count(); i++)
                            {
                                Console.WriteLine($"{i + 1}. {fetchResponse.Jobs.ElementAt(i).Title}");
                            }

                            // Choose job on specific printer
                            Console.Write("Please choose a print job to download: ");
                            int printJobOption = -1;
                            if (int.TryParse(Console.ReadLine(), out printJobOption))
                            {
                                if (printJobOption == -1)
                                {
                                    return;
                                }
                                var printJob = fetchResponse.Jobs.ElementAt(printJobOption - 1);
                                if (DownloadTicket(client, printJob) && DownloadDocument(client, printJob))
                                {
                                    var updateRequest = new ControlRequest
                                    {
                                        JobId  = printJob.Id,
                                        Status = Models.Job.LegacyJobStatus.DONE
                                    };
                                    var updateResponse = client.UpdateJobStatusAsync(updateRequest).Result;
                                    Console.WriteLine($"Update job status: {updateResponse.Success}");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Fail to choose print job");
                            }
                        }
                        else
                        {
                            Console.WriteLine("There is currently no job!");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("There is no printer to delete!");
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #31
0
        public void IsValidNulltopic()
        {
            FetchRequest request = new FetchRequest(null, 1, 10L, 100);

            Assert.IsFalse(request.IsValid());
        }
Пример #32
0
        private List <FetchResponse> FetchResponses(FetchRequest fetchRequest)
        {
            var route = this._options.Router.SelectBrokerRoute(Topic, PartitionId);

            return(route.Connection.SendAsync(fetchRequest).Result);
        }
Пример #33
0
        private void TestRequests(string variableName, string[] dataSources, Tuple <double, double> saneRange, bool isNaNoverLand, bool isNaNoverOcean)
        {
            RemoteFetchClient client = new RemoteFetchClient(new Uri(serviceUri));

            for (int i = 0; i < 8; ++i)
            {
                for (int k = 0; k < 4; ++k)
                {
                    bool[] times = new bool[3];
                    times[0] = !(i > 3);
                    times[1] = !((i % 4) > 1);
                    times[2] = !((i % 2) > 0);
                    int          timeDims      = times.Count(x => !x);
                    int          spatDims      = k / 2 + 1;
                    int          timeDimsShift = spatDims - 1;
                    FetchRequest request;
                    if (k == 0)
                    {
                        request = new FetchRequest(variableName, CreatePointsDomain(CreateTestTimeRegion(times[0], times[1], times[2])), dataSources);
                    }
                    else if (k == 1)
                    {
                        request = new FetchRequest(variableName, CreateCellsDomain(CreateTestTimeRegion(times[0], times[1], times[2])), dataSources);
                    }
                    else if (k == 2)
                    {
                        request = new FetchRequest(variableName, CreatePointGridDomain(CreateTestTimeRegion(times[0], times[1], times[2])), dataSources);
                    }
                    else
                    {
                        request = new FetchRequest(variableName, CreateCellGridDomain(CreateTestTimeRegion(times[0], times[1], times[2])), dataSources);
                    }

                    Trace.WriteLine(String.Format("Testing. Spatial domain type: {0}. Time domain type: years: {1}, days: {2}, hours: {3}.",
                                                  k == 0 ? "2 points" : k == 1 ? "2 cells" : k == 2 ? "point grid" : "cell grid",
                                                  times[0] ? "1 interval" : "2 points",
                                                  times[1] ? "1 interval" : "2 points",
                                                  times[2] ? "1 interval" : "2 points"));

                    using (var result = client.FetchAsync(request, s => Trace.WriteLine(s)).Result)
                    {
                        var v = result.Variables["values"];
                        Assert.IsTrue(v.Dimensions.Count == spatDims + timeDims);
                        switch (k)
                        {
                        case 0:
                        case 1:
                            Assert.AreEqual(2, v.Dimensions[0].Length);
                            break;

                        case 2:
                            Assert.AreEqual(request.Domain.Lons.Length, v.Dimensions[0].Length);
                            Assert.AreEqual(request.Domain.Lats.Length, v.Dimensions[1].Length);
                            break;

                        case 3:
                            Assert.AreEqual(request.Domain.Lons.Length - 1, v.Dimensions[0].Length);
                            Assert.AreEqual(request.Domain.Lats.Length - 1, v.Dimensions[1].Length);
                            break;
                        }
                        for (int j = 1; j <= timeDims; ++j)
                        {
                            Assert.AreEqual(2, v.Dimensions[j + timeDimsShift].Length);
                        }

                        if (dataSources != null && dataSources.Length > 1)
                        {
                            var p = result.Variables["provenance"];
                            Assert.IsTrue(p.Dimensions.Count == spatDims + timeDims);
                            for (int j = 1; j <= timeDims; ++j)
                            {
                                Assert.AreEqual(2, p.Dimensions[j + timeDimsShift].Length);
                            }
                            switch (k)
                            {
                            case 0:
                            case 1:
                                Assert.AreEqual(2, p.Dimensions[0].Length);
                                break;

                            case 2:
                                Assert.AreEqual(request.Domain.Lons.Length, p.Dimensions[0].Length);
                                Assert.AreEqual(request.Domain.Lats.Length, p.Dimensions[1].Length);
                                break;

                            case 3:
                                Assert.AreEqual(request.Domain.Lons.Length - 1, p.Dimensions[0].Length);
                                Assert.AreEqual(request.Domain.Lats.Length - 1, p.Dimensions[1].Length);
                                break;
                            }
                        }

                        var sd = result.Variables["sd"];
                        Assert.IsTrue(sd.Dimensions.Count == spatDims + timeDims);
                        for (int j = 1; j <= timeDims; ++j)
                        {
                            Assert.AreEqual(2, sd.Dimensions[j + timeDimsShift].Length);
                        }
                        switch (k)
                        {
                        case 0:
                        case 1:
                            Assert.AreEqual(2, sd.Dimensions[0].Length);
                            break;

                        case 2:
                            Assert.AreEqual(request.Domain.Lons.Length, sd.Dimensions[0].Length);
                            Assert.AreEqual(request.Domain.Lats.Length, sd.Dimensions[1].Length);
                            break;

                        case 3:
                            Assert.AreEqual(request.Domain.Lons.Length - 1, sd.Dimensions[0].Length);
                            Assert.AreEqual(request.Domain.Lats.Length - 1, sd.Dimensions[1].Length);
                            break;
                        }

                        if (spatDims == 1)
                        {
                            if (timeDims == 0)
                            {
                                double[] vals = (double[])v.GetData();
                                TestValue(vals[0], isNaNoverOcean ? null : saneRange);
                                TestValue(vals[1], isNaNoverLand ? null : saneRange);
                            }
                            else if (timeDims == 1)
                            {
                                double[,] vals = (double[, ])v.GetData();
                                TestValue(vals[0, 0], isNaNoverOcean ? null : saneRange);
                                TestValue(vals[0, 1], isNaNoverOcean ? null : saneRange);
                                TestValue(vals[1, 0], isNaNoverLand ? null : saneRange);
                                TestValue(vals[1, 1], isNaNoverLand ? null : saneRange);
                            }
                            else if (timeDims == 2)
                            {
                                double[, ,] vals = (double[, , ])v.GetData();
                                for (int j1 = 0; j1 < 2; ++j1)
                                {
                                    for (int j2 = 0; j2 < 2; ++j2)
                                    {
                                        TestValue(vals[0, j1, j2], isNaNoverOcean ? null : saneRange);
                                        TestValue(vals[1, j1, j2], isNaNoverLand ? null : saneRange);
                                    }
                                }
                            }
                            else if (timeDims == 3)
                            {
                                double[, , ,] vals = (double[, , , ])v.GetData();
                                for (int j1 = 0; j1 < 2; ++j1)
                                {
                                    for (int j2 = 0; j2 < 2; ++j2)
                                    {
                                        for (int j3 = 0; j3 < 2; ++j3)
                                        {
                                            TestValue(vals[0, j1, j2, j3], isNaNoverOcean ? null : saneRange);
                                            TestValue(vals[1, j1, j2, j3], isNaNoverLand ? null : saneRange);
                                        }
                                    }
                                }
                            }
                        }
                        else //grids
                        {
                            int lonOceanFirst = 0;
                            int lonOceanLast  = 2;
                            int latOceanFirst = 0;
                            int latOceanLast  = 5;
                            if (timeDims == 0)
                            {
                                double[,] vals = (double[, ])v.GetData();
                                for (int s1 = 0; s1 < v.Dimensions[0].Length; ++s1)
                                {
                                    for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                    {
                                        TestValue(vals[s1, s2], saneRange);
                                    }
                                }
                                if (isNaNoverLand)
                                {
                                    for (int s1 = 27; s1 < v.Dimensions[0].Length; ++s1)
                                    {
                                        for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                        {
                                            TestValue(vals[s1, s2], null);
                                        }
                                    }
                                }
                                if (isNaNoverOcean)
                                {
                                    for (int s1 = lonOceanFirst; s1 <= lonOceanLast; ++s1)
                                    {
                                        for (int s2 = latOceanFirst; s2 <= latOceanLast; ++s2)
                                        {
                                            TestValue(vals[s1, s2], null);
                                        }
                                    }
                                }
                            }
                            else if (timeDims == 1)
                            {
                                double[, ,] vals = (double[, , ])v.GetData();
                                for (int s1 = 0; s1 < v.Dimensions[0].Length; ++s1)
                                {
                                    for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                    {
                                        for (int j1 = 0; j1 < 2; ++j1)
                                        {
                                            TestValue(vals[s1, s2, j1], saneRange);
                                        }
                                    }
                                }
                                if (isNaNoverLand)
                                {
                                    for (int s1 = 27; s1 < v.Dimensions[0].Length; ++s1)
                                    {
                                        for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                TestValue(vals[s1, s2, j1], null);
                                            }
                                        }
                                    }
                                }
                                if (isNaNoverOcean)
                                {
                                    for (int s1 = lonOceanFirst; s1 <= lonOceanLast; ++s1)
                                    {
                                        for (int s2 = latOceanFirst; s2 <= latOceanLast; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                TestValue(vals[s1, s2, j1], null);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (timeDims == 2)
                            {
                                double[, , ,] vals = (double[, , , ])v.GetData();
                                for (int s1 = 0; s1 < v.Dimensions[0].Length; ++s1)
                                {
                                    for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                    {
                                        for (int j1 = 0; j1 < 2; ++j1)
                                        {
                                            for (int j2 = 0; j2 < 2; ++j2)
                                            {
                                                TestValue(vals[s1, s2, j1, j2], saneRange);
                                            }
                                        }
                                    }
                                }
                                if (isNaNoverLand)
                                {
                                    for (int s1 = 27; s1 < v.Dimensions[0].Length; ++s1)
                                    {
                                        for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                for (int j2 = 0; j2 < 2; ++j2)
                                                {
                                                    TestValue(vals[s1, s2, j1, j2], null);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isNaNoverOcean)
                                {
                                    for (int s1 = lonOceanFirst; s1 <= lonOceanLast; ++s1)
                                    {
                                        for (int s2 = latOceanFirst; s2 <= latOceanLast; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                for (int j2 = 0; j2 < 2; ++j2)
                                                {
                                                    TestValue(vals[s1, s2, j1, j2], null);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            else if (timeDims == 3)
                            {
                                double[, , , ,] vals = (double[, , , , ])v.GetData();
                                for (int s1 = 0; s1 < v.Dimensions[0].Length; ++s1)
                                {
                                    for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                    {
                                        for (int j1 = 0; j1 < 2; ++j1)
                                        {
                                            for (int j2 = 0; j2 < 2; ++j2)
                                            {
                                                for (int j3 = 0; j3 < 2; ++j3)
                                                {
                                                    TestValue(vals[s1, s2, j1, j2, j3], saneRange);
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isNaNoverLand)
                                {
                                    for (int s1 = 27; s1 < v.Dimensions[0].Length; ++s1)
                                    {
                                        for (int s2 = 0; s2 < v.Dimensions[1].Length; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                for (int j2 = 0; j2 < 2; ++j2)
                                                {
                                                    for (int j3 = 0; j3 < 2; ++j3)
                                                    {
                                                        TestValue(vals[s1, s2, j1, j2, j3], null);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (isNaNoverOcean)
                                {
                                    for (int s1 = lonOceanFirst; s1 <= lonOceanLast; ++s1)
                                    {
                                        for (int s2 = latOceanFirst; s2 <= latOceanLast; ++s2)
                                        {
                                            for (int j1 = 0; j1 < 2; ++j1)
                                            {
                                                for (int j2 = 0; j2 < 2; ++j2)
                                                {
                                                    for (int j3 = 0; j3 < 2; ++j3)
                                                    {
                                                        TestValue(vals[s1, s2, j1, j2, j3], null);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Trace.WriteLine("Passed");
                }
            }
        }
Пример #34
0
        public void GetBytesValidStructure()
        {
            string topicName     = "topic";
            int    correlationId = 1;
            string clientId      = "TestClient";
            int    maxWait       = 234;
            int    minBytes      = 345;

            var requestMap = new Dictionary <string, List <PartitionFetchInfo> >();

            requestMap[topicName] = new List <PartitionFetchInfo>()
            {
                new PartitionFetchInfo(2, 4000, 777)
            };


            var request = new FetchRequest(correlationId, clientId, maxWait, minBytes, requestMap);


            int requestSize = 4 +                                                                        //request size
                              2 +                                                                        //request type id
                              2 +                                                                        //versionId
                              4 +                                                                        //correlation id
                              BitWorks.GetShortStringLength(clientId, AbstractRequest.DefaultEncoding) + // client id length
                              4 +                                                                        //replica id
                              4 +                                                                        //max wait
                              4 +                                                                        //min bytes
                              4 +                                                                        //offset info count
                                                                                                         //=== offset info part
                              request.OffsetInfo.Keys.Sum(x => BitWorks.GetShortStringLength(x, AbstractRequest.DefaultEncoding)) +
                              request.OffsetInfo.Values.Select(pl => 4 + pl.Sum(p => p.SizeInBytes)).Sum();

            var ms = new MemoryStream();

            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);

            // add 4 bytes for the length of the message at the beginning
            Assert.AreEqual(requestSize, bytes.Length);

            // first 4 bytes = the message length
            Assert.AreEqual(requestSize - 4, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the request type
            Assert.AreEqual((short)RequestTypes.Fetch,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the version id
            Assert.AreEqual((short)FetchRequest.CurrentVersion,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray <byte>()), 0));

            // next 2 bytes = the correlation id
            Assert.AreEqual(correlationId,
                            BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(8).Take(4).ToArray <byte>()), 0));

            // next 2 bytes = the client id length
            Assert.AreEqual((short)clientId.Length,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(12).Take(2).ToArray <byte>()), 0));

            // next few bytes = the client id
            Assert.AreEqual(clientId, Encoding.ASCII.GetString(bytes.Skip(14).Take(clientId.Length).ToArray <byte>()));

            // next 4 bytes = replica id
            Assert.AreEqual(-1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(14 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = max wait
            Assert.AreEqual(maxWait,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(18 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = min bytes
            Assert.AreEqual(minBytes,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(22 + clientId.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = offset info count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(26 + clientId.Length).Take(4).ToArray <byte>()), 0));

            //=== offset info part

            // next 2 bytes = the topic length
            Assert.AreEqual((short)topicName.Length,
                            BitConverter.ToInt16(
                                BitWorks.ReverseBytes(bytes.Skip(30 + clientId.Length).Take(2).ToArray <byte>()), 0));

            // next few bytes = the topic
            Assert.AreEqual(topicName,
                            Encoding.ASCII.GetString(
                                bytes.Skip(32 + clientId.Length).Take(topicName.Length).ToArray <byte>()));

            // next 4 bytes = partitions count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(32 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = partition
            Assert.AreEqual(2,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(36 + clientId.Length + topicName.Length).Take(4).ToArray <byte>()), 0));

            // next 4 bytes = offset
            Assert.AreEqual(4000,
                            BitConverter.ToInt64(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(40 + clientId.Length + topicName.Length).Take(8).ToArray <byte>()), 0));

            // next 4 bytes = fetch size
            Assert.AreEqual(777,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(48 + clientId.Length + topicName.Length).Take(8).ToArray <byte>()), 0));
        }
Пример #35
0
        public async Task CellGridDataLinearInterpolationTest()
        {
            var storage = TestDataStorageFactory.GetStorageContext(TestConstants.UriReanalysisRegular);
            NCEPReanalysisRegularGridDataHandler reanalysis = await NCEPReanalysisRegularGridDataHandler.CreateAsync(storage);


            ITimeRegion tr = new TimeRegion(firstYear: 1980, lastYear: 1980, firstDay: 2, lastDay: 3, startHour: 0, stopHour: 0).
                             GetSeasonlyTimeseries(firstDay: 2, lastDay: 3, isIntervalTimeseries: false);//indeces 46756,46760; hours sice 1-1-1 00:00:00   =  17347560 , 17347524
            IFetchDomain domain = FetchDomain.CreateCellGrid(
                //ocean region
                new double[] { 5.0, 7.5, 10.0 },       //indeces 34,33,32
                new double[] { 40.0, 42.5, 45, 47.5 }, //indeces 16,17,18,19
                tr);
            FetchRequest request = new FetchRequest("air", domain);

            bool[,, ] mask = new bool[3, 2, 2];
            bool[] smask = System.Linq.Enumerable.Repeat(true, 12).ToArray();
            Buffer.BlockCopy(smask, 0, mask, 0, 12 * sizeof(bool));

            var handlerPrivate    = new PrivateObject(reanalysis, new PrivateType(typeof(DataHandlerFacade)));
            var aggregatorPrivate = new PrivateObject(handlerPrivate, "valuesAggregator");

            for (int i = 0; i < 2; i++)
            {
                bool[, ,] effectiveMask = i == 0 ? null : mask;

                var result = await(Task <Array>)(aggregatorPrivate.Invoke("AggregateAsync", RequestContextStub.GetStub(storage, request), effectiveMask));

                //lon,lat,t
                double[, , ] t = (double[, , ])result;

                Assert.AreEqual(12, t.Length);
                Assert.AreEqual(3, t.GetLength(0));
                Assert.AreEqual(2, t.GetLength(1));

                //first time layer manual calculation
                //SDS fetched data
                //C:\Users\Dmitry>sds data "msds:az?name=ReanalysisRegular&DefaultEndpointsProl=http&AccountName=fetch&AccountKey=1Y0EOrnCX6ULY8c3iMHg9rrul2BWbPHKsHUceZ7ShM/q9K0ml49gQm+PE7G7i7zCvrpuT/vT1aHzEArutw==" air[32:34,16:19,46756]

                //[32,16,46756]    -22521
                //[32,17,46756]    -22091
                //[32,18,46756]    -22221
                //[32,19,46756]    -22291
                //[33,16,46756]    -22391
                //[33,17,46756]    -21861
                //[33,18,46756]    -21921
                //[33,19,46756]    -22021
                //[34,16,46756]    -21811
                //[34,17,46756]    -21421
                //[34,18,46756]    -21461
                //[34,19,46756]    -21541

                //excel calculated values               x*0.01+239.66                       mean
                //-22521	-22091	-22221	-22291		14.45	18.75	17.45	16.75		17.5	19.425	18.525
                //-22391	-21861	-21921	-22021		15.75	21.05	20.45	19.45		20.95	23	    22.3
                //-21811	-21421	-21461	-21541		21.55	25.45	25.05	24.25

                //checking first time layer
                //ATTENTION: notice the lat indexing. it is flaped!
                //lon,lat,t
                A(17.5, t[0, 1, 0]); A(19.425, t[1, 1, 0]); A(18.525, t[2, 1, 0]);
                A(20.95, t[0, 0, 0]); A(23, t[1, 0, 0]); A(22.3, t[2, 0, 0]);


                //second time layer asserting
                //SDS data fetching
                //lon,lat,t
                //C:\Users\Dmitry>sds data "msds:az?name=ReanalysisRegular&DefaultEndpointsProl=http&AccountName=fetch&AccountKey=1Y0EOrnCX6ULY8c3iMHg9rrul2BWbPHKsHUceZ7ShM/q9K0ml49gQm+PE7G7i7zCvrpuT/vT1aHzEArutw==" air[32:34,16:19,46760]

                //[32,16,46760]    -22761
                //[32,17,46760]    -22071
                //[32,18,46760]    -22201
                //[32,19,46760]    -22391
                //[33,16,46760]    -22571
                //[33,17,46760]    -21911
                //[33,18,46760]    -21961
                //[33,19,46760]    -22111
                //[34,16,46760]    -21891
                //[34,17,46760]    -21601
                //[34,18,46760]    -21611
                //[34,19,46760]    -21631

                //excel calculated values               x*0.01+239.66                       mean
                //-22761	-22071	-22201	-22391		12.05	18.95	17.65	15.75		16.375	19.3	18
                //-22571	-21911	-21961	-22111		13.95	20.55	20.05	18.55		19.725	21.95	21.375
                //-21891	-21601	-21611	-21631		20.75	23.65	23.55	23.35

                //checking second time layer
                //ATTENTION: notice the lat indexing. it is flaped!
                //lon,lat,t
                A(16.375, t[0, 1, 1]); A(19.3, t[1, 1, 1]); A(18, t[2, 1, 1]);
                A(19.725, t[0, 0, 1]); A(21.95, t[1, 0, 1]); A(21.375, t[2, 0, 1]);
            }
        }
Пример #36
0
        private Task ConsumeTopicPartitionAsync(string topic, int partitionId)
        {
            return(Task.Factory.StartNew(async() =>
            {
                try
                {
                    var bufferSizeHighWatermark = FetchRequest.DefaultBufferSize;

                    _options.Log.DebugFormat("Consumer: Creating polling task for topic: {0} on parition: {1}", topic, partitionId);
                    while (_disposeToken.IsCancellationRequested == false)
                    {
                        try
                        {
                            //get the current offset, or default to zero if not there.
                            long offset = 0;
                            _partitionOffsetIndex.AddOrUpdate(partitionId, i => offset, (i, currentOffset) => { offset = currentOffset; return currentOffset; });

                            //build a fetch request for partition at offset
                            var fetch = new Fetch
                            {
                                Topic = topic,
                                PartitionId = partitionId,
                                Offset = offset,
                                MaxBytes = bufferSizeHighWatermark,
                            };

                            var fetches = new List <Fetch> {
                                fetch
                            };

                            var fetchRequest = new FetchRequest
                            {
                                MaxWaitTime = (int)_options.MaxWaitTimeForMinimumBytes.TotalMilliseconds,
                                MinBytes = _options.MinimumBytes,
                                Fetches = fetches
                            };

                            //make request and post to queue
                            var route = _options.Router.SelectBrokerRoute(topic, partitionId);

                            var responses = await route.Connection.SendAsync(fetchRequest).ConfigureAwait(false);

                            if (responses.Count > 0)
                            {
                                var response = responses.FirstOrDefault(); //we only asked for one response

                                HandleResponseErrors(fetch, response);

                                if (response != null && response.Messages.Count > 0)
                                {
                                    foreach (var message in response.Messages)
                                    {
                                        _fetchResponseQueue.Add(message, _disposeToken.Token);

                                        if (_disposeToken.IsCancellationRequested)
                                        {
                                            return;
                                        }
                                    }

                                    var nextOffset = response.Messages.Max(x => x.Meta.Offset) + 1;
                                    _partitionOffsetIndex.AddOrUpdate(partitionId, i => nextOffset, (i, l) => nextOffset);

                                    // sleep is not needed if responses were received
                                    continue;
                                }
                            }

                            //no message received from server wait a while before we try another long poll
                            Thread.Sleep(_options.BackoffInterval);
                        }
                        catch (BufferUnderRunException ex)
                        {
                            bufferSizeHighWatermark = (int)(ex.RequiredBufferSize * _options.FetchBufferMultiplier) + ex.MessageHeaderSize;
                            _options.Log.InfoFormat("Buffer underrun.  Increasing buffer size to: {0}", bufferSizeHighWatermark);
                        }
                        catch (OffsetOutOfRangeException ex)
                        {
                            //TODO this turned out really ugly.  Need to fix this section.
                            _options.Log.ErrorFormat(ex.Message);
                            FixOffsetOutOfRangeExceptionAsync(ex.FetchRequest);
                        }
                        catch (InvalidMetadataException ex)
                        {
                            //refresh our metadata and ensure we are polling the correct partitions
                            _options.Log.ErrorFormat(ex.Message);
                            _options.Router.RefreshTopicMetadata(topic);
                            EnsurePartitionPollingThreads();
                        }
                        catch (Exception ex)
                        {
                            _options.Log.ErrorFormat("Exception occured while polling topic:{0} partition:{1}.  Polling will continue.  Exception={2}", topic, partitionId, ex);
                        }
                    }
                }
                finally
                {
                    _options.Log.DebugFormat("Consumer: Disabling polling task for topic: {0} on parition: {1}", topic, partitionId);
                    Task tempTask;
                    _partitionPollingIndex.TryRemove(partitionId, out tempTask);
                }
            }));
        }
Пример #37
0
 public IRequestContext CopyWithNewRequest(FetchRequest request)
 {
     throw new NotImplementedException();
 }
Пример #38
0
        public void ProducerSends1Message()
        {
            var prodConfig = this.ConfigBasedSyncProdConfig;

            int totalWaitTimeInMiliseconds = 0;
            int waitSingle      = 100;
            var originalMessage = new Message(Encoding.UTF8.GetBytes("TestData"));

            var multipleBrokersHelper = new TestMultipleBrokersHelper(CurrentTestTopic);

            multipleBrokersHelper.GetCurrentOffsets(
                new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 });
            using (var producer = new Producer(prodConfig))
            {
                var producerData = new ProducerData <string, Message>(
                    CurrentTestTopic, new List <Message> {
                    originalMessage
                });
                producer.Send(producerData);
                Thread.Sleep(waitSingle);
            }

            while (
                !multipleBrokersHelper.CheckIfAnyBrokerHasChanged(
                    new[] { this.SyncProducerConfig1, this.SyncProducerConfig2, this.SyncProducerConfig3 }))
            {
                totalWaitTimeInMiliseconds += waitSingle;
                Thread.Sleep(waitSingle);
                if (totalWaitTimeInMiliseconds > this.maxTestWaitTimeInMiliseconds)
                {
                    Assert.Fail("None of the brokers changed their offset after sending a message");
                }
            }

            totalWaitTimeInMiliseconds = 0;

            var consumerConfig = new ConsumerConfiguration(
                multipleBrokersHelper.BrokerThatHasChanged.Host, multipleBrokersHelper.BrokerThatHasChanged.Port);
            IConsumer          consumer = new Consumer(consumerConfig);
            var                request1 = new FetchRequest(CurrentTestTopic, multipleBrokersHelper.PartitionThatHasChanged, multipleBrokersHelper.OffsetFromBeforeTheChange);
            BufferedMessageSet response;

            while (true)
            {
                Thread.Sleep(waitSingle);
                response = consumer.Fetch(request1);
                if (response != null && response.Messages.Count() > 0)
                {
                    break;
                }

                totalWaitTimeInMiliseconds += waitSingle;
                if (totalWaitTimeInMiliseconds >= this.maxTestWaitTimeInMiliseconds)
                {
                    break;
                }
            }

            Assert.NotNull(response);
            Assert.AreEqual(1, response.Messages.Count());
            Assert.AreEqual(originalMessage.ToString(), response.Messages.First().ToString());
        }
Пример #39
0
        /// <summary>
        /// handle openid logins
        /// </summary>
        /// <param name="id">returnURL</param>
        /// <returns></returns>
        public ActionResult openid(string id)
        {
            bool         isNewUser = false;
            LoginService lservice  = new LoginService(LoginService.ServiceType.OPENID);
            var          openid    = new OpenIdRelyingParty();
            var          response  = openid.GetResponse();

            if (response == null)
            {
                Identifier identifier;
                string     stringid = Request.QueryString["openid_url"] ?? Request.Form["openid_identifier"];
                if (!stringid.StartsWith("http"))
                {
                    throw new Exception("unknown OpenID format: " + stringid);
                }
                // Stage 2: user submitting Identifier

                if (Identifier.TryParse(stringid, out identifier))
                {
                    try
                    {
                        var request = openid.CreateRequest(stringid);

                        // only ask for extra info if this user is new
                        if (!lservice.DoesIDExist(stringid))
                        {
                            // do this for anything that does not have an explicit openid known by the user
                            //
                            if (Request.QueryString["openid_url"] != null)
                            {
                                var ext = new FetchRequest();
                                ext.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                                ext.Attributes.AddRequired(WellKnownAttributes.Name.First);
                                ext.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                                ext.Attributes.AddRequired(WellKnownAttributes.Preferences.Language);
                                ext.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
                                request.AddExtension(ext);

                                //OAuthExtensionRequest oauth = new OAuthExtensionRequest();
                                //oauth.ConsumerKey = "my6solutions.com";
                                //oauth.Scope = "http://www.google.com/m8/feeds/";
                                //request.AddExtension(oauth);
                            }
                            else
                            {
                                var claimsReq = new ClaimsRequest
                                {
                                    BirthDate  = DemandLevel.Request,
                                    Nickname   = DemandLevel.Request,
                                    PostalCode = DemandLevel.Request,
                                    Language   = DemandLevel.Request,
                                    TimeZone   = DemandLevel.Request,
                                    Country    = DemandLevel.Request,
                                    Email      = DemandLevel.Require,
                                    FullName   = DemandLevel.Request,
                                    Gender     = DemandLevel.Request
                                };
                                request.AddExtension(claimsReq);
                            }
                        }

                        return(request.RedirectingResponse.AsActionResult());
                    }
                    catch (ProtocolException ex)
                    {
                        return(View("Index", (object)ex.Message));
                    }
                }
                return(View("Index", (object)"Invalid identifier"));
            }

            // Stage 3: OpenID Provider sending assertion response
            switch (response.Status)
            {
            case AuthenticationStatus.Authenticated:
                var friendlyIdentifier = response.FriendlyIdentifierForDisplay;

                // find if this openid exists
                user   u     = lservice.GetUserByProviderID("http://" + friendlyIdentifier);
                string email = null;
                if (u == null)
                {
                    var claimsExt = response.GetExtension <ClaimsResponse>();
                    if (claimsExt == null)
                    {
                        var fetchExt = response.GetExtension <FetchResponse>();
                        if (fetchExt != null &&
                            fetchExt.Attributes.Contains(WellKnownAttributes.Contact.Email))
                        {
                            email = fetchExt.Attributes[WellKnownAttributes.Contact.Email].Values[0];
                        }
                    }
                    else
                    {
                        email = claimsExt.Email;
                    }

                    // maybe user is already registered on this site
                    // see if we can get a user account based on email address
                    u = repository.GetUserByEmail(email, subdomainid.Value);
                    if (u == null)
                    {
                        if (claimsExt != null)
                        {
                            var newUser = new user();

                            if (claimsExt.Gender.HasValue)
                            {
                                newUser.gender = claimsExt.Gender.Value.ToString().ToLower();
                            }
                            if (claimsExt.BirthDate.HasValue)
                            {
                                newUser.birthday = claimsExt.BirthDate.Value;
                            }
                            string[] nameparts = claimsExt.FullName.Split(' ');
                            newUser.role         = (int)UserRole.ALL;
                            newUser.lastName     = nameparts[0];
                            newUser.firstName    = string.Join(" ", nameparts.Skip(1).ToArray());
                            newUser.passwordHash = "";
                            newUser.email        = claimsExt.Email;
                            newUser.openID       = "http://" + friendlyIdentifier;
                            //newUser.passwordHash = Utility.ComputePasswordHash(friendlyIdentifier);
                            newUser.role = (int)UserRole.ALL;
                            repository.AddUser(newUser);
                            u         = newUser;
                            isNewUser = true;
                        }
                        else
                        {
                            // need to handle this
                        }
                    }
                    else
                    {
                        // update user with openid
                        repository.UpdateOpenID("http://" + friendlyIdentifier, u.id);
                    }
                }

                if (u == null)
                {
                    // unable to login user
                    return(View("Index", (object)"Unable to sign in. Please register an account."));
                }


#if DEBUG
                var hash = Utility.ComputePasswordHash(friendlyIdentifier);
                repository.UpdatePasswordHash(u.id, hash);
#else
                var hash = u.passwordHash;
#endif
                SetAuthCookie(hash);
                SetSessionAuthInfo(u.id, (UserRole)u.role);

                // handle new users and not completely setuped users
                if (isNewUser)
                {
                    return(RedirectToAction("setup", "register"));
                }

                // handle login required from somewhere else
                if (!string.IsNullOrEmpty(id))
                {
                    return(Redirect(id));
                }

                return(RedirectToAction("Index", "Home"));

            case AuthenticationStatus.Canceled:
                return(View("Index", (object)"Canceled at provider"));

            case AuthenticationStatus.Failed:
                return(View("Index", (object)response.Exception.Message));
            }
            return(new EmptyResult());
        }
Пример #40
0
        public ActionResult Authenticate(string returnUrl)
        {
            // handle oauth authentication
            if (string.IsNullOrEmpty(Request.Form["openid_identifier"]))
            {
                // handle oauth version 2.0
                if (Request.Form["oauth_version"] == "2.0")
                {
                    return(FacebookAuth(returnUrl));
                }
            }

            var response = openid.GetResponse();

            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                {
                    try
                    {
                        var request = openid.CreateRequest(Request.Form["openid_identifier"]);
                        var fetch   = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        request.AddExtension(fetch);

                        return(request.RedirectingResponse.AsActionResult());
                    }
                    catch (ProtocolException ex)
                    {
                        ViewData["Message"] = ex.Message;
                        return(View("Logon"));
                    }
                }
                else
                {
                    ViewData["Message"] = "Invalid identifier";
                    return(View("Logon"));
                }
            }
            else
            {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status)
                {
                case AuthenticationStatus.Authenticated:

                    UsersModels user = new UsersModels();

                    var    fetch = response.GetExtension <FetchResponse>();
                    string email = null;
                    if (fetch != null)
                    {
                        email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                    }

                    // for new OpenId
                    user = user.GetUserByOpenId(response.ClaimedIdentifier);
                    if (user == null)
                    {
                        RegisterOpenId roi = new RegisterOpenId();
                        roi.ClaimedOpenId  = response.ClaimedIdentifier;
                        roi.FriendlyOpenId = PostHelper.GetFriendlyOpenId(response, email);
                        roi.ReturnUrl      = returnUrl;
                        roi.Email          = email;
                        return(View(roi));
                    }

                    FormsAuthenticationService formAuth = new FormsAuthenticationService();
                    formAuth.SignIn(response.ClaimedIdentifier, false);

                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                case AuthenticationStatus.Canceled:
                    ViewData["Message"] = "Canceled at provider";
                    return(View("Logon"));

                case AuthenticationStatus.Failed:
                    ViewData["Message"] = response.Exception.Message;
                    return(View("Logon"));
                }
            }
            return(new EmptyResult());
        }
Пример #41
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Metodo viejo que anda

            //if (Session["GoogleAppId"] != null && Session["GoogleAppSecret"] != null) {
            //try {
            //   string code = Request.QueryString["code"];
            //   string url = "https://accounts.google.com/o/oauth2/token";
            //   string appId = Session["GoogleAppId"] as string;
            //   string appSecret = Session["GoogleAppSecret"] as string;
            //   string redirectUrl = ConvertRelativeUrlToAbsoluteUrl(HttpUrlBuilder.BuildUrl("~/DesktopModules/CoreModules/SignIn/LogInGoogle.aspx"));

            //   StringBuilder postData = new StringBuilder();

            //   postData.Append("code=" + code + "&");
            //   postData.Append("client_id=" + appId + "&");
            //   postData.Append("client_secret=" + appSecret + "&");
            //   postData.Append("redirect_uri=" + redirectUrl + "&");
            //   postData.Append("grant_type=authorization_code");


            //   //ETC for all Form Elements

            //   // Now to Send Data.
            //   StreamWriter writer = null;

            //   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //   request.Method = "POST";
            //   request.ContentType = "application/x-www-form-urlencoded";
            //   request.ContentLength = postData.ToString().Length;
            //   try {
            //       writer = new StreamWriter(request.GetRequestStream());
            //       writer.Write(postData.ToString());
            //   }
            //   finally {
            //       if (writer != null)
            //           writer.Close();
            //   }

            //   WebResponse response = request.GetResponse();
            //   StreamReader sr = new StreamReader(response.GetResponseStream());
            //   String json = sr.ReadToEnd().Trim();


            //   JavaScriptSerializer ser = new JavaScriptSerializer();
            //   var profileDic = ser.Deserialize<Dictionary<string, object>>(json);

            //   string access_token = (string)profileDic["access_token"];
            //   var refresh_token = profileDic["refresh_token"];

            //   int statusCode = -1;
            //   String strUrl = "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + access_token;
            //   request = WebRequest.Create(strUrl) as HttpWebRequest;


            //       using (HttpWebResponse res = (HttpWebResponse)request.GetResponse() as HttpWebResponse) {
            //           statusCode = (int)res.StatusCode;
            //           using (Stream dataStream = res.GetResponseStream()) {
            //               var resp = XDocument.Load(dataStream);
            //               XNamespace atom = "http://www.w3.org/2005/Atom";
            //               var q = from c in resp.Root.Descendants(atom + "author")
            //                       select new {
            //                           Name = (string)c.Element(atom + "name"),
            //                           Email = (string)c.Element(atom + "email")
            //                       };
            //               string name = string.Empty;
            //               string email = string.Empty;
            //               foreach (var person in q) {
            //                   name = person.Name;
            //                   email = person.Email;
            //               }

            //               if (Membership.GetUser(email) == null){

            //                   Session["GoogleUserName"] = name;
            //                   Session["GoogleUserEmail"] = email;

            //                   Session["CameFromSocialNetwork"] = true;
            //                   Session["CameFromGoogleLogin"] = true;
            //                   Response.Redirect(HttpUrlBuilder.BuildUrl("~/DesktopModules/CoreModules/Register/Register.aspx"));
            //               }
            //               else{
            //                   string urlHome = ConvertRelativeUrlToAbsoluteUrl("~");
            //                   Session["CameFromSocialNetwork"] = true;
            //                   PortalSecurity.SignOn(email, GeneratePasswordHash(email), false, urlHome);
            //               }

            //           }
            //       }
            //   }
            //   catch (Exception ex) {
            //       ErrorHandler.Publish(LogLevel.Error, "Google error ocurred", ex);
            //       Response.Redirect(HttpUrlBuilder.BuildUrl(""));
            //   }


            //}
            //else{
            //   ErrorHandler.Publish(LogLevel.Error,"Google settings weren't loaded.");
            //   Response.Redirect(HttpUrlBuilder.BuildUrl(""));
            //}

            OpenIdRelyingParty Openid = new OpenIdRelyingParty();
            var openIdUrl             = "http://www.google.com/accounts/o8/id";
            var response = Openid.GetResponse();

            if (response == null)
            {
                // User submitting Identifier
                Identifier id;
                if (Identifier.TryParse(openIdUrl, out id))
                {
                    try
                    {
                        var request = Openid.CreateRequest(openIdUrl);
                        var fetch   = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                        request.AddExtension(fetch);
                        request.AddExtension(new UIRequest()
                        {
                            Mode = UIModes.Popup
                        });
                        var b = new UriBuilder(Request.Url)
                        {
                            Query = ""
                        };
                        request.RedirectToProvider();
                    }
                    catch (ProtocolException ex)
                    {
                        ErrorHandler.Publish(LogLevel.Error, "Google Error", ex);
                        Response.Redirect(HttpUrlBuilder.BuildUrl("~/"));
                    }
                }
                ErrorHandler.Publish(LogLevel.Error, "OpenID Error...invalid url. url='" + openIdUrl + "'");
                Response.Redirect(HttpUrlBuilder.BuildUrl("~/"));
            }

            // OpenID Provider sending assertion response
            switch (response.Status)
            {
            case AuthenticationStatus.Authenticated:
                var    fetch     = response.GetExtension <FetchResponse>();
                string firstName = "unknown";
                string lastName  = "unknown";
                string email     = "unknown";
                if (fetch != null)
                {
                    firstName = fetch.GetAttributeValue(WellKnownAttributes.Name.First);
                    lastName  = fetch.GetAttributeValue(WellKnownAttributes.Name.Last);
                    email     = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                }
                if (Membership.GetUser(email) == null)
                {
                    Session["GoogleUserFirstName"] = firstName;
                    Session["GoogleUserLastName"]  = lastName;
                    Session["GoogleUserEmail"]     = email;

                    Session["CameFromSocialNetwork"] = true;
                    Session["CameFromGoogleLogin"]   = true;
                    var           redirect = ConvertRelativeUrlToAbsoluteUrl(HttpUrlBuilder.BuildUrl("~/DesktopModules/CoreModules/Register/Register.aspx"));
                    StringBuilder sb       = new StringBuilder();
                    sb.Append("<script language='javascript'>");
                    sb.Append("window.opener.location.href = '");
                    sb.Append(redirect);
                    sb.Append("';window.opener.focus();");
                    sb.Append("window.close();");
                    sb.Append("</script>");
                    Response.Write(sb.ToString());
                    Response.End();
                }
                else
                {
                    Session["CameFromSocialNetwork"] = true;
                    Session["UserName"] = email;
                    var           redirect = ConvertRelativeUrlToAbsoluteUrl(HttpUrlBuilder.BuildUrl("~/DesktopModules/CoreModules/SignIn/LoginIn.aspx"));
                    StringBuilder sb       = new StringBuilder();
                    sb.Append("<script language='javascript'>");
                    sb.Append("window.opener.location.href = '");
                    sb.Append(redirect);
                    sb.Append("';window.opener.focus();");
                    sb.Append("window.close();");
                    sb.Append("</script>");
                    Response.Write(sb.ToString());
                    Response.End();
                }
                break;

            case AuthenticationStatus.Canceled:
                ErrorHandler.Publish(LogLevel.Info, "OpenID: Cancelled at provider.");
                Response.Write("<script language=javascript>close();</script>");
                Response.End();
                Response.Redirect(HttpUrlBuilder.BuildUrl("~/"));
                break;

            case AuthenticationStatus.Failed:

                ErrorHandler.Publish(LogLevel.Error, "OpenID Exception...", response.Exception);
                Response.Write("<script language=javascript>close();</script>");
                Response.End();
                Response.Redirect(HttpUrlBuilder.BuildUrl("~/"));
                break;
            }
            Response.Redirect(HttpUrlBuilder.BuildUrl("~/"));
        }
Пример #42
0
        public void FetchRequest()
        {
            var request = new FetchRequest();

            request.ReplicaId       = _random.Next();
            request.MaxWaitTime     = _random.Next();
            request.MinBytes        = _random.Next();
            request.TopicPartitions = new[] {
                new FetchRequestTopicPartition {
                    TopicName          = Guid.NewGuid().ToString(),
                    FetchOffsetDetails = new [] {
                        new FetchRequestTopicPartitionDetail {
                            Partition   = _random.Next(),
                            FetchOffset = _random.Next(),
                            MaxBytes    = _random.Next()
                        }
                    }
                }
            };

            Stream binary1 = new MemoryStream();

            request.Serialize(binary1);

            binary1.Seek(0L, SeekOrigin.Begin);
            var request2 = new FetchRequest();

            request2.Deserialize(binary1);

            var compareLogic = new CompareLogic();
            var result       = compareLogic.Compare(request, request2);

            Assert.True(result.AreEqual);

            Stream binary2 = new MemoryStream();

            request.Serialize(binary2);
            Assert.Equal(binary1.Length, binary2.Length);

            using (var stream1 = new MemoryStream())
                using (var stream2 = new MemoryStream()) {
                    binary1.Seek(0L, SeekOrigin.Begin);
                    binary1.CopyTo(stream1);

                    binary2.Seek(0L, SeekOrigin.Begin);
                    binary2.CopyTo(stream2);

                    Assert.Equal(stream1.Length, stream2.Length);
                    stream1.Seek(0L, SeekOrigin.Begin);
                    var bytes1 = stream1.ToArray();

                    stream2.Seek(0L, SeekOrigin.Begin);
                    var bytes2 = stream2.ToArray();
                    Assert.Equal(bytes1.Length, bytes2.Length);

                    for (int i = 0; i < bytes1.Length; i++)
                    {
                        Assert.Equal(bytes1[i], bytes2[i]);
                    }
                }
        }
Пример #43
0
 /// <summary>
 /// Writes a fetch request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="FetchRequest"/> to send to the server.</param>
 public FetchResponse Send(FetchRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     return(this.Handle(request.RequestBuffer.GetBuffer(), new FetchResponse.Parser()));
 }
Пример #44
0
        public ActionResult Authenticate(string returnUrl)
        {
            var response = openid.GetResponse();

            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                {
                    try {
                        IAuthenticationRequest request = openid.CreateRequest(Request.Form["openid_identifier"]);
                        request.AddExtension(new ClaimsRequest {
                            Email      = DemandLevel.Require,
                            PostalCode = DemandLevel.Require,
                            TimeZone   = DemandLevel.Require,
                        });
                        var ax = new FetchRequest();
                        ax.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, true));
                        ax.Attributes.Add(new AttributeRequest(WellKnownAttributes.Name.FullName, true));
                        request.AddExtension(ax);
                        return(request.RedirectingResponse.AsActionResult());
                    } catch (ProtocolException ex) {
                        ViewData["Message"] = ex.Message;
                        return(View("Login"));
                    }
                }
                else
                {
                    ViewData["Message"] = "Invalid identifier";
                    return(View("Login"));
                }
            }
            else
            {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status)
                {
                case AuthenticationStatus.Authenticated:
                    Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                    Session["ClaimedIdentifier"]  = response.ClaimedIdentifier;
                    string claimedidentifier = response.ClaimedIdentifier;
                    FormsAuthentication.SetAuthCookie(response.FriendlyIdentifierForDisplay, false);
                    string username = MembershipService.GetUserName(claimedidentifier);
                    if (username != "")
                    {
                        FormsAuthentication.SetAuthCookie(username, false);
                        return(RedirectToAction("Index", "Home"));
                    }
                    else
                    {
                        return(RedirectToAction("Register", "Account"));
                    }

                case AuthenticationStatus.Canceled:
                    ViewData["Message"] = "Canceled at provider";
                    return(View("Login"));

                case AuthenticationStatus.Failed:
                    ViewData["Message"] = response.Exception.Message;
                    return(View("Login"));
                }
            }
            return(new EmptyResult());
        }
Пример #45
0
        public LoginProfile ProcessAuthoriztion(HttpContext context, IDictionary <string, string> @params)
        {
            var response = Openid.GetResponse();

            if (response == null)
            {
                Identifier id;
                if (Identifier.TryParse(@params["oid"], out id))
                {
                    try
                    {
                        IAuthenticationRequest request;

                        var realmUrlString = String.Empty;

                        if (@params.ContainsKey("realmUrl"))
                        {
                            realmUrlString = @params["realmUrl"];
                        }

                        if (!String.IsNullOrEmpty(realmUrlString))
                        {
                            request = Openid.CreateRequest(id, new Realm(realmUrlString));
                        }
                        else
                        {
                            request = Openid.CreateRequest(id);
                        }

                        request.AddExtension(new ClaimsRequest
                        {
                            Email      = DemandLevel.Require,
                            Nickname   = DemandLevel.Require,
                            Country    = DemandLevel.Request,
                            Gender     = DemandLevel.Request,
                            PostalCode = DemandLevel.Request,
                            TimeZone   = DemandLevel.Request,
                            FullName   = DemandLevel.Request,
                        });
                        var fetch = new FetchRequest();
                        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        //Duplicating attributes
                        fetch.Attributes.AddRequired("http://schema.openid.net/contact/email");//Add two more
                        fetch.Attributes.AddRequired("http://openid.net/schema/contact/email");
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Alias);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Media.Images.Default);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Name.Middle);
                        fetch.Attributes.AddRequired(WellKnownAttributes.Person.Gender);
                        fetch.Attributes.AddRequired(WellKnownAttributes.BirthDate.WholeBirthDate);
                        request.AddExtension(fetch);
                        request.RedirectToProvider();
                        context.Response.End();//This will throw thread abort
                    }
                    catch (ProtocolException ex)
                    {
                        return(LoginProfile.FromError(ex));
                    }
                }
                else
                {
                    return(LoginProfile.FromError(new Exception("invalid OpenID identifier")));
                }
            }
            else
            {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status)
                {
                case AuthenticationStatus.Authenticated:
                    var spprofile    = response.GetExtension <ClaimsResponse>();
                    var fetchprofile = response.GetExtension <FetchResponse>();

                    var realmUrlString = String.Empty;
                    if (@params.ContainsKey("realmUrl"))
                    {
                        realmUrlString = @params["realmUrl"];
                    }

                    var profile = ProfileFromOpenId(spprofile, fetchprofile, response.ClaimedIdentifier.ToString(), realmUrlString);
                    return(profile);

                case AuthenticationStatus.Canceled:
                    return(LoginProfile.FromError(new Exception("Canceled at provider")));

                case AuthenticationStatus.Failed:
                    return(LoginProfile.FromError(response.Exception));
                }
            }
            return(null);
        }
Пример #46
0
        public async Task ShouldRecoverUpdateMetadataForNewTopic()
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router = routerProxy.Create();

            int partitionId = 0;
            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            bool firstTime = true;
            Func<Task<FetchResponse>> ShouldReturnError = async () =>
             {
                 return new FetchResponse() { Error = (short)ErrorResponseCode.NoError };
             };

            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse;
            int numberOfCall = 1000;
            Task[] tasks = new Task[numberOfCall];
            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);
            }
            //change MetadataResponseFunction
            routerProxy.BrokerConn0.MetadataResponseFunction = async () =>
            {
                var response = await BrokerRouterProxy.DefaultMetadataResponse();
                response.Topics[0].Name = "test2";
                return response;
            };

            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(fetchRequest, "test2", partitionId);
            }

            await Task.WhenAll(tasks);
            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall));
            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2));
        }
        public void GetBytesValidStructure()
        {
            string topicName = "topic";
            int correlationId = 1;
            string clientId = "TestClient";
            int maxWait = 234;
            int minBytes = 345;

            var requestMap = new Dictionary<string, List<PartitionFetchInfo>>();

            requestMap[topicName] = new List<PartitionFetchInfo>() {new PartitionFetchInfo(2, 4000, 777)};

            var request = new FetchRequest(correlationId, clientId, maxWait, minBytes, requestMap);

            int requestSize = 4 + //request size
                              2 + //request type id
                              2 + //versionId
                              4 + //correlation id
                              BitWorks.GetShortStringLength(clientId, AbstractRequest.DefaultEncoding) + // client id length
                              4 + //replica id
                              4 + //max wait
                              4 + //min bytes
                              4 + //offset info count
                              //=== offset info part
                              request.OffsetInfo.Keys.Sum(x => BitWorks.GetShortStringLength(x, AbstractRequest.DefaultEncoding)) +
                              request.OffsetInfo.Values.Select(pl => 4 + pl.Sum(p => p.SizeInBytes)).Sum();

            var ms = new MemoryStream();
            request.WriteTo(ms);
            byte[] bytes = ms.ToArray();
            Assert.IsNotNull(bytes);

            // add 4 bytes for the length of the message at the beginning
            Assert.AreEqual(requestSize, bytes.Length);

            // first 4 bytes = the message length
            Assert.AreEqual(requestSize - 4, BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the request type
            Assert.AreEqual((short) RequestTypes.Fetch,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(4).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the version id
            Assert.AreEqual((short) FetchRequest.CurrentVersion,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(6).Take(2).ToArray<byte>()), 0));

            // next 2 bytes = the correlation id
            Assert.AreEqual(correlationId,
                            BitConverter.ToInt32(BitWorks.ReverseBytes(bytes.Skip(8).Take(4).ToArray<byte>()), 0));

            // next 2 bytes = the client id length
            Assert.AreEqual((short) clientId.Length,
                            BitConverter.ToInt16(BitWorks.ReverseBytes(bytes.Skip(12).Take(2).ToArray<byte>()), 0));

            // next few bytes = the client id
            Assert.AreEqual(clientId, Encoding.ASCII.GetString(bytes.Skip(14).Take(clientId.Length).ToArray<byte>()));

            // next 4 bytes = replica id
            Assert.AreEqual(-1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(14 + clientId.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = max wait
            Assert.AreEqual(maxWait,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(18 + clientId.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = min bytes
            Assert.AreEqual(minBytes,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(22 + clientId.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = offset info count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(bytes.Skip(26 + clientId.Length).Take(4).ToArray<byte>()), 0));

            //=== offset info part

            // next 2 bytes = the topic length
            Assert.AreEqual((short) topicName.Length,
                            BitConverter.ToInt16(
                                BitWorks.ReverseBytes(bytes.Skip(30 + clientId.Length).Take(2).ToArray<byte>()), 0));

            // next few bytes = the topic
            Assert.AreEqual(topicName,
                            Encoding.ASCII.GetString(
                                bytes.Skip(32 + clientId.Length).Take(topicName.Length).ToArray<byte>()));

            // next 4 bytes = partitions count
            Assert.AreEqual(1,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(32 + clientId.Length + topicName.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = partition
            Assert.AreEqual(2,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(36 + clientId.Length + topicName.Length).Take(4).ToArray<byte>()), 0));

            // next 4 bytes = offset
            Assert.AreEqual(4000,
                            BitConverter.ToInt64(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(40 + clientId.Length + topicName.Length).Take(8).ToArray<byte>()), 0));

            // next 4 bytes = fetch size
            Assert.AreEqual(777,
                            BitConverter.ToInt32(
                                BitWorks.ReverseBytes(
                                    bytes.Skip(48 + clientId.Length + topicName.Length).Take(8).ToArray<byte>()), 0));
        }
Пример #48
0
        public async Task ShouldRecoverUpdateMetadataForNewTopic()
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router = routerProxy.Create();

            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnValidMessage;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers;
            int numberOfCall = 1000;
            Task[] tasks = new Task[numberOfCall];
            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, _partitionId);
            }

            routerProxy.BrokerConn0.MetadataResponseFunction = async () =>
            {
                var response = await BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers();
                response.Topics[0].Name = "test2";
                return response;
            };

            for (int i = 0; i < numberOfCall / 2; i++)
            {
                tasks[i + numberOfCall / 2] = protocolGateway.SendProtocolRequest(fetchRequest, "test2", _partitionId);
            }

            await Task.WhenAll(tasks);
            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall));
            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2));
        }
Пример #49
0
        public async Task SendProtocolRequestShouldNoTryToRefreshMataDataIfCanNotRecoverByRefreshMetadata(ErrorResponseCode code)
        {
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(10);
            var router = routerProxy.Create();

            int partitionId = 0;
            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            bool firstTime = true;
            Func<Task<FetchResponse>> ShouldReturnError = async () =>
            {
                if (firstTime)
                {
                    firstTime = !firstTime;
                    return new FetchResponse() { Error = (short)code };
                }
                return new FetchResponse() { Error = (short)ErrorResponseCode.NoError };
            };
            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnError;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.DefaultMetadataResponse;
            await protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);
        }
Пример #50
0
 /// <summary>
 /// Writes a fetch request to the server.
 /// </summary>
 /// <remarks>
 /// Write timeout is defaulted to infitite.
 /// </remarks>
 /// <param name="request">The <see cref="FetchRequest"/> to send to the server.</param>
 public void Write(FetchRequest request)
 {
     this.EnsuresNotDisposed();
     Guard.NotNull(request, "request");
     this.Write(request.RequestBuffer.GetBuffer());
 }
Пример #51
0
        /// <summary>
        /// Request inventory items
        /// </summary>
        /// <param name="itemIDs">Inventory items to request</param>
        /// <param name="ownerID">Owners of the inventory items</param>
        /// <param name="callback"></param>
        /// <seealso cref="InventoryManager.OnItemReceived"/>
        public void RequestFetchItems(ICollection<UUID> itemIDs, UUID ownerID, FetchItemsCallback callback)
        {
            FetchRequest request = new FetchRequest(callback, itemIDs);
            lock (_FetchRequests)
                _FetchRequests.Add(request);

            // Send the packet:
            FetchInventoryPacket fetch = new FetchInventoryPacket();
            fetch.AgentData = new FetchInventoryPacket.AgentDataBlock();
            fetch.AgentData.AgentID = _Agents.AgentID;
            fetch.AgentData.SessionID = _Agents.SessionID;

            fetch.InventoryData = new FetchInventoryPacket.InventoryDataBlock[itemIDs.Count];
            int i = 0;
            foreach (UUID item in itemIDs)
            {
                fetch.InventoryData[i] = new FetchInventoryPacket.InventoryDataBlock();
                fetch.InventoryData[i].ItemID = item;
                fetch.InventoryData[i].OwnerID = ownerID;
                ++i;
            }

            _Network.SendPacket(fetch);
        }
Пример #52
0
        public async Task TestNewTopicProductionWorksOk()
        {
            using (var temporaryTopic = testCluster.CreateTemporaryTopic())
            using (var connection = await KafkaConnectionFactory.CreateSimpleKafkaConnectionAsync(testCluster.CreateBrokerUris()[0]))
            {
                var topic = temporaryTopic.Name;
                {
                    var request = new MetadataRequest
                    {
                        Topics = new List<string>
                         {
                             topic
                         }
                    };
                    MetadataResponse response = null;
                    while (response == null)
                    {
                        response = await connection.SendRequestAsync(request, CancellationToken.None);
                        if (response.Topics[0].ErrorCode == ErrorResponseCode.LeaderNotAvailable)
                        {
                            response = null;
                            await Task.Delay(1000);
                        }

                    }
                    Assert.That(response, Is.Not.Null);
                    var first = response;
                    Assert.That(first.Topics, Has.Length.EqualTo(1));

                    var firstTopic = first.Topics.First();
                    Assert.That(firstTopic.ErrorCode, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(firstTopic.Name, Is.EqualTo(topic));
                    Assert.That(firstTopic.Partitions, Has.Length.EqualTo(1));

                    var firstPartition = firstTopic.Partitions.First();
                    Assert.That(firstPartition.PartitionId, Is.EqualTo(0));
                }

                {
                    var request = new ProduceRequest
                    {
                        Acks = 1,
                        TimeoutMS = 10000,
                        Payload = new List<Payload>
                             {
                                 new Payload
                                 {
                                      Topic = topic,
                                      Partition = 0,
                                      Codec = MessageCodec.CodecNone,
                                      Messages = new List<Message>
                                      {
                                          new Message("Message 1"),
                                          new Message("Message 2"),
                                          new Message("Message 3"),
                                          new Message("Message 4"),
                                      }
                                 }
                             }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Is.Not.Null);

                    var first = response.First();
                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.Offset, Is.EqualTo(0));
                }

                {
                    var request = new FetchRequest
                    {
                        MinBytes = 0,
                        MaxWaitTime = 0,
                        Fetches = new List<Fetch>
                             {
                                 new Fetch
                                 {
                                    MaxBytes = 40,
                                    Offset = 0,
                                    PartitionId = 0,
                                    Topic = topic,
                                 }
                            }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.HighWaterMark, Is.EqualTo(4));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.Messages, Has.Count.EqualTo(1));

                    var firstMessage = first.Messages.First();
                    Assert.That(firstMessage.Meta.Offset, Is.EqualTo(0));
                    Assert.That(firstMessage.Meta.PartitionId, Is.EqualTo(0));
                    Assert.That(firstMessage.Attribute, Is.EqualTo(0));
                    Assert.That(firstMessage.Key, Is.Null);
                    Assert.That(firstMessage.MagicNumber, Is.EqualTo(0));
                    Assert.That(firstMessage.Value, Is.Not.Null);

                    var firstString = firstMessage.Value.ToUtf8String();
                    Assert.That(firstString, Is.EqualTo("Message 1"));
                }

                {
                    var request = new OffsetRequest
                    {
                        Offsets = new List<Offset>
                             {
                                 new Offset
                                 {
                                      MaxOffsets = 2,
                                      PartitionId = 0,
                                      Time = -1,
                                      Topic = topic
                                 }
                             }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.Offsets, Has.Length.EqualTo(2));

                    Assert.That(first.Offsets[0], Is.EqualTo(4));
                    Assert.That(first.Offsets[1], Is.EqualTo(0));
                }

                {
                    var request = new ConsumerMetadataRequest
                    {
                        ConsumerGroup = topic
                    };
                    ConsumerMetadataResponse response = null;
                    while (response == null)
                    {
                        response = await connection.SendRequestAsync(request, CancellationToken.None);
                        if (response.Error == ErrorResponseCode.ConsumerCoordinatorNotAvailableCode)
                        {
                            response = null;
                            await Task.Delay(1000);
                        }
                    }
                    Assert.That(response.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Console.WriteLine("Id = {0}, Host = {1}, Port = {2}", response.CoordinatorId, response.CoordinatorHost, response.CoordinatorPort);

                }

                {
                    var request = new OffsetFetchRequest
                    {
                        ConsumerGroup = topic,
                        Topics = new List<OffsetFetch>
                              {
                                  new OffsetFetch
                                  {
                                       PartitionId = 0,
                                       Topic = topic
                                  }
                              }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.MetaData, Is.Empty);
                    Assert.That(first.Offset, Is.EqualTo(-1));
                }

                {
                    var request = new OffsetCommitRequest
                    {
                        ConsumerGroup = topic,
                        ConsumerGroupGenerationId = 1,
                        ConsumerId = "0",
                        OffsetCommits = new List<OffsetCommit>
                               {
                                   new OffsetCommit
                                   {
                                        Metadata = "Metadata 1",
                                        Offset = 0,
                                        PartitionId = 0,
                                        TimeStamp = -1,
                                        Topic = topic,
                                   }
                               }
                    };
                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                }

                {
                    var request = new OffsetFetchRequest
                    {
                        ConsumerGroup = topic,
                        Topics = new List<OffsetFetch>
                              {
                                  new OffsetFetch
                                  {
                                       PartitionId = 0,
                                       Topic = topic
                                  }
                              }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.MetaData, Is.EqualTo("Metadata 1"));
                    Assert.That(first.Offset, Is.EqualTo(0));
                }

                {
                    var request = new FetchRequest
                    {
                        MinBytes = 0,
                        MaxWaitTime = 0,
                        Fetches = new List<Fetch>
                             {
                                 new Fetch
                                 {
                                    MaxBytes = 1024,
                                    Offset = 0 + 1,
                                    PartitionId = 0,
                                    Topic = topic,
                                 }
                            }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.HighWaterMark, Is.EqualTo(4));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.Messages, Has.Count.EqualTo(3));

                    var firstMessage = first.Messages.First();
                    Assert.That(firstMessage.Meta.Offset, Is.EqualTo(1));
                    Assert.That(firstMessage.Meta.PartitionId, Is.EqualTo(0));
                    Assert.That(firstMessage.Attribute, Is.EqualTo(0));
                    Assert.That(firstMessage.Key, Is.Null);
                    Assert.That(firstMessage.MagicNumber, Is.EqualTo(0));
                    Assert.That(firstMessage.Value, Is.Not.Null);

                    var firstString = firstMessage.Value.ToUtf8String();
                    Assert.That(firstString, Is.EqualTo("Message 2"));

                    var lastMessage = first.Messages.Last();
                    Assert.That(lastMessage.Meta.Offset, Is.EqualTo(3));
                    Assert.That(lastMessage.Meta.PartitionId, Is.EqualTo(0));
                    Assert.That(lastMessage.Attribute, Is.EqualTo(0));
                    Assert.That(lastMessage.Key, Is.Null);
                    Assert.That(lastMessage.MagicNumber, Is.EqualTo(0));
                    Assert.That(lastMessage.Value, Is.Not.Null);

                    var lastString = lastMessage.Value.ToUtf8String();
                    Assert.That(lastString, Is.EqualTo("Message 4"));


                }

                {
                    var request = new FetchRequest
                    {
                        MinBytes = 0,
                        MaxWaitTime = 0,
                        Fetches = new List<Fetch>
                             {
                                 new Fetch
                                 {
                                    MaxBytes = 1024,
                                    Offset = 3 + 1,
                                    PartitionId = 0,
                                    Topic = topic,
                                 }
                            }
                    };

                    var response = await connection.SendRequestAsync(request, CancellationToken.None);
                    Assert.That(response, Has.Count.EqualTo(1));
                    var first = response.First();

                    Assert.That(first.Error, Is.EqualTo(ErrorResponseCode.NoError));
                    Assert.That(first.HighWaterMark, Is.EqualTo(4));
                    Assert.That(first.PartitionId, Is.EqualTo(0));
                    Assert.That(first.Topic, Is.EqualTo(topic));
                    Assert.That(first.Messages, Has.Count.EqualTo(0));
                }
            }
            Console.WriteLine("Test completed");
        }
Пример #53
0
        public async Task ShouldRecoverFromFailerByUpdateMetadataOnce() //Do not debug this test !!
        {
            var log = new DefaultTraceLog();
            var routerProxy = new BrokerRouterProxy(_kernel);
            routerProxy._cacheExpiration = TimeSpan.FromMilliseconds(1000);
            var router = routerProxy.Create();

            int partitionId = 0;
            ProtocolGateway protocolGateway = new ProtocolGateway(router);
            var fetchRequest = new FetchRequest();

            int numberOfCall = 100;
            long numberOfErrorSend = 0;
            TaskCompletionSource<int> x = new TaskCompletionSource<int>();
            Func<Task<FetchResponse>> ShouldReturnNotLeaderForPartitionAndThenNoError = async () =>
            {
                log.DebugFormat("FetchResponse Start ");
                if (!x.Task.IsCompleted)
                {
                    if (Interlocked.Increment(ref numberOfErrorSend) == numberOfCall)
                    {
                        await Task.Delay(routerProxy._cacheExpiration);
                        await Task.Delay(1);
                        x.TrySetResult(1);
                        log.DebugFormat("all is complete ");
                    }

                    await x.Task;
                    log.DebugFormat("SocketException ");
                    throw new BrokerConnectionException("",new KafkaEndpoint());
                }
                log.DebugFormat("Completed ");

                return new FetchResponse() { Error = (short)ErrorResponseCode.NoError };
            };

            routerProxy.BrokerConn0.FetchResponseFunction = ShouldReturnNotLeaderForPartitionAndThenNoError;
            routerProxy.BrokerConn0.MetadataResponseFunction = BrokerRouterProxy.CreateMetadataResponseWithMultipleBrokers;

            Task[] tasks = new Task[numberOfCall];

            for (int i = 0; i < numberOfCall; i++)
            {
                tasks[i] = protocolGateway.SendProtocolRequest(fetchRequest, BrokerRouterProxy.TestTopic, partitionId);
            }

            await Task.WhenAll(tasks);
            Assert.That(numberOfErrorSend, Is.GreaterThan(1), "numberOfErrorSend");
            Assert.That(routerProxy.BrokerConn0.FetchRequestCallCount, Is.EqualTo(numberOfCall + numberOfErrorSend),
                "FetchRequestCallCount");
            Assert.That(routerProxy.BrokerConn0.MetadataRequestCallCount, Is.EqualTo(2), "MetadataRequestCallCount");
        }
Пример #54
0
 public VirtVarRequestContextStub(FetchRequest request, double referenceUncrtatinty)
 {
     this.request = request;
     this.referenceUncrtatinty = referenceUncrtatinty;
 }
Пример #55
0
 public FetchData fetchData(FetchRequest request)
 {
    lock (sync)
    {
       if (closed)
       {
          throw new ApplicationException("RRD already closed, cannot fetch data");
       }
       Archive archive = findMatchingArchive(request);
       return archive.fetchData(request);
    }
 }
Пример #56
0
        public void IsValidNoTopic()
        {
            FetchRequest request = new FetchRequest(" ", 1, 10L, 100);

            Assert.IsFalse(request.IsValid());
        }
Пример #57
0
      public FetchData fetchData(FetchRequest request)
      {
         long arcStep = getArcStep();
         long fetchStart = Util.normalize(request.FetchStart, arcStep);
         long fetchEnd = Util.normalize(request.FetchEnd, arcStep);
         if (fetchEnd < request.FetchEnd)
         {
            fetchEnd += arcStep;
         }
         long startTime = getStartTime();
         long endTime = getEndTime();
         String[] dsToFetch = request.getFilter();
         if (dsToFetch == null)
         {
            dsToFetch = parentDb.getDsNames();
         }
         int dsCount = dsToFetch.Length;
         int ptsCount = (int)((fetchEnd - fetchStart) / arcStep + 1);
         long[] timestamps = new long[ptsCount];
         double[][] values = new double[dsCount][];
         for (int i = 0; i < dsCount; i++)
            values[i] = new double[ptsCount];


         long matchStartTime = Math.Max(fetchStart, startTime);
         long matchEndTime = Math.Min(fetchEnd, endTime);
         double[][] robinValues = null;
         if (matchStartTime <= matchEndTime)
         {
            // preload robin values
            int matchCount = (int)((matchEndTime - matchStartTime) / arcStep + 1);
            int matchStartIndex = (int)((matchStartTime - startTime) / arcStep);
            robinValues = new double[dsCount][];
            for (int i = 0; i < dsCount; i++)
            {
               int dsIndex = parentDb.getDsIndex(dsToFetch[i]);
               robinValues[i] = robins[dsIndex].getValues(matchStartIndex, matchCount);
            }
         }
         for (int ptIndex = 0; ptIndex < ptsCount; ptIndex++)
         {
            long time = fetchStart + ptIndex * arcStep;
            timestamps[ptIndex] = time;
            for (int i = 0; i < dsCount; i++)
            {
               double value = Double.NaN;
               if (time >= matchStartTime && time <= matchEndTime)
               {
                  // inbound time
                  int robinValueIndex = (int)((time - matchStartTime) / arcStep);
                  Debug.Assert(robinValues != null);
                  value = robinValues[i][robinValueIndex];
               }
               values[i][ptIndex] = value;
            }
         }
         FetchData fetchData = new FetchData(steps.get(),endTime, parentDb.getDsNames());
         fetchData.setTimestamps(timestamps);
         fetchData.setValues(values);
         return fetchData;
      }
Пример #58
0
      private Archive findMatchingArchive(FetchRequest request)
      {
         long fetchStart = request.FetchStart;
         long fetchEnd = request.FetchEnd;
         long resolution = request.Resolution;
         Archive bestFullMatch = null, bestPartialMatch = null;
         long bestStepDiff = 0, bestMatch = 0;
         foreach (Archive archive in archives)
         {
            if (archive.getConsolFun().Name == request.ConsolidateFunctionName)
            {
               long arcStep = archive.getArcStep();
               long arcStart = archive.getStartTime() - arcStep;
               long arcEnd = archive.getEndTime();
               long fullMatch = fetchEnd - fetchStart;
               if (arcEnd >= fetchEnd && arcStart <= fetchStart)
               {
                  long tmpStepDiff = Math.Abs(archive.getArcStep() - resolution);

                  if (tmpStepDiff < bestStepDiff || bestFullMatch == null)
                  {
                     bestStepDiff = tmpStepDiff;
                     bestFullMatch = archive;
                  }
               }
               else
               {
                  long tmpMatch = fullMatch;

                  if (arcStart > fetchStart)
                  {
                     tmpMatch -= (arcStart - fetchStart);
                  }
                  if (arcEnd < fetchEnd)
                  {
                     tmpMatch -= (fetchEnd - arcEnd);
                  }
                  if (bestPartialMatch == null || bestMatch < tmpMatch)
                  {
                     bestPartialMatch = archive;
                     bestMatch = tmpMatch;
                  }
               }
            }
         }
         if (bestFullMatch != null)
         {
            return bestFullMatch;
         }
         else if (bestPartialMatch != null)
         {
            return bestPartialMatch;
         }
         else
         {
            throw new ApplicationException("RRD file does not contain RRA: " + request.ConsolidateFunctionName + " archive");
         }
      }
Пример #59
0
        private Task ConsumeTopicPartitionAsync(string topic, int partitionId)
        {
            return Task.Run(async () =>
            {
                try
                {
                    var bufferSizeHighWatermark = FetchRequest.DefaultBufferSize;

                    _options.Log.DebugFormat("Consumer: Creating polling task for topic: {0} on parition: {1}", topic, partitionId);
                    while (_disposeToken.IsCancellationRequested == false)
                    {
                        try
                        {
                            //get the current offset, or default to zero if not there.
                            long offset = 0;
                            _partitionOffsetIndex.AddOrUpdate(partitionId, i => offset, (i, currentOffset) => { offset = currentOffset; return currentOffset; });

                            //build a fetch request for partition at offset
                            var fetch = new Fetch
                            {
                                Topic = topic,
                                PartitionId = partitionId,
                                Offset = offset,
                                MaxBytes = bufferSizeHighWatermark,
                            };

                            var fetches = new List<Fetch> { fetch };

                            var fetchRequest = new FetchRequest
                                {
                                    MaxWaitTime = (int)Math.Min((long)int.MaxValue, _options.MaxWaitTimeForMinimumBytes.TotalMilliseconds),
                                    MinBytes = _options.MinimumBytes,
                                    Fetches = fetches
                                };

                            //make request and post to queue
                            var route = _options.Router.SelectBrokerRoute(topic, partitionId);

                            var responses = await route.Connection.SendAsync(fetchRequest).ConfigureAwait(false);

                            if (responses.Count > 0)
                            {
                                var response = responses.FirstOrDefault(); //we only asked for one response

                                if (response != null && response.Messages.Count > 0)
                                {
                                    HandleResponseErrors(fetch, response);

                                    foreach (var message in response.Messages)
                                    {
                                        _fetchResponseQueue.Add(message, _disposeToken.Token);

                                        if (_disposeToken.IsCancellationRequested) return;
                                    }

                                    var nextOffset = response.Messages.Max(x => x.Meta.Offset) + 1;
                                    _partitionOffsetIndex.AddOrUpdate(partitionId, i => nextOffset, (i, l) => nextOffset);

                                    // sleep is not needed if responses were received
                                    continue;
                                }
                            }

                            //no message received from server wait a while before we try another long poll
                            Thread.Sleep(_options.BackoffInterval);
                        }
                        catch (BufferUnderRunException ex)
                        {
                            bufferSizeHighWatermark = (int)(ex.RequiredBufferSize * _options.FetchBufferMultiplier) + ex.MessageHeaderSize;
                            _options.Log.InfoFormat("Buffer underrun.  Increasing buffer size to: {0}", bufferSizeHighWatermark);
                        }
                        catch (OffsetOutOfRangeException ex)
                        {
                            //TODO this turned out really ugly.  Need to fix this section.
                            _options.Log.ErrorFormat(ex.Message);
                            FixOffsetOutOfRangeExceptionAsync(ex.FetchRequest);
                        }
                        catch (InvalidMetadataException ex)
                        {
                            //refresh our metadata and ensure we are polling the correct partitions
                            _options.Log.ErrorFormat(ex.Message);
                            _options.Router.RefreshTopicMetadata(topic);
                            EnsurePartitionPollingThreads();
                        }
                        catch (Exception ex)
                        {
                            _options.Log.ErrorFormat("Exception occured while polling topic:{0} partition:{1}.  Polling will continue.  Exception={2}", topic, partitionId, ex);
                        }
                    }
                }
                finally
                {
                    _options.Log.DebugFormat("Consumer: Disabling polling task for topic: {0} on parition: {1}", topic, partitionId);
                    Task tempTask;
                    _partitionPollingIndex.TryRemove(partitionId, out tempTask);
                }
            });
        }
Пример #60
0
        /// <summary>
        /// Called by <see cref="DataPortal" /> to load an
        /// existing business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
#pragma warning disable 1998
        public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
#pragma warning restore 1998
        {
#if !SILVERLIGHT && !NETFX_CORE
            ChannelFactory <IWcfPortal> cf = GetChannelFactory();
            var         proxy    = GetProxy(cf);
            WcfResponse response = null;
#if NET40
            try
            {
                var request = new FetchRequest(objectType, criteria, context);
                if (isSync)
                {
                    response = proxy.Fetch(request);
                }
                else
                {
                    var worker = new Csla.Threading.BackgroundWorker();
                    var tcs    = new TaskCompletionSource <WcfResponse>();
                    worker.RunWorkerCompleted += (o, e) =>
                    {
                        tcs.SetResult((WcfResponse)e.Result);
                    };
                    worker.DoWork += (o, e) =>
                    {
                        e.Result = proxy.Fetch(request);
                    };
                    worker.RunWorkerAsync();
                    response = await tcs.Task;
                }
                if (cf != null)
                {
                    cf.Close();
                }
                object result = response.Result;
                if (result is Exception)
                {
                    throw (Exception)result;
                }
                return((DataPortalResult)result);
            }
            catch
            {
                cf.Abort();
                throw;
            }
#else
            try
            {
                var request = new FetchRequest(objectType, criteria, context);
                if (isSync)
                {
                    response = proxy.Fetch(request);
                }
                else
                {
                    response = await proxy.FetchAsync(request);
                }
                if (cf != null)
                {
                    cf.Close();
                }
            }
            catch
            {
                cf.Abort();
                throw;
            }
            object result = response.Result;
            if (result is Exception)
            {
                throw (Exception)result;
            }
            return((DataPortalResult)result);
#endif
#else // WinRT and Silverlight
            var request = GetBaseCriteriaRequest();
            request.TypeName = objectType.AssemblyQualifiedName;
            if (!(criteria is IMobileObject))
            {
                criteria = new PrimitiveCriteria(criteria);
            }
            request.CriteriaData = MobileFormatter.Serialize(criteria);

            request = ConvertRequest(request);

            var proxy = GetProxy();
            DataPortalResult result = null;

#if !NETFX_CORE // Silverlight
            var tcs = new TaskCompletionSource <DataPortalResult>();
            proxy.FetchCompleted += (s, e) =>
            {
                try
                {
                    Csla.WcfPortal.WcfResponse response = null;
                    if (e.Error == null)
                    {
                        response = ConvertResponse(e.Result);
                    }
                    ContextDictionary globalContext = null;
                    if (response != null)
                    {
                        globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
                    }
                    if (e.Error == null && response != null && response.ErrorData == null)
                    {
                        var obj = MobileFormatter.Deserialize(response.ObjectData);
                        result = new DataPortalResult(obj, null, globalContext);
                    }
                    else if (response != null && response.ErrorData != null)
                    {
                        var ex = new DataPortalException(response.ErrorData);
                        result = new DataPortalResult(null, ex, globalContext);
                    }
                    else
                    {
                        result = new DataPortalResult(null, e.Error, globalContext);
                    }
                }
                catch (Exception ex)
                {
                    result = new DataPortalResult(null, ex, null);
                }
                finally
                {
                    tcs.SetResult(result);
                }
            };
            proxy.FetchAsync(request);
            var finalresult = await tcs.Task;
            if (finalresult.Error != null)
            {
                throw finalresult.Error;
            }
            return(finalresult);
#else // WinRT
            try
            {
                var response = await proxy.FetchAsync(request);

                response = ConvertResponse(response);
                if (response == null)
                {
                    throw new DataPortalException("null response", null);
                }
                var globalContext = (ContextDictionary)MobileFormatter.Deserialize(response.GlobalContext);
                if (response.ErrorData == null)
                {
                    var obj = MobileFormatter.Deserialize(response.ObjectData);
                    result = new DataPortalResult(obj, null, globalContext);
                }
                else
                {
                    var ex = new DataPortalException(response.ErrorData);
                    result = new DataPortalResult(null, ex, globalContext);
                }
            }
            catch (Exception ex)
            {
                result = new DataPortalResult(null, ex, null);
            }
            if (result.Error != null)
            {
                throw result.Error;
            }
            return(result);
#endif
#endif
        }