Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send through bidders configuration so that it's available everywhere … #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions bidding/src/main/java/com/bidtorrent/bidding/AuctionResult.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
package com.bidtorrent.bidding;

import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;

import java.io.Serializable;
import java.util.Collection;

public class AuctionResult implements Serializable {
private IBidder winningBidder;
private final BidResponse winningBid;
private final float winningPrice;
private long runnerUp;
private Collection<BidResponse> responses;
private final ContextualizedBidResponse winningBid;
private final ContextualizedBidResponse runnerUp;
private Collection<ContextualizedBidResponse> responses;
private float winningPrice;

public AuctionResult(
BidResponse winningBid,
float winningPrice,
IBidder winningBidder,
Collection<BidResponse> responses,
long runnerUp)
{
ContextualizedBidResponse winningBid,
Collection<ContextualizedBidResponse> responses,
ContextualizedBidResponse runnerUp,
float winningPrice){
this.winningBid = winningBid;
this.winningPrice = winningPrice;
this.winningBidder = winningBidder;
this.responses = responses;
this.runnerUp = runnerUp;
this.winningPrice = winningPrice;
}

public BidResponse getWinningBid() {
return winningBid;
public AuctionResult() {
this(null,null,null, 0f);
}

public float getWinningPrice() {
return winningPrice;
public ContextualizedBidResponse getWinningBid() {
return winningBid;
}

public Collection<BidResponse> getResponses() {
public Collection<ContextualizedBidResponse> getResponses() {
return responses;
}

public IBidder getWinningBidder() {
return winningBidder;
}

public long getRunnerUp() {
public ContextualizedBidResponse getRunnerUp() {
return runnerUp;
}
}
52 changes: 20 additions & 32 deletions bidding/src/main/java/com/bidtorrent/bidding/Auctioneer.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bidtorrent.bidding;

import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;
import com.bidtorrent.bidding.messages.Imp;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
Expand Down Expand Up @@ -36,18 +36,18 @@ public Future<AuctionResult> runAuction(final Auction auction) {
resultFuture = this.executor.submit(new Callable<AuctionResult>() {
@Override
public AuctionResult call() throws ExecutionException, InterruptedException {
Collection<ListenableFuture<BidResponse>> responseFutures;
ListenableFuture<List<BidResponse>> responses;
Collection<ListenableFuture<ContextualizedBidResponse>> responseFutures;
ListenableFuture<List<ContextualizedBidResponse>> responses;

responseFutures = pushResponseFutures(auction.getImpression(), auction.getBidders());
responses = getBidResponses(responseFutures);

return Futures.lazyTransform(responses, new Function<List<BidResponse>, AuctionResult>() {
return Futures.lazyTransform(responses, new Function<List<ContextualizedBidResponse>, AuctionResult>() {
@Nullable
@Override
public AuctionResult apply(List<BidResponse> input) {
public AuctionResult apply(List<ContextualizedBidResponse> input) {
input.removeAll(Collections.singleton(null));
return buildAuctionResult(input, auction.getImpression().bidfloor, auction.getBidders());
return buildAuctionResult(input, auction.getImpression().bidfloor);
}
}).get();
}
Expand All @@ -56,35 +56,33 @@ public AuctionResult apply(List<BidResponse> input) {
return resultFuture;
}

private static AuctionResult buildAuctionResult(Collection<BidResponse> responses, float floor, Collection<IBidder> bidders) {
private static AuctionResult buildAuctionResult(Collection<ContextualizedBidResponse> responses, float floor) {
float secondPrice;
long runnerUp = 0l;
BidResponse winningBid;
SortedSet<BidResponse> sortedResponses;
Iterator<BidResponse> iterator;
ContextualizedBidResponse runnerUp = null;
ContextualizedBidResponse winningBid;
SortedSet<ContextualizedBidResponse> sortedResponses;
Iterator<ContextualizedBidResponse> iterator;

sortedResponses = new TreeSet<>(responses);
iterator = sortedResponses.iterator();

if (!iterator.hasNext())
return new AuctionResult(null, 0, null, responses, 0);
return new AuctionResult();

winningBid = iterator.next();

if (iterator.hasNext()){
BidResponse second = iterator.next();
secondPrice = second.getPrice();
runnerUp = second.getBidderId();
}
else
ContextualizedBidResponse second = iterator.next();
runnerUp = second;
secondPrice = runnerUp.getBidResponse().getPrice();
} else
secondPrice = floor;

return new AuctionResult(winningBid, secondPrice, getBidderById(winningBid.getBidderId(), bidders), sortedResponses, runnerUp);
return new AuctionResult(winningBid, sortedResponses, runnerUp, secondPrice);
}

private static Collection<ListenableFuture<BidResponse>> pushResponseFutures(Imp impression, List<IBidder> bidders)
{
Collection<ListenableFuture<BidResponse>> responseFutures;
private static Collection<ListenableFuture<ContextualizedBidResponse>> pushResponseFutures(Imp impression, List<IBidder> bidders) {
Collection<ListenableFuture<ContextualizedBidResponse>> responseFutures;

responseFutures = new ArrayList<>(bidders.size());

Expand All @@ -101,17 +99,7 @@ public void processError(Exception e) {
}
};

private static ListenableFuture<List<BidResponse>> getBidResponses(Collection<ListenableFuture<BidResponse>> responseFutures)
{
private static ListenableFuture<List<ContextualizedBidResponse>> getBidResponses(Collection<ListenableFuture<ContextualizedBidResponse>> responseFutures) {
return Futures.successfulAsList(responseFutures);
}

private static IBidder getBidderById(long bidderId, Collection<IBidder> bidders) {
for (IBidder bidder : bidders) {
if (bidder.getId() == bidderId)
return bidder;
}

return null;
}
}
6 changes: 2 additions & 4 deletions bidding/src/main/java/com/bidtorrent/bidding/IBidder.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.bidtorrent.bidding;

import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;
import com.bidtorrent.bidding.messages.Imp;
import com.google.common.util.concurrent.ListenableFuture;

import java.io.Serializable;
import java.util.concurrent.Callable;

public interface IBidder extends Serializable{
ListenableFuture<BidResponse> bid(Imp impression, IErrorCallback errorCallback);

ListenableFuture<ContextualizedBidResponse> bid(Imp impression, IErrorCallback errorCallback);
long getId();
}
11 changes: 0 additions & 11 deletions bidding/src/main/java/com/bidtorrent/bidding/Notificator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
package com.bidtorrent.bidding;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down
10 changes: 7 additions & 3 deletions bidding/src/main/java/com/bidtorrent/bidding/bidders/ConstantBidder.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.bidtorrent.bidding.IBidder;
import com.bidtorrent.bidding.IErrorCallback;
import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;
import com.bidtorrent.bidding.messages.Imp;
import com.bidtorrent.bidding.messages.configuration.BidderConfiguration;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

Expand All @@ -18,12 +20,14 @@ public ConstantBidder(long id, BidResponse response) {
}

@Override
public ListenableFuture<BidResponse> bid(Imp impression, IErrorCallback errorCallback) {
return Futures.immediateFuture(response);
public ListenableFuture<ContextualizedBidResponse> bid(Imp impression, IErrorCallback errorCallback) {
BidderConfiguration bidderConfiguration = new BidderConfiguration();
bidderConfiguration.id = id;
return Futures.immediateFuture(new ContextualizedBidResponse(response, bidderConfiguration));
}

@Override
public long getId() {
return this.id;
}
}
}
34 changes: 22 additions & 12 deletions bidding/src/main/java/com/bidtorrent/bidding/bidders/HttpBidder.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,52 @@
import com.bidtorrent.bidding.messages.App;
import com.bidtorrent.bidding.messages.BidRequest;
import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;
import com.bidtorrent.bidding.messages.Device;
import com.bidtorrent.bidding.messages.Ext;
import com.bidtorrent.bidding.messages.Geo;
import com.bidtorrent.bidding.messages.Imp;
import com.bidtorrent.bidding.messages.Publisher;
import com.bidtorrent.bidding.messages.User;
import com.bidtorrent.bidding.messages.configuration.BidderConfiguration;
import com.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.Arrays;

import javax.annotation.Nullable;

public class HttpBidder implements IBidder {

private long id;
private final String name;
private final String bidUrl;
private final BidderConfiguration bidderConfiguration;
private final PooledHttpClient pooledHttpClient;

public HttpBidder(long id, String name, String bidUrl, PooledHttpClient pooledHttpClient)
{
this.id = id;
this.name = name;
this.bidUrl = bidUrl;
public HttpBidder(BidderConfiguration bidderConfiguration, PooledHttpClient pooledHttpClient) {
this.bidderConfiguration = bidderConfiguration;
this.pooledHttpClient = pooledHttpClient;
}

@Override
public ListenableFuture<BidResponse> bid(final Imp impression, IErrorCallback errorCallback) {
return pooledHttpClient.jsonPost(bidUrl, HttpBidder.createBidRequest(impression), BidResponse.class);
public ListenableFuture<ContextualizedBidResponse> bid(final Imp impression, IErrorCallback errorCallback) {
return Futures.transform(pooledHttpClient.jsonPost(this.bidderConfiguration.bid_ep, HttpBidder.createBidRequest(impression), BidResponse.class),
new Function<BidResponse, ContextualizedBidResponse>() {
@Nullable
@Override
public ContextualizedBidResponse apply(BidResponse input) {
if (input == null) return null;

return new ContextualizedBidResponse(input, bidderConfiguration);
}
});
}

public String getName() {
return name;
return this.bidderConfiguration.name;
}

public long getId() {
return id;
return this.bidderConfiguration.id;
}

private static BidRequest createBidRequest(Imp impression){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bidtorrent.bidding.messages;

import com.bidtorrent.bidding.messages.configuration.BidderConfiguration;

import java.io.Serializable;
import java.util.ArrayList;

Expand All @@ -10,6 +12,8 @@ public class BidResponse implements Comparable, Serializable {
public String cur;
public ArrayList<Seatbid> seatbid;

private BidderConfiguration bidderConfiguration;

public BidResponse(long bidderId, float bidPrice, int bidId, String domain, String creative, String notificationUrl) {
this.bidderId = bidderId;
Bid bid = new Bid("" + bidId, "", bidPrice, "", notificationUrl, domain, creative);
Expand All @@ -35,6 +39,14 @@ public void setBidderId(long bidderId)
this.bidderId = bidderId;
}

public void setBidderConfiguration(BidderConfiguration bidderConfiguration) {
this.bidderConfiguration = bidderConfiguration;
}

public BidderConfiguration getBidderConfiguration() {
return bidderConfiguration;
}

@Override
public int compareTo(Object o) {
BidResponse s = (BidResponse) o;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bidtorrent.bidding.messages;

import com.bidtorrent.bidding.messages.configuration.BidderConfiguration;

import java.io.Serializable;

public class ContextualizedBidResponse implements Comparable, Serializable{

private BidResponse bidResponse;
private BidderConfiguration bidderConfiguration;

public ContextualizedBidResponse(BidResponse bidResponse, BidderConfiguration bidderConfiguration) {
this.bidResponse = bidResponse;
this.bidderConfiguration = bidderConfiguration;
}

public BidderConfiguration getBidderConfiguration() {
return bidderConfiguration;
}

public BidResponse getBidResponse() {
return bidResponse;
}

@Override
public int compareTo(Object o) {
return this.bidResponse.compareTo(((ContextualizedBidResponse)o).bidResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.bidtorrent.bidding.messages.configuration;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.io.Serializable;

public class BidderConfiguration {
public class BidderConfiguration implements Serializable {
public Long id;
public String bid_ep;
public String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.bidtorrent.bidding.messages.configuration;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

/**
* Created by x.noelle on 30/06/2015.
*/
public class BidderConfigurationFilters {
public class BidderConfigurationFilters implements Serializable{
public Float sampling;
public Collection<String> pub_ctry;
public Boolean pub_ctry_wl;
Expand Down
Loading