Skip to content

Commit

Permalink
Add a grpc-web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
x746e committed Mar 5, 2021
1 parent 2522d4d commit f0531c4
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 13 deletions.
20 changes: 13 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
all: tmux_pb2.py tmux_pb2_grpc.py chrome_extension/chrome_pb.js chrome_extension/chrome_grpc_web_pb.js

tmux_pb2.py: tmux.proto
python -m grpc_tools.protoc -I. --python_out=. tmux.proto

tmux_pb2_grpc.py: tmux.proto
python -m grpc_tools.protoc -I. --grpc_python_out=. tmux.proto
all: chrome_pb2.py chrome_pb2_grpc.py chrome_extension/chrome_pb.js chrome_extension/chrome_grpc_web_pb.js tmux_pb2.py tmux_pb2_grpc.py

test:
python -m unittest discover -p '*test.py'

chrome_pb2.py: chrome.proto
python -m grpc_tools.protoc -I. --python_out=. chrome.proto

chrome_pb2_grpc.py: chrome.proto
python -m grpc_tools.protoc -I. --grpc_python_out=. chrome.proto

chrome_extension/chrome_pb.js: chrome.proto
protoc -I=. chrome.proto --js_out=import_style=commonjs:chrome_extension --grpc-web_out=import_style=commonjs,mode=grpcweb:chrome_extension

chrome_extension/chrome_grpc_web_pb.js: chrome.proto
protoc -I=. chrome.proto --js_out=import_style=commonjs:chrome_extension --grpc-web_out=import_style=commonjs,mode=grpcweb:chrome_extension

tmux_pb2.py: tmux.proto
python -m grpc_tools.protoc -I. --python_out=. tmux.proto

tmux_pb2_grpc.py: tmux.proto
python -m grpc_tools.protoc -I. --grpc_python_out=. tmux.proto
25 changes: 25 additions & 0 deletions chrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
from wsgiref.simple_server import make_server
import sonora.wsgi

from google.protobuf import empty_pb2

import chrome_pb2_grpc


class Chrome(chrome_pb2_grpc.ChromeServicer):

def __init__(self, span_tracker):
self._span_tracker = span_tracker

def session_changed(self, request, context):
print(request)
return empty_pb2.Empty()


def serve(servicer):
grpc_wsgi_app = sonora.wsgi.grpcWSGI(None)

with make_server("", 3142, grpc_wsgi_app) as httpd:
chrome_pb2_grpc.add_ChromeServicer_to_server(servicer, grpc_wsgi_app)
httpd.serve_forever()
4 changes: 1 addition & 3 deletions chrome_extension/client_chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ chrome.runtime.onInstalled.addListener(function() {
const {SessionChangedRequest} = require('./chrome_pb.js');
const {ChromeClient} = require('./chrome_grpc_web_pb.js');

let client = new ChromeClient('http://localhost:3141', null, null);
let client = new ChromeClient('http://localhost:3142', null, null);

let request = new SessionChangedRequest();
request.setSessionName('TestSession');
Expand All @@ -13,8 +13,6 @@ chrome.runtime.onInstalled.addListener(function() {
if (err) {
console.log(`Unexpected error for sessionChange: code = ${err.code}` +
`, message = "${err.message}"`);
} else {
console.log(response.getMessage());
}
});
});
2 changes: 1 addition & 1 deletion chrome_extension/main.js

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions chrome_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions chrome_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

import chrome_pb2 as chrome__pb2
from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2


class ChromeStub(object):
"""Missing associated documentation comment in .proto file."""

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.session_changed = channel.unary_unary(
'/trackd.Chrome/session_changed',
request_serializer=chrome__pb2.SessionChangedRequest.SerializeToString,
response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
)


class ChromeServicer(object):
"""Missing associated documentation comment in .proto file."""

def session_changed(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_ChromeServicer_to_server(servicer, server):
rpc_method_handlers = {
'session_changed': grpc.unary_unary_rpc_method_handler(
servicer.session_changed,
request_deserializer=chrome__pb2.SessionChangedRequest.FromString,
response_serializer=google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'trackd.Chrome', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))


# This class is part of an EXPERIMENTAL API.
class Chrome(object):
"""Missing associated documentation comment in .proto file."""

@staticmethod
def session_changed(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/trackd.Chrome/session_changed',
chrome__pb2.SessionChangedRequest.SerializeToString,
google_dot_protobuf_dot_empty__pb2.Empty.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
10 changes: 8 additions & 2 deletions trackd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import grpc
import tzlocal

import chrome
import tmux
import tmux_pb2_grpc
import x11
Expand Down Expand Up @@ -125,17 +126,22 @@ def main():

span_storage = SpanStorage('spans.db')
span_tracker = SpanTracker(span_storage)

chrome_servicer = chrome.Chrome(span_tracker)
chrome_thread = threading.Thread(target=chrome.serve, args=(chrome_servicer,), daemon=True)
chrome_thread.start()

tmux_adapter = tmux.TmuxAdapter(span_tracker)

x_window_focus_tracker = x11.XWindowFocusTracker()
x_window_focus_tracker.register(tmux_adapter.set_focused_x_window_id)
x_thread = threading.Thread(target=x_window_focus_tracker.run, daemon=True)
x_thread.start()

servicer = tmux.Tmux(tmux_adapter)
tmux_servicer = tmux.Tmux(tmux_adapter)

server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
tmux_pb2_grpc.add_TmuxServicer_to_server(servicer, server)
tmux_pb2_grpc.add_TmuxServicer_to_server(tmux_servicer, server)
server.add_insecure_port('[::]:3141')
server.start()
server.wait_for_termination()
Expand Down

0 comments on commit f0531c4

Please sign in to comment.