Skip to content

Commit

Permalink
Removed server package. Fixed CRLF line endings with .gitattributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffery committed Dec 6, 2012
1 parent d690750 commit d02dd72
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 265 deletions.
13 changes: 13 additions & 0 deletions .gitattributes
@@ -0,0 +1,13 @@
# Explicitly specify which files are text.
# Text files will be normalized (crlf -> lf)
*.go text
*.txt text
*.md text
*.html text
*.css text
*.js text

# Binary files
*.png binary
*.jpg binary
*.jpeg binary
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -20,3 +20,6 @@ _cgo_export.*
_testmain.go

*.exe

# used for generating documentation during development
doc.txt
26 changes: 12 additions & 14 deletions server/connection.go → connection.go
@@ -1,8 +1,7 @@
package server
package stomp

import (
"errors"
"github.com/jjeffery/stomp"
"net"
)

Expand All @@ -18,14 +17,14 @@ const (
type Connection struct {
conn net.Conn
channel chan Request
writeChannel chan *stomp.Frame
writeChannel chan *Frame
}

// Represents a request received from the client,
// consisting of a frame and the connection it
// was received from
type Request struct {
Frame *stomp.Frame
Frame *Frame
Connection *Connection
Error error
}
Expand All @@ -34,7 +33,7 @@ func NewConnection(conn net.Conn, channel chan Request) *Connection {
c := new(Connection)
c.conn = conn
c.channel = channel
c.writeChannel = make(chan *stomp.Frame, 32)
c.writeChannel = make(chan *Frame, 32)
go c.ReadLoop()
go c.WriteLoop()
return c
Expand All @@ -43,7 +42,7 @@ func NewConnection(conn net.Conn, channel chan Request) *Connection {
// Write a frame to the connection. TODO: caller blocks, need to introduce
// another channel and a go routine to read from the channel and write to
// the other party.
func (c *Connection) Send(f *stomp.Frame) {
func (c *Connection) Send(f *Frame) {
// place the frame on the write channel, or
// close the connection if the write channel is full,
// as this means the client is not keeping up.
Expand All @@ -62,16 +61,14 @@ func (c *Connection) Send(f *stomp.Frame) {

// TODO: should send other information, such as receipt-id
func (c *Connection) SendError(err error) {
f := new(stomp.Frame)
f.Command = stomp.Error
messageHeader := stomp.Header{Name: stomp.Message}
messageHeader.SetValue(err.Error())
f.Headers = append(f.Headers, messageHeader)
f := new(Frame)
f.Command = Error
f.Headers.Append(Message, err.Error())
c.Send(f) // will close after successful send
}

func (c *Connection) ReadLoop() {
reader := stomp.NewReader(c.conn)
reader := NewReader(c.conn)
for {
f, err := reader.Read()
if err != nil {
Expand All @@ -90,15 +87,16 @@ func (c *Connection) ReadLoop() {
}

func (c *Connection) WriteLoop() {
writer := NewWriter(c.conn)
for {
f := <-c.writeChannel
_, err := f.WriteTo(c.conn)
err := writer.Write(f)
if err != nil {
c.conn.Close()
c.channel <- Request{Connection: c, Error: err}
return
}
if f.Command == stomp.Error {
if f.Command == Error {
// sent an ERROR frame, so disconnect
c.conn.Close()
c.channel <- Request{Connection: c, Error: errors.New("closed after ERROR frame sent")}
Expand Down
7 changes: 4 additions & 3 deletions data.go
@@ -1,7 +1,8 @@
package stomp

const (
// client frames
// client frames
ConnectCommand = "CONNECT"
Send = "SEND"
Subscribe = "SUBSCRIBE"
Unsubscribe = "UNSUBSCRIBE"
Expand All @@ -12,12 +13,12 @@ const (
Abort = "ABORT"
Disconnect = "DISCONNECT"

// server frames
// server frames
Message = "MESSAGE"
Receipt = "RECEIPT"
Error = "ERROR"

// header names
// header names
ContentLength = "content-length"
ContentType = "content-type"
ReceiptHeader = "receipt"
Expand Down
4 changes: 2 additions & 2 deletions encode_test.go
Expand Up @@ -10,10 +10,10 @@ var _ = Suite(&EncodeSuite{})

func (s *EncodeSuite) TestEncodeValue(c *C) {
c.Check(encodeValue("Contains\r\nNewLine and : colon and \\ backslash"),
Equals, "Contains\\r\\nNewLine and \\c colon and \\\\ backslash")
Equals, "Contains\\r\\nNewLine and \\c colon and \\\\ backslash")
}

func (s *EncodeSuite) TestUnencodeValue(c *C) {
c.Check(unencodeValue("Contains\\r\\nNewLine and \\c colon and \\\\ backslash"),
Equals, "Contains\r\nNewLine and : colon and \\ backslash")
Equals, "Contains\r\nNewLine and : colon and \\ backslash")
}
1 change: 0 additions & 1 deletion frame.go
Expand Up @@ -47,4 +47,3 @@ func (f *Frame) ContentLength() (contentLength int, ok bool, err error) {
ok = true
return
}

54 changes: 27 additions & 27 deletions headers.go
@@ -1,6 +1,6 @@
package stomp

// Collection of STOMP headers. Each header consists of a key value pair.
// Collection of STOMP headers. Each header consists of a key value pair.
type Headers struct {
headers []string
}
Expand All @@ -15,19 +15,19 @@ func (h *Headers) Clone() *Headers {
copy(clone.headers, h.headers)
return clone
}

func (h *Headers) Count() int {
return len(h.headers) / 2
}

func (h *Headers) GetAt(index int) (key, value string) {
index *= 2
return h.headers[index], h.headers[index + 1]
}

// Sets the value for a key/value pair in the Headers collection.
// If the key already exists its value is replaced. If the key does
// not already exist it is added.
func (h *Headers) Count() int {
return len(h.headers) / 2
}

func (h *Headers) GetAt(index int) (key, value string) {
index *= 2
return h.headers[index], h.headers[index+1]
}

// Sets the value for a key/value pair in the Headers collection.
// If the key already exists its value is replaced. If the key does
// not already exist it is added.
func (h *Headers) Set(key, value string) {
if i, ok := h.index(key); ok {
h.headers[i+1] = value
Expand All @@ -36,37 +36,37 @@ func (h *Headers) Set(key, value string) {
}
}

// Appends the key/value pair to the Headers collection without
// checking if the key already exists in the collection. Use this
// method when de-serializing the headers from the frame data, as
// a frame may contain multiple values for the same key. When this
// happens, the value for the first key is used and the other values
// are ignored.
// Appends the key/value pair to the Headers collection without
// checking if the key already exists in the collection. Use this
// method when de-serializing the headers from the frame data, as
// a frame may contain multiple values for the same key. When this
// happens, the value for the first key is used and the other values
// are ignored.
func (h *Headers) Append(key, value string) {
h.headers = append(h.headers, key, value)
}

// Removes the key/value pair from the Headers collection. Takes
// no action if the key does not already exist in the colleciton.
// If the key appears more than once in the collection, all values
// are removed.
// Removes the key/value pair from the Headers collection. Takes
// no action if the key does not already exist in the colleciton.
// If the key appears more than once in the collection, all values
// are removed.
func (h *Headers) Remove(key string) {
for i, ok := h.index(key); ok; i, ok = h.index(key) {
h.headers = append(h.headers[:i], h.headers[i+2:]...)
}
}

// Returns the associated value and true if the Headers collection contains
// the specified key.
// Returns the associated value and true if the Headers collection contains
// the specified key.
func (h *Headers) Contains(key string) (string, bool) {
if i, ok := h.index(key); ok {
return h.headers[i+1], true
}
return "", false
}

// Returns the index of a header key in Headers, and a bool to indicate
// whether it was found or not.
// Returns the index of a header key in Headers, and a bool to indicate
// whether it was found or not.
func (h *Headers) index(key string) (int, bool) {
for i := 0; i < len(h.headers); i += 2 {
if h.headers[i] == key {
Expand Down
16 changes: 6 additions & 10 deletions server/processor.go → processor.go
@@ -1,8 +1,4 @@
package server

import (
"github.com/jjeffery/stomp"
)
package stomp

// input channel for receiving requests
var inputChannel chan Request
Expand All @@ -17,7 +13,7 @@ type Process struct {
handleRequest requestHandler
}

func Run() {
func RunProcessor() {

for {
select {
Expand All @@ -35,7 +31,7 @@ func handleRequest(r Request) {
processes[r.Connection] = process
}
process.handleRequest(r)

// if an error was received, remove the process
if r.Error != nil {
delete(processes, r.Connection)
Expand All @@ -47,8 +43,8 @@ func waitingForConnect(r Request) {
// no cleanup required, as nothing happened yet
return
}
if frame == nil || frame.Command != stomp.Connect {

if r.Frame == nil || r.Frame.Command != ConnectCommand {

}
}

0 comments on commit d02dd72

Please sign in to comment.