Skip to content

Commit 4080ac6

Browse files
add dsp sim server
1 parent 0583937 commit 4080ac6

21 files changed

+727
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DSP_SIM_MODEL.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.atg.openssp.dspSim.model.client;
2+
3+
public class ServerCommand {
4+
private ServerCommandType type;
5+
private String id;
6+
private float price;
7+
8+
public void setType(ServerCommandType type) {
9+
this.type = type;
10+
}
11+
12+
public ServerCommandType getType() {
13+
return type;
14+
}
15+
16+
public void setId(String id) {
17+
this.id = id;
18+
}
19+
20+
public String getId() {
21+
return id;
22+
}
23+
24+
public void setPrice(float price) {
25+
this.price = price;
26+
}
27+
28+
public float getPrice() {
29+
return price;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.atg.openssp.dspSim.model.client;
2+
3+
public enum ServerCommandType {
4+
LIST, UPDATE, ADD
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.atg.openssp.dspSim.model.client;
2+
3+
import com.atg.openssp.dspSim.model.dsp.SimBidder;
4+
5+
import java.util.List;
6+
7+
public class ServerResponse {
8+
private ServerResponseStatus status;
9+
private String reason="";
10+
private List<SimBidder> bidders;
11+
12+
public void setStatus(ServerResponseStatus status) {
13+
this.status = status;
14+
}
15+
16+
public ServerResponseStatus getStatus() {
17+
return status;
18+
}
19+
20+
public void setReason(String reason) {
21+
this.reason = reason;
22+
}
23+
24+
public String getReason() {
25+
return reason;
26+
}
27+
28+
public void setBidders(List<SimBidder> bidders) {
29+
this.bidders = bidders;
30+
}
31+
32+
public List<SimBidder> getBidders() {
33+
return bidders;
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.atg.openssp.dspSim.model.client;
2+
3+
public enum ServerResponseStatus {
4+
FAILURE, SUCCESS
5+
}

dsp-sim/pom.xml

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.atg.openssp</groupId>
7+
<artifactId>open-ssp-dsp-sim</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
<name>open-ssp-dsp-sim</name>
10+
<url>http://maven.apache.org</url>
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.google.code.gson</groupId>
24+
<artifactId>gson</artifactId>
25+
<version>2.6.2</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.atg.openssp</groupId>
29+
<artifactId>open-ssp-common</artifactId>
30+
<version>0.3-SNAPSHOT</version>
31+
</dependency>
32+
33+
</dependencies>
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-compiler-plugin</artifactId>
39+
<version>3.5.1</version>
40+
<configuration>
41+
<source>1.8</source>
42+
<target>1.8</target>
43+
</configuration>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-dependency-plugin</artifactId>
48+
<executions>
49+
<execution>
50+
<id>copy-dependencies</id>
51+
<phase>prepare-package</phase>
52+
<goals>
53+
<goal>copy-dependencies</goal>
54+
</goals>
55+
<configuration>
56+
<outputDirectory>${project.build.directory}/lib</outputDirectory>
57+
<overWriteReleases>false</overWriteReleases>
58+
<overWriteSnapshots>false</overWriteSnapshots>
59+
<overWriteIfNewer>true</overWriteIfNewer>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-jar-plugin</artifactId>
67+
<version>3.0.2</version>
68+
<configuration>
69+
<archive>
70+
<manifest>
71+
<addClasspath>true</addClasspath>
72+
<addDefaultImplementationEntries />
73+
<classpathPrefix>lib/</classpathPrefix>
74+
<mainClass>com.atg.openssp.dspSim.DspSimClient</mainClass>
75+
</manifest>
76+
</archive>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
</project>

dsp-sim/run_sim.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
java -jar target/open-ssp-dsp-sim-0.1-SNAPSHOT.jar
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.atg.openssp.dspSim;
2+
3+
import com.atg.openssp.dspSim.model.ad.AdModel;
4+
import com.google.gson.Gson;
5+
import com.sun.net.httpserver.HttpExchange;
6+
import com.sun.net.httpserver.HttpHandler;
7+
import com.atg.openssp.dspSim.channel.adserving.AdservingCampaignProvider;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.*;
12+
13+
public class AdServerHandler implements HttpHandler {
14+
private static final Logger log = LoggerFactory.getLogger(AdServerHandler.class);
15+
private final AdModel model;
16+
17+
public AdServerHandler(AdModel model) {
18+
this.model = model;
19+
}
20+
21+
@Override
22+
public void handle(HttpExchange httpExchange) throws IOException {
23+
24+
StringBuilder input = new StringBuilder();
25+
try {
26+
BufferedReader is = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody()));
27+
String line;
28+
while((line = is.readLine()) != null) {
29+
input.append(line+"\n");
30+
}
31+
} catch (Exception ex) {
32+
log.error(ex.getMessage(), ex);
33+
}
34+
log.info("AD-->"+new Gson().toJson(input));
35+
36+
AdservingCampaignProvider p = new AdservingCampaignProvider();
37+
p.setIsValid(true);
38+
p.setPrice(40f);
39+
p.setPriceEur(30f);
40+
41+
String result = new Gson().toJson(p);
42+
log.info("<--"+result);
43+
httpExchange.sendResponseHeaders(200, result.length());
44+
OutputStream os = httpExchange.getResponseBody();
45+
os.write(result.getBytes());
46+
os.close();
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.atg.openssp.dspSim;
2+
3+
import com.atg.openssp.dspSim.model.ModelException;
4+
import com.atg.openssp.dspSim.model.client.ClientCommand;
5+
import com.atg.openssp.dspSim.model.client.ClientCommandType;
6+
import com.atg.openssp.dspSim.model.client.ClientResponse;
7+
import com.atg.openssp.dspSim.model.client.ClientResponseStatus;
8+
import com.atg.openssp.dspSim.model.dsp.DspModel;
9+
import com.atg.openssp.dspSim.model.dsp.SimBidder;
10+
import com.google.gson.Gson;
11+
import com.sun.net.httpserver.HttpExchange;
12+
import com.sun.net.httpserver.HttpHandler;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import java.io.*;
17+
import java.util.UUID;
18+
19+
public class ClientHandler implements HttpHandler {
20+
private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);
21+
private final DspModel model;
22+
23+
public ClientHandler(DspModel model) {
24+
this.model = model;
25+
}
26+
27+
@Override
28+
public void handle(HttpExchange httpExchange) throws IOException {
29+
ClientCommand cc = new Gson().fromJson(new InputStreamReader(httpExchange.getRequestBody()), ClientCommand.class);
30+
log.info("CC-->"+new Gson().toJson(cc));
31+
ClientResponse cr = new ClientResponse();
32+
if (cc == null) {
33+
cr.setStatus(ClientResponseStatus.FAILURE);
34+
cr.setReason("Command not found");
35+
cr.setBidders(model.getBidders());
36+
} else if (cc.getType() == ClientCommandType.LIST) {
37+
cr.setStatus(ClientResponseStatus.SUCCESS);
38+
cr.setBidders(model.getBidders());
39+
} else if (cc.getType() == ClientCommandType.ADD) {
40+
SimBidder sb = model.lookupBidder(cc.getId());
41+
if (sb == null) {
42+
sb = new SimBidder(cc.getId());
43+
sb.setPrice(cc.getPrice());
44+
model.add(sb);
45+
try {
46+
model.saveModel();
47+
cr.setStatus(ClientResponseStatus.SUCCESS);
48+
} catch (ModelException e) {
49+
log.error(e.getMessage(), e);
50+
cr.setStatus(ClientResponseStatus.FAILURE);
51+
cr.setReason("add save failed: "+e.getMessage());
52+
}
53+
} else {
54+
cr.setStatus(ClientResponseStatus.FAILURE);
55+
cr.setReason("SimBidder already exists");
56+
}
57+
cr.setBidders(model.getBidders());
58+
} else if (cc.getType() == ClientCommandType.UPDATE) {
59+
SimBidder sb = model.lookupBidder(cc.getId());
60+
if (sb == null) {
61+
cr.setStatus(ClientResponseStatus.FAILURE);
62+
cr.setReason("SimBidder not found");
63+
} else {
64+
sb.setPrice(cc.getPrice());
65+
cr.setStatus(ClientResponseStatus.SUCCESS);
66+
try {
67+
model.saveModel();
68+
} catch (ModelException e) {
69+
log.error(e.getMessage(), e);
70+
cr.setStatus(ClientResponseStatus.FAILURE);
71+
cr.setReason("update save failed: "+e.getMessage());
72+
}
73+
}
74+
cr.setBidders(model.getBidders());
75+
}
76+
String result = new Gson().toJson(cr);
77+
log.info("CR<--"+result);
78+
httpExchange.sendResponseHeaders(200, result.length());
79+
OutputStream os = httpExchange.getResponseBody();
80+
os.write(result.getBytes());
81+
os.close();
82+
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.atg.openssp.dspSim;
2+
3+
import com.atg.openssp.dspSim.model.dsp.DspModel;
4+
import com.google.gson.Gson;
5+
import com.sun.net.httpserver.HttpExchange;
6+
import com.sun.net.httpserver.HttpHandler;
7+
import openrtb.bidrequest.model.BidRequest;
8+
import openrtb.bidresponse.model.BidResponse;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.io.IOException;
13+
import java.io.InputStreamReader;
14+
import java.io.OutputStream;
15+
16+
public class DspHandler implements HttpHandler {
17+
private static final Logger log = LoggerFactory.getLogger(DspHandler.class);
18+
private final DspModel model;
19+
20+
public DspHandler(DspModel model) {
21+
this.model = model;
22+
}
23+
24+
@Override
25+
public void handle(HttpExchange httpExchange) throws IOException {
26+
// method: post
27+
// writeFinished: false
28+
// uri: /dsp-sim/DemandService
29+
// reqContentLen: 724
30+
31+
// _REQ_HEADERS_
32+
// Accept-encoding: gzip,deflate
33+
// Connection: Keep-Alive
34+
// Host: localhost:8082
35+
// User-agent: Apache-HttpClient/4.5.2 (Java/1.8.0_161)
36+
// Content-type: application/json; charset=UTF-8
37+
// Contenttype: application/json
38+
// X-openrtb-version: 2.2
39+
// Content-length: 724
40+
41+
BidRequest brq = new Gson().fromJson(new InputStreamReader(httpExchange.getRequestBody()), BidRequest.class);
42+
log.info("-->"+new Gson().toJson(brq));
43+
44+
BidResponse brsp = model.createBidResponse(brq);
45+
46+
if (brsp.getSeatbid().size() > 0 || true) {
47+
String result = new Gson().toJson(brsp);
48+
log.info("<--"+result);
49+
httpExchange.sendResponseHeaders(200, result.length());
50+
OutputStream os = httpExchange.getResponseBody();
51+
os.write(result.getBytes());
52+
os.close();
53+
} else {
54+
httpExchange.sendResponseHeaders(201, 0);
55+
}
56+
57+
58+
}
59+
60+
}

0 commit comments

Comments
 (0)