Skip to content

Commit

Permalink
Send through bidders configuration so that it's available everywhere …
Browse files Browse the repository at this point in the history
…and reenables notifications
  • Loading branch information
mwaisgold committed Aug 27, 2015
1 parent a6acd6c commit 7ebfcbb
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 137 deletions.
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;
}
}
50 changes: 20 additions & 30 deletions bidding/src/main/java/com/bidtorrent/bidding/Auctioneer.java
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.google.common.base.Function;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand Down Expand Up @@ -35,18 +35,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.getOpportunity(), 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.getFloor(), auction.getBidders());
return buildAuctionResult(input, auction.getFloor());
}
}).get();
}
Expand All @@ -55,36 +55,35 @@ 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(
private static Collection<ListenableFuture<ContextualizedBidResponse>> pushResponseFutures(
final BidOpportunity opportunity, final List<IBidder> bidders)
{
Collection<ListenableFuture<BidResponse>> responseFutures;
Collection<ListenableFuture<ContextualizedBidResponse>> responseFutures;

responseFutures = new ArrayList<>(bidders.size());
for (final IBidder bidder: bidders) {
Expand All @@ -100,17 +99,8 @@ public void processError(Exception e) {
return responseFutures;
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bidtorrent.bidding;

import com.bidtorrent.bidding.messages.BidResponse;
import com.bidtorrent.bidding.messages.ContextualizedBidResponse;
import com.bidtorrent.bidding.messages.configuration.BidderConfiguration;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
Expand All @@ -17,8 +19,10 @@ public ConstantBidder(long id, BidResponse response) {
}

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

@Override
Expand Down
56 changes: 22 additions & 34 deletions bidding/src/main/java/com/bidtorrent/bidding/HttpBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,31 @@
import com.bidtorrent.bidding.messages.Banner;
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.google.common.io.ByteStreams;
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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.http.client.methods.HttpPost;
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.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URI;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.Callable;

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, IResponseConverter<String> responseConverter, int timeout, PooledHttpClient pooledHttpClient)
{
this.id = id;
this.name = name;
this.bidUrl = bidUrl;
public HttpBidder(BidderConfiguration bidderConfiguration, PooledHttpClient pooledHttpClient) {
this.bidderConfiguration = bidderConfiguration;
this.pooledHttpClient = pooledHttpClient;
}

Expand All @@ -70,7 +50,6 @@ public static String getLocalIpAddress()
return null;
}


private static BidRequest createBidRequest(BidOpportunity bidOpportunity){
return new BidRequest(
new User("MyID", "MyID"),
Expand All @@ -90,15 +69,24 @@ private static BidRequest createBidRequest(BidOpportunity bidOpportunity){
}

@Override
public ListenableFuture<BidResponse> bid(final BidOpportunity opportunity, IErrorCallback errorCallback) {
return pooledHttpClient.jsonPost(bidUrl, createBidRequest(opportunity), BidResponse.class);
public ListenableFuture<ContextualizedBidResponse> bid(final BidOpportunity opportunity, IErrorCallback errorCallback) {
return Futures.transform(pooledHttpClient.jsonPost(this.bidderConfiguration.bid_ep, createBidRequest(opportunity), 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;
}
}
3 changes: 2 additions & 1 deletion bidding/src/main/java/com/bidtorrent/bidding/IBidder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.bidtorrent.bidding;

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

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

public interface IBidder extends Serializable{
ListenableFuture<BidResponse> bid(BidOpportunity opportunity, IErrorCallback errorCallback);
ListenableFuture<ContextualizedBidResponse> bid(BidOpportunity opportunity, 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
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
Loading

0 comments on commit 7ebfcbb

Please sign in to comment.