Skip to content

Commit

Permalink
Add rpc communication and screenshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesblasco committed Jul 18, 2020
1 parent 4c8092d commit 61d5925
Show file tree
Hide file tree
Showing 47 changed files with 2,039 additions and 575 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Jaime Blasco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 21 additions & 1 deletion flutter-package/LICENSE
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
TODO: Add your license here.
MIT License

Copyright (c) 2020 Jaime Blasco

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
136 changes: 136 additions & 0 deletions flutter-package/bin/daemon_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import 'preview.dart';

class DaemonService extends MultiplePeer {
final int port;
DaemonService(this.port);

@override
registerMethods(Peer server) {
server.registerMethod('preview.getPort', () {
return port;
});

server.registerMethod('preview.setActiveFile', (Parameters params) {
final path = params['path'].asString;
changeActiveFile(path);
return true;
});

server.registerMethod('preview.restart', (Parameters params) {
_server.sendNotification('preview.restart');
return true;
});
}
}

class Stoutsink extends StreamSink<String> {
@override
void add(String event) {
stdout.writeln(event);
}

@override
void addError(Object error, [StackTrace stackTrace]) {
stdout.addError(error, stackTrace);
}

@override
Future addStream(Stream<String> stream) {
return stdout.addStream(stream.transform(Utf8Encoder()));
}

@override
Future close() {
return stdout.close();
}

@override
Future get done => stdout.close();
}

abstract class MultiplePeer {
Peer _server;
Map<WebSocketChannel, Peer> sockets = {};

//StreamChannel<String> _socket;

bool isListening = false;

addWebSocket(WebSocketChannel webSocket) {
assert(sockets[webSocket] == null);
final peer = Peer(webSocket.cast<String>(), strictProtocolChecks: false);
sockets[webSocket] = peer;
registerMethods(peer);
if (isListening) {
peer.listen();
}
}

removeWebSocket(WebSocketChannel webSocket) {
final peer = sockets[webSocket];
peer.close();
sockets[webSocket] = null;
}

Future run() async {
// assert(port != null && webSocket != null);
try {
/* _socket = webSocket ??
WebSocketChannel.connect(Uri.parse('ws://127.0.0.1:$port/ws'))
.cast<String>(); */
} catch (e, s) {
stderr.addError(e, s);
}

final channel = StreamChannel<String>(
stdin.transform(Utf8Decoder()).transform(LineSplitter()),
Stoutsink(),
);

_server = Peer(channel, strictProtocolChecks: false,
onUnhandledError: (error, stacktrace) {
stdout.write('Error $error');
});

registerMethods(_server);
}

Future listen() async {
isListening = true;
for (final socket in sockets.entries) {
socket.value.listen().then(
(value) {
removeWebSocket(socket.key);
},
);
}
return await _server.listen();
}

Future close() async {
for (final peer in sockets.values) {
await peer.close();
}
return _server.close();
}

/* void registerMethod(String name, Function callback) =>
_server.registerMethod(name, callback); */

void sendNotification(String method, [dynamic parameters]) {
_server.sendNotification(method, parameters);
sockets.values.forEach((e) {
e.sendNotification(method, parameters);
});
}

registerMethods(Peer server);
}
56 changes: 56 additions & 0 deletions flutter-package/bin/old/asset_server.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:io';
import 'package:path/path.dart' as path;

final localAddress = InternetAddress('127.0.0.1');

class AssetServer {
HttpServer _server;

Future<bool> runServer(int port) async {
try {
_server = await HttpServer.bind(localAddress, port);
return true;
} catch (e, s) {
stderr.addError(e, s);
return false;
}
}

Future<void> listen() async {
await for (final request in _server) {
try {
if (request.uri.path.startsWith('/asset/')) {
final File file =
new File(request.uri.path.replaceFirst('/asset/', ''));

if (file.existsSync()) {
final raw = await file.readAsBytes();

request.response.headers
.set('Content-Type', 'image/${path.extension(file.path)}');
request.response.headers.set('Content-Length', raw.length);
request.response.add(raw);
await request.response.close();
} else {
request.response.statusCode = HttpStatus.notFound;
request.response.write('Not found');
await request.response.close();
}
} else {
request.response.statusCode = HttpStatus.notFound;
request.response.write('Not found');
await request.response.close();
}
} catch (e, s) {
stderr.addError(e, s);
request.response.statusCode = HttpStatus.internalServerError;
request.response.write('Internal Error');
await request.response.close();
}
}
}

Future<void> close() async {
await _server.close();
}
}
Loading

0 comments on commit 61d5925

Please sign in to comment.