Skip to content

Commit

Permalink
Chapter 8 added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo-Balbaert committed Jul 1, 2014
1 parent 484405e commit fdaf207
Show file tree
Hide file tree
Showing 427 changed files with 61,268 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Chapter 7/secure_server/bin/pkcert/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This is a certificate database used by Dart for testing purposes.

It is created as a certificate database by NSS (Network Security Services),
a library from Mozilla, using the certutil tool. It uses a cert9.db file,
rather than a cert8.db file, so the database directory must be specified with
"sql:" in front of the directory path, or the environment variable
NSS_DEFAULT_DB_TYPE must be set to "sql".

The password for the key database is "dartdart".

The database contains a root certificate from Equifax, used to verify the
client https connection to www.google.dk. It contains a self-signed
certificate for a local certificate authority myauthority_cert, and a
server certificate for localhost called localhost_cert, signed by
myauthority_cert. It contains the key for localhost_cert, but
not the key for myauthority_cert.
Binary file added Chapter 7/secure_server/bin/pkcert/cert9.db
Binary file not shown.
Binary file added Chapter 7/secure_server/bin/pkcert/key4.db
Binary file not shown.
22 changes: 22 additions & 0 deletions Chapter 7/secure_server/bin/secure_client.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:io';

InternetAddress HOST = InternetAddress.LOOPBACK_IP_V6;
const int PORT = 4777;
SecureSocket socket;

void main() {
SecureSocket.connect(HOST, PORT, onBadCertificate: (X509Certificate c) {
print("Certificate WARNING: ${c.issuer}:${c.subject}");
return true;
}).then(handleSecureSocket);
}

handleSecureSocket(SecureSocket ss) {
// send to server:
ss.write("From client: can you encrypt me server?");
// read from server:
ss.listen((List data) {
String msg = new String.fromCharCodes(data).trim();
print(msg);
});
}
19 changes: 19 additions & 0 deletions Chapter 7/secure_server/bin/secure_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:io';

InternetAddress HOST = InternetAddress.LOOPBACK_IP_V6;
const int PORT = 4777;

main() {
var testcertDb = Platform.script.resolve('pkcert').toFilePath();
SecureSocket.initialize(database: testcertDb, password: 'dartdart');

HttpServer.bindSecure(HOST, PORT, certificateName: 'localhost_cert').then((server) {
print('Secure Server listening');
server.listen((HttpRequest req) {
print('Request for ${req.uri.path}');
var resp = req.response;
resp.write("Don't worry: I encrypt your messages!");
resp.close();
});
});
}
3 changes: 3 additions & 0 deletions Chapter 7/secure_server/pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by pub
# See http://pub.dartlang.org/doc/glossary.html#lockfile
packages: {}
4 changes: 4 additions & 0 deletions Chapter 7/secure_server/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: secure_server
description: A sample command-line application
#dev_dependencies:
# unittest: any
32 changes: 32 additions & 0 deletions Chapter 7/stock_viewer/packages/browser/dart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

(function() {
// Bootstrap support for Dart scripts on the page as this script.
if (navigator.userAgent.indexOf('(Dart)') === -1) {
// TODO:
// - Support in-browser compilation.
// - Handle inline Dart scripts.

// Fall back to compiled JS. Run through all the scripts and
// replace them if they have a type that indicate that they source
// in Dart code (type="application/dart").
var scripts = document.getElementsByTagName("script");
var length = scripts.length;
for (var i = 0; i < length; ++i) {
if (scripts[i].type == "application/dart") {
// Remap foo.dart to foo.dart.js.
if (scripts[i].src && scripts[i].src != '') {
var script = document.createElement('script');
script.src = scripts[i].src.replace(/\.dart(?=\?|$)/, '.dart.js');
var parent = scripts[i].parentNode;
// TODO(vsm): Find a solution for issue 8455 that works with more
// than one script.
document.currentScript = script;
parent.replaceChild(script, scripts[i]);
}
}
}
}
})();
10 changes: 10 additions & 0 deletions Chapter 7/stock_viewer/packages/browser/interop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Type for remote proxies to Dart objects with dart2js.
// WARNING: do not call this constructor or rely on it being
// in the global namespace, as it may be removed.
function DartObject(o) {
this.o = o;
}
7 changes: 7 additions & 0 deletions Chapter 7/stock_viewer/pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by pub
# See http://pub.dartlang.org/doc/glossary.html#lockfile
packages:
browser:
description: browser
source: hosted
version: "0.9.1"
6 changes: 6 additions & 0 deletions Chapter 7/stock_viewer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: AdnBlogViewer
author: Philippe Leefsma <philippe.leefsma@autodesk.com>
homepage: http://adndevblog.typepad.com/cloud_and_mobile/philippe-leefsma.html
description: A sample web service application, by Philippe Leefsma
dependencies:
browser: any
17 changes: 17 additions & 0 deletions Chapter 7/stock_viewer/web/stock_viewer.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

body {
background-color: #F8F8F8;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
font-weight: normal;
line-height: 1.2em;
margin: 15px;
}

h1, p {
color: #333;
}

.blogLink {
font-size: 150px;
}
48 changes: 48 additions & 0 deletions Chapter 7/stock_viewer/web/stock_viewer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:html';
import 'dart:convert';

main() {
LoadData();
}
void LoadData() {
// var url = 'http://query.yahooapis.com/v1/public/yql?q=';
// var select = 'select * from yahoo.finance.quotes where symbol in ("GOOG")';
// var format = '&env=http://datatables.org/Falltables.env&format=json';
// var request = url + Uri.encodeQueryComponent(select + format);
var stock = "GOOG";
var request = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20"
"where%20symbol%20in%20(%22$stock%22)%0A%09%09"
"&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json";
// call the web server asynchronously
var result = HttpRequest.getString(request).then(OnDataLoaded);
}

// Web service response callback
void OnDataLoaded(String response) {
String json = response.substring(response.indexOf("symbol") - 2, response.length - 3);
Map data = JSON.decode(json);
var table = CreateTable();
var props = data.keys;
props.forEach((prop) => ProcessStockEntry(prop, data, table));
document.body.nodes.add(table);
}

TableElement CreateTable() {
TableElement table = new TableElement();
var tBody = table.createTBody();
return table;
}

void ProcessStockEntry(String prop, Map data, TableElement table) {
String value = data["$prop"];
// Add new row to our table
var row = table.insertRow(-1);
// Add new cell for the property
var propCell = row.insertCell(0);
String prophtml = '$prop:';
propCell.setInnerHtml(prophtml);
// Add new cell for the value
var valueCell = row.insertCell(1);
String valuehtml = '$value';
valueCell.setInnerHtml(valuehtml);
}
18 changes: 18 additions & 0 deletions Chapter 7/stock_viewer/web/stock_viewer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Stock Data Viewer</title>
<link rel="stylesheet" href="stock_viewer.css">
</head>

<body>
<p><b>View latest stock data from Google</b></p>

<script type="application/dart" src="stock_viewer.dart"></script>
<script src="packages/browser/dart.js"></script>
<script src="packages/browser/interop.js"></script>

</body>
</html>
68 changes: 68 additions & 0 deletions Chapter 8/future_errors/bin/future_errors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:async';

void main() {
// Examples working with futures:
// 1) chaining futures:
firstStep()
.then(secondStep)
.then(thirdStep)
.then(fourthStep)
.catchError(handleError);
// 2) concurrent Futures:
List futs = [firstStep, secondStep, thirdStep, fourthStep];
Future.wait(futs)
.then((List<T> val) => processValues(val))
.catchError(handleError);
// 3) catching specific errors:
firstStep()
.then(secondStep)
.catchError(handleArgumentError,
test: (e) => e is ArgumentError)
.catchError(handleFormatException,
test: (e) => e is FormatException)
.catchError(handleRangeError,
test: (e) => e is RangeError)
.catchError(handleException, test: (e) => e is Exception);
// 4) whenComplete:
firstStep()
.then(secondStep)
.catchError(handleError)
.whenComplete(cleanUp);
// 5) Handling synchronous and asynchronous errors:
var data;
mixedFunction(data).catchError(handleError);
}

firstStep() {}
T secondStep() {}
T thirdStep() {}
T fourthStep() {}
T finalStep() {}
handleError() {}
processValues(val) {}
handleArgumentError() {}
handleFormatException() {}
handleRangeError() {}
handleException() {}
cleanUp() {}
processResult() {}

class T {}

// wrong version:
//mixedFunction(data) {
// var var1 = synFunc(data); // Could throw error.
// return var1.asynFunc().then(processResult); // Could throw error.
//}

// correct version:
mixedFunction(data) {
return new Future.sync(() {
var var1 = synFunc(data); // Could throw error.
return var1.asynFunc().then(processResult); // Could throw error.
});
}

//
synFunc(data) {}
asynFunc() {}
21 changes: 21 additions & 0 deletions Chapter 8/future_errors/bin/wait_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:async';

main() {
Future<int> a = new Future(() {
print('a');
return 1;
});
Future<int> b = new Future.error('Error occurred in b!');
Future<int> c = new Future(() {
print('c');
return 3;
});
// Future<int> d = new Future(() {
// print('d');
// return 4;
// });
Future<int> d = new Future(() { throw('Error occurred in d!'); });

Future.wait([a, b, c, d]).then((List<int> values) => print(values)).catchError(print);
print('happy end');
}
3 changes: 3 additions & 0 deletions Chapter 8/future_errors/pubspec.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by pub
# See http://pub.dartlang.org/doc/glossary.html#lockfile
packages: {}
4 changes: 4 additions & 0 deletions Chapter 8/future_errors/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: future_errors
description: A sample command-line application
#dev_dependencies:
# unittest: any
14 changes: 14 additions & 0 deletions Chapter 8/gameloop/game.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

body {
background-color: #f6f6f6;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
font-weight: normal;
line-height: 1.2em;
margin: 15px;
}

h1, p, label {
color: #67707a;
}

18 changes: 18 additions & 0 deletions Chapter 8/gameloop/game.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
library memory;

import 'dart:async';
import 'dart:html';
import 'package:boarding/boarding_model.dart';
import 'package:boarding/boarding.dart';

part 'model/memory.dart';
part 'view/board.dart';

main() {
new Board(new Memory(4), querySelector('#canvas')).draw();
querySelector('#play').onClick.listen(playAgain);
}

playAgain(Event e) {
window.location.reload();
}
22 changes: 22 additions & 0 deletions Chapter 8/gameloop/game.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>Memory game</title>
<link rel="stylesheet" href="game.css">
</head>
<body>
<h1>Memory</h1>

<section>
<canvas id="canvas" width="320" height="320">
Canvas is not supported in your browser.
</canvas>
<button id="play">Play again</button>
</section>

<script type="application/dart" src="game.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions Chapter 8/gameloop/lib/boarding.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library boarding;

import 'dart:html';
import 'dart:math';

import 'package:boarding/boarding_model.dart';

part 'view/shape.dart';
part 'view/surface.dart';

9 changes: 9 additions & 0 deletions Chapter 8/gameloop/lib/boarding_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library boarding_model;

import 'dart:math';

part 'model/cell.dart';
part 'model/grid.dart';

part 'util/color.dart';
part 'util/random.dart';
Loading

0 comments on commit fdaf207

Please sign in to comment.