Beispiel #1
0
	})
})

var _ = Describe("Connection", func() {
	var (
		connection     Connection
		writeBuffer    *bytes.Buffer
		wardenMessages []proto.Message
		resourceLimits warden.ResourceLimits
	)

	assertWriteBufferContains := func(messages ...proto.Message) {
		reader := bufio.NewReader(bytes.NewBuffer(writeBuffer.Bytes()))

		for _, msg := range messages {
			req, err := transport.ReadRequest(reader)
			Ω(err).ShouldNot(HaveOccurred())

			Ω(req).Should(Equal(msg))
		}
	}

	JustBeforeEach(func() {
		writeBuffer = bytes.NewBuffer([]byte{})

		fakeConn := &FakeConn{
			ReadBuffer:  protocol.Messages(wardenMessages...),
			WriteBuffer: writeBuffer,
		}

		connection = New(fakeConn)
	. "github.com/onsi/gomega"

	protocol "github.com/cloudfoundry-incubator/garden/protocol"
	"github.com/cloudfoundry-incubator/garden/transport"
)

var _ = Describe("Reading request messages over the wire", func() {
	Context("when a request is received", func() {
		It("returns the request and no error", func() {
			payload := bufio.NewReader(
				protocol.Messages(&protocol.EchoRequest{
					Message: proto.String("some-message"),
				}),
			)

			request, err := transport.ReadRequest(payload)
			Expect(err).ToNot(HaveOccurred())
			Expect(request).To(Equal(
				&protocol.EchoRequest{
					Message: proto.String("some-message"),
				},
			))
		})
	})

	Context("when the connection is broken", func() {
		It("returns an error", func() {
			payload := protocol.Messages(&protocol.PingRequest{})

			bogusPayload := bufio.NewReader(
				bytes.NewBuffer(payload.Bytes()[0 : payload.Len()-1]),
Beispiel #3
0
func (s *WardenServer) serveConnection(conn net.Conn) {
	read := bufio.NewReader(conn)

	for {
		var response proto.Message
		var err error

		if <-s.stopping {
			conn.Close()
			break
		}

		request, err := transport.ReadRequest(read)
		if err == io.EOF {
			break
		}

		if err != nil {
			log.Println("error reading request:", err)
			continue
		}

		if <-s.stopping {
			conn.Close()
			break
		}

		s.openRequests.Incr()

		switch req := request.(type) {
		case *protocol.PingRequest:
			response, err = s.handlePing(req)
		case *protocol.EchoRequest:
			response, err = s.handleEcho(req)
		case *protocol.CapacityRequest:
			response, err = s.handleCapacity(req)
		case *protocol.CreateRequest:
			response, err = s.handleCreate(req)
		case *protocol.DestroyRequest:
			response, err = s.handleDestroy(req)
		case *protocol.ListRequest:
			response, err = s.handleList(req)
		case *protocol.StopRequest:
			response, err = s.handleStop(req)
		case *protocol.StreamInRequest:
			response, err = s.handleStreamIn(conn, read, req)
		case *protocol.StreamOutRequest:
			response, err = s.handleStreamOut(conn, req)
		case *protocol.RunRequest:
			s.openRequests.Decr()
			response, err = s.handleRun(conn, req)
			s.openRequests.Incr()
		case *protocol.AttachRequest:
			s.openRequests.Decr()
			response, err = s.handleAttach(conn, req)
			s.openRequests.Incr()
		case *protocol.LimitBandwidthRequest:
			response, err = s.handleLimitBandwidth(req)
		case *protocol.LimitMemoryRequest:
			response, err = s.handleLimitMemory(req)
		case *protocol.LimitDiskRequest:
			response, err = s.handleLimitDisk(req)
		case *protocol.LimitCpuRequest:
			response, err = s.handleLimitCpu(req)
		case *protocol.NetInRequest:
			response, err = s.handleNetIn(req)
		case *protocol.NetOutRequest:
			response, err = s.handleNetOut(req)
		case *protocol.InfoRequest:
			response, err = s.handleInfo(req)
		default:
			err = UnhandledRequestError{request}
		}

		if err != nil {
			response = &protocol.ErrorResponse{
				Message: proto.String(err.Error()),
			}
		}

		if response != nil {
			protocol.Messages(response).WriteTo(conn)
		}

		s.openRequests.Decr()
	}
}