-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eaf6bb
commit 0162a60
Showing
25 changed files
with
286 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/headfirst/designpatterns/combined/djview/DJViewHttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package headfirst.designpatterns.combined.djview; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import com.sun.net.httpserver.*; | ||
|
||
// Controller for the DJ Web View | ||
// Takes actions from the view and translates them to actions on the model | ||
// View has no interaction to the model | ||
// Using the exact same model, BeatModel, as we used with the DJView and BeatContoller | ||
public class DJViewHttpHandler implements HttpHandler { | ||
BeatModel beatModel; | ||
Map<String, String> queryPairs; | ||
|
||
public DJViewHttpHandler(BeatModel beatModel) { | ||
this.beatModel = beatModel; | ||
} | ||
public void handle(HttpExchange httpExchange) throws IOException { | ||
String uri = httpExchange.getRequestURI().toString(); | ||
System.out.println("URI: " + uri); | ||
String[] params = uri.split("\\?"); | ||
String queryStrings = ""; | ||
if (params.length > 1) { | ||
queryStrings = params[1]; | ||
} | ||
|
||
boolean noAction = true; | ||
try { | ||
queryPairs = this.splitQuery(queryStrings); | ||
noAction = false; | ||
} catch (Exception e) { | ||
System.out.println("Error splitting query: " + e.getMessage()); | ||
} | ||
System.out.println("Query pairs: " + queryPairs); | ||
|
||
if (!noAction) { | ||
String bpm = this.getParameter("bpm"); | ||
if (bpm == null) { | ||
bpm = beatModel.getBPM() + ""; | ||
} | ||
String set = this.getParameter("set"); | ||
if (set != null) { | ||
int bpmNumber = 90; | ||
bpmNumber = Integer.parseInt(bpm); | ||
beatModel.setBPM(bpmNumber); | ||
} | ||
String decrease = this.getParameter("decrease"); | ||
if (decrease != null) { | ||
beatModel.setBPM(beatModel.getBPM() - 1); | ||
} | ||
String increase = this.getParameter("increase"); | ||
if (increase != null) { | ||
beatModel.setBPM(beatModel.getBPM() + 1); | ||
} | ||
String on = this.getParameter("on"); | ||
if (on != null) { | ||
beatModel.on(); | ||
} | ||
String off = this.getParameter("off"); | ||
if (off != null) { | ||
beatModel.off(); | ||
} | ||
} | ||
|
||
// allow access from web server other than 8080 | ||
Headers headers = httpExchange.getResponseHeaders(); | ||
headers.add("Access-Control-Allow-Origin","*"); | ||
|
||
OutputStream outputStream = httpExchange.getResponseBody(); | ||
StringBuilder responseStringBuilder = new StringBuilder(); | ||
responseStringBuilder.append(beatModel.getBPM()); | ||
System.out.println("BPM: " + beatModel.getBPM()); | ||
|
||
String responseString = responseStringBuilder.toString(); | ||
httpExchange.sendResponseHeaders(200, responseString.length()); | ||
outputStream.write(responseString.getBytes()); | ||
outputStream.flush(); | ||
outputStream.close(); | ||
} | ||
|
||
Map<String, String> splitQuery(String query) throws UnsupportedEncodingException { | ||
Map<String, String> query_pairs = new HashMap<String, String>(); | ||
String[] pairs = query.split("&"); | ||
for (String pair : pairs) { | ||
int idx = pair.indexOf("="); | ||
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8")); | ||
} | ||
return query_pairs; | ||
} | ||
|
||
// bpm, set, decrease, increase, on, off | ||
String getParameter(String param) { | ||
return queryPairs.get(param); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/headfirst/designpatterns/combined/djview/DJViewHttpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package headfirst.designpatterns.combined.djview; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
import java.net.InetSocketAddress; | ||
|
||
public class DJViewHttpServer { | ||
|
||
public static void main(String[] args) { | ||
|
||
try { | ||
System.out.println("DJView Http Server Running"); | ||
|
||
BeatModel beatModel = new BeatModel(); | ||
beatModel.initialize(); | ||
// create a server on port 8080, with a backlog queue allowed of size 0 (ie, no queue) | ||
// most people will already have a web server running on 80, so we're using 8080 | ||
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 8080), 0); | ||
server.createContext("/djview", new DJViewHttpHandler(beatModel)); | ||
server.start(); | ||
System.out.println("DJView Server is running at http://localhost:8080/djview"); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/headfirst/designpatterns/combining/observer/Observable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/headfirst/designpatterns/command/diner/BurgerAndFriesOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package headfirst.designpatterns.command.diner; | ||
|
||
public class BurgerAndFriesOrder implements Order { | ||
Cook cook; | ||
public BurgerAndFriesOrder(Cook cook) { | ||
this.cook = cook; | ||
} | ||
public void orderUp() { | ||
cook.makeBurger(); | ||
cook.makeFries(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.