Go
Concurrent and Systems Programming
daemon & ws
network communication patterns
net, http, url, websocket packages
02/19/12 daemon – 1
02/19/12 daemon – 2
02/19/12 daemon – 3
02/19/12 daemon – 4
func main() {
for command line {
go server.New(address, factory, requests, sessions).Run()
}
cleanup?
}
main
02/19/12 daemon – 5
func main() {
for command line {
go server.New(address, factory, requests, sessions).Run()
}
cleanup? type Factory func(net.Conn) Session
}
type Server struct {
port net.Listener
factory Factory
requests int
sessions
}
for {
c := port.Accept
go? factory(c)(c, c)
cleanup?
}
main cleanup?
server
02/19/12 daemon – 5
func main() {
for command line {
go server.New(address, factory, requests, sessions).Run()
}
cleanup? type Factory func(net.Conn) Session
}
type Server struct {
port net.Listener
factory Factory
requests int
sessions
}
for {
c := port.Accept
go? factory(c)(c, c)
cleanup?
} type Session func(io.Writer, io.Reader)
main for {
cleanup? read
server compute
write
} unix command?
02/19/12 daemon – 5
server.go
net.Listen creates listener for stream-
oriented sockets.
listener.Accept returns net.Conn which can
be configured, read and written, and closed.
use buffered channel to wait for limited
number of concurrent sessions.
use buffered channel to wait until limited
number of requests is done.
02/19/12 daemon – 6
http://golang.org/pkg/net/
echod.go, daemon.go
Session is a function which reads, computes,
and writes.
io.Copy implements an echo daemon.
exec can be used to execute a command as a
process (don't connect process to itself).
io.Copy is used with goroutines to transfer to
and from the command.
02/19/12 daemon – 7
http://golang.org/pkg/exec/
main.go
manage network (tcp or unix) and addresses
(ports or paths).
spawn and count servers, use a channel to
report errors, wait for termination, and
remove Unix domain sockets if necessary.
use signal.Incoming to intercept signals.
02/19/12 daemon – 8
http://golang.org/pkg/os/signal/
talk.go, client.go
net.Dial connects to a listener on a stream-
oriented socket and returns net.Conn which
can be configured, read and written, and
closed.
io.Copy is used with goroutines to transfer to
and from the connection.
use a channel to wait for the first goroutine to
terminate at end of file; close connection.
02/19/12 daemon – 9
http://golang.org/pkg/net/
proxy.go
local stand-in for another port, e.g., to trace
traffic and/or circumvent security restrictions.
runs a Server on a local, stream-oriented
socket.
each session connects to a local or remote
listener and uses goroutines to transfer to and
from the connection.
02/19/12 daemon – 10
qr.go
qr is a web server which uses a Google API to
create a barcode for a text.
http.Handle links a URL prefix and a handler
function, http.ListenAndServe is the server.
template.Parse compiles a template,
template.Execute walks the template and
visualizes structured data.
02/19/12 daemon – 11
http://golang.org/pkg/http/
http://golang.org/pkg/template/
ws.go, talk.go
web sockets are an alternative to
XMLHttpRequest, defined for HMTL 5.
http.Handle connects a websocket.Handler
to ws://host:port/path.
websocket.Dial pushes a ws: URL onto a
http: URL and returns a websocket.Conn
which can be configured, read and written,
and closed.
02/19/12 daemon – 12
http://golang.org/pkg/websocket/
chat.go
ChatRoom is created by a Handler and
manages a list of Peers.
Peerhas a network connection, a list of
messages to send, and two go-routines.
one receives and queues messages to all other
Peers in the ChatRoom.
the other sends queued messages.
02/19/12 daemon – 13