private bool getBannerAd(String MxitUserID, MXit.User.GenderType userGenderType, int displayWidth, int displayHeight, int userAge, out BannerAd adDetail) { //Default to no preselected ad unit String preselectedAdUnitID = ""; return getBannerAd(MxitUserID, userGenderType, displayWidth, displayHeight, userAge, preselectedAdUnitID, out adDetail); }
private bool getBannerAd(String MxitUserID, MXit.User.GenderType userGenderType, int displayWidth, int displayHeight, int userAge, out BannerAd adDetail) { bool success = false; adDetail = new BannerAd(); //Post back to openx String openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_120; String deviceName = "samsung/sgh"; //default to most popular device if (displayWidth >= 480) { openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_320; deviceName = "android"; } else if (displayWidth >= 300) { openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_320; deviceName = "nokia/c3"; } else if (displayWidth >= 240) { openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_240; deviceName = "nokia/5130%20xpressmusic"; } else if (displayWidth >= 180) { openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_180; deviceName = "samsung/sgh"; } else { openXAdUnitToUse = AdvertConfig.OpenX_AdUnitID_120; deviceName = "samsung/sgh"; } string strOpenxUrl = AdvertConfig.OpenX_URL + openXAdUnitToUse; string strUserDetails = "&" + "c.device=" + deviceName + "&" + "c.age=" + userAge + "&" + "c.gender=" + userGenderType.ToString().ToLower() + "&" + "xid=" + MxitUserID + "&" + "c.country=za"; string strCompleteUrl = strOpenxUrl + strUserDetails; //use the complete url on a mobile HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strCompleteUrl); req.UserAgent = "Mozilla Compatible mxit_client"; Random random = new Random(DateTime.Now.Second); int randomUpTo254 = random.Next(1, 254); String tempIP = "196.25.101." + randomUpTo254; req.Headers.Add("X-Forwarded-For", tempIP); req.Referer = AdvertConfig.appID; req.Timeout = AdvertConfig.bannerAdTimeout; req.Proxy = null;//GlobalProxySelection.GetEmptyWebProxy(); // null; req.KeepAlive = false; req.ServicePoint.ConnectionLeaseTimeout = 1000; req.ServicePoint.MaxIdleTime = 1000; //if (AdvertConfig.isShowMessages) //{ // Console.WriteLine(DateTime.Now.ToString() + " URL: " + strCompleteUrl); //} string strResponse = ""; try { logger.Debug("[" + MethodBase.GetCurrentMethod().Name + "()] - Starting to read server ad request..."); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader streamIn = new StreamReader(responseStream)) { strResponse = streamIn.ReadToEnd(); streamIn.Close(); } responseStream.Flush(); responseStream.Close(); } response.Close(); } logger.Debug("[" + MethodBase.GetCurrentMethod().Name + "()] - Finished reading server ad request..."); if (strResponse.Contains("media")) { //pass in the tags to remove //Determine the ad type: //We aren't using XML processing to try to save processing time, not having to prase the entire XML response into objects, etc. Instead we use basic string searches. //Welcome for somebody to do a benchmark to see if XML parsing will be faster or slower. adDetail.creativeType = Between(strResponse, "<ad ", "<html>", "type=", 0, ">"); bool isGotCreativeType = (!string.IsNullOrEmpty(adDetail.creativeType)); if (isGotCreativeType) { //Get the generic click and impression fields adDetail.clickURL = Between(strResponse, "<click>", "</click>", "", 7, ""); adDetail.impressionURL = Between(strResponse, "<impression>", "</impression>", "", 12, ""); //adDetail.inView = Between(strResponse, "<inview>", "</inview>", "", 8, ""); //Do logic for image type ad: if (adDetail.creativeType == "image") { adDetail.altText = Between(strResponse, "<creative", "<media>", "alt=", 0, "target"); if (!String.IsNullOrEmpty(adDetail.altText)) { adDetail.adImageURL = Between(strResponse, "<media>", "</media>", "", 7, ""); success = true; } else { logger.Error(MethodBase.GetCurrentMethod().Name + " - " + "SHINKA ERROR: Could not find alt text in string: " + strResponse + " USING URL: " + strCompleteUrl); success = false; } } else if (adDetail.creativeType == "html") { //Use the altText field for the text ad body String hrefHTMLText = Between(strResponse, "<media>", "</media>", "", 7, ""); int startIndex = hrefHTMLText.IndexOf(">") + 4; int endIndex = hrefHTMLText.IndexOf("</a>"); int length = endIndex - startIndex; adDetail.altText = hrefHTMLText.Substring(startIndex, length); success = true; } else { logger.Error(MethodBase.GetCurrentMethod().Name + " - " + "SHINKA ERROR: Not a known creative type: " + adDetail.creativeType); success = false; } } else { logger.Error(MethodBase.GetCurrentMethod().Name + " - " + "SHINKA ERROR: Could not get creative type: " + strResponse + " USING URL: " + strCompleteUrl); success = false; } } else { logger.Debug(MethodBase.GetCurrentMethod().Name + " - " + "SHINKA Warning: Empty ad response:" + strResponse + " USING URL: " + strCompleteUrl + " FOR USER: "******" - " + "Error doing Shinka Server Add Call: " + ex.ToString()); logger.Error(MethodBase.GetCurrentMethod().Name + " - " + "Error with String: " + strResponse); success = false; } finally { req.Abort(); req = null; GC.Collect(); } if ((adDetail.creativeType == "image") && !String.IsNullOrEmpty(adDetail.adImageURL)) { logger.Debug("[" + MethodBase.GetCurrentMethod().Name + "()] - Starting to read ad bitmap image..."); //convert the url into an image and load to the bitmap adDetail.adImage = BitmapFromWeb(adDetail.adImageURL); logger.Debug("[" + MethodBase.GetCurrentMethod().Name + "()] - Finished reading ad bitmap image..."); success = true; } //add banner to user session //userSession.CurrentBannerAd = adDetail; return success; }
private void appendBannerImage(ref MessageToSend messageToSend, MXit.User.UserInfo userInfo, BannerAd adTodisplay) { if ((AdvertConfig.bannerCacheSize > 0) && (messageToSend.ToDevice.HasFeature(DeviceFeatures.Gaming))) { //use ImageStrips to allow caching of images on users device string bannerHash = GetImageHash(adTodisplay.adImage); int cachePosition; if (!bannerHashMap.ContainsKey(bannerHash)) { cachePosition = lastCachePosition; //we create a dictionary of ImageStrips with the required sizes AdvertStripCollection bannerStrips = new AdvertStripCollection(bannerHash, adTodisplay.adImage); bannerStripCache[cachePosition] = bannerStrips; //remove any other hashes that are using this position foreach (string key in bannerHashMap.Keys) { if (bannerHashMap[key] == cachePosition) bannerHashMap.Remove(key); break; } //and assign this position to the hash bannerHashMap[bannerHash] = cachePosition; //advance the pointer of the last added imagestrip. lastCachePosition++; if (lastCachePosition == AdvertConfig.bannerCacheSize) { lastCachePosition = 0; } } else { cachePosition = bannerHashMap[bannerHash]; } string userSize = GetUserSize(userInfo); //this doesn't allow client-side auto resizing of images, may want to consider server side resizing ITable boardAd = MessageBuilder.Elements.CreateTable(messageToSend, "ad-" + bannerHash + "-" + userSize, 1, 1); boardAd.SelectionMode = SelectionRectType.Outline; boardAd.Style.Align = (AlignmentType)((int)AlignmentType.VerticalCenter + (int)AlignmentType.HorizontalCenter); boardAd.Mode = TableSendModeType.Update; //Mxit SDK 1.4.6 shows Frames.Set as obsolete, to be replaced with Frames.Add, but will break compatibility with older SDK's boardAd[0, 0].Frames.Set(bannerStripCache[cachePosition].GetStrip(userSize), 0); messageToSend.Append(boardAd); } else { int displayWidth = userInfo.DeviceInfo.DisplayWidth; int imageDisplayWidthPerc; if (displayWidth <= 128) { imageDisplayWidthPerc = 99; } else { imageDisplayWidthPerc = 100; } IMessageElement inlineImage = MessageBuilder.Elements.CreateInlineImage(adTodisplay.adImage, ImageAlignment.Center, TextFlow.AloneOnLine, imageDisplayWidthPerc); //IMessageElement inlineImage = MessageBuilder.Elements.CreateInlineImage(adTodisplay.adImageURL, ImageAlignment.Center, TextFlow.AloneOnLine); messageToSend.AppendLine(inlineImage); } }