Permalink
Browse files

Initial check-in of 70% api complete but seriously broken devl version

  • Loading branch information...
1 parent dfbc41c commit 414165afe8760996b49dded5bc44d8995347f5c8 @boggle committed Apr 28, 2010
Showing with 191 additions and 155 deletions.
  1. +7 −0 LICENSE
  2. +0 −1 Makefile
  3. +19 −0 README
  4. +55 −39 main.go
  5. +9 −44 utils.go
  6. +101 −71 zmq.go
View
@@ -0,0 +1,7 @@
+Copyright (c) 2010, Stefan Plantikow
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+Redistribution BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -4,7 +4,6 @@ PKGDIR=$(GOROOT)/pkg/$(GOOS)_$(GOARCH)
TARG=zmq
CGOFILES=utils.go zmq.go
-# GOFILES=main.pb.go
CGO_CFLAGS=-I. -I "$(GOROOT)/include"
CGO_LDFLAGS=-lzmq
GOFMT=$(GOROOT)/bin/gofmt -tabwidth=2 -spaces=true -tabindent=false -w
View
19 README
@@ -0,0 +1,19 @@
+gozero
+======
+
+ZMQ bindings for Go.
+
+This is a very early attempt at libzmq bindings to the Go language.
+
+To run this you first need to install github.com/boggle/coffer
+which is a small package for copying memory between C and go.
+
+Basic contexts, sockets, and messages have been implemented along
+with Send, and Receive. No Poll, no setSocketOpts at this time.
+
+Out of the box this gives you inproc communication and tcp, see main.go.
+IPC is broken for some yet unknown reason. TCP only works with one
+socket per UNIX process, so you can use tcp between go processes but
+not inside a single go process. Bugs abound. Wouldnt mind anyone
+picking this up. Thanks!
+
View
94 main.go
@@ -5,9 +5,10 @@ import . "zmq"
import . "bytes"
import rt "runtime"
import "os"
+// import "syscall"
func Server(ctx Context, ch chan bool, bchan chan bool, addr string) {
- defer func(){ ch <- true }()
+ defer func() { ch <- true }()
rt.LockOSThread()
defer rt.UnlockOSThread()
@@ -20,24 +21,28 @@ func Server(ctx Context, ch chan bool, bchan chan bool, addr string) {
bchan <- true
fmt.Println("server: Bound")
- msg := srv.Provider().NewMessage()
- buf := NewBuffer(make([]byte, 2))
- defer msg.Close()
+ msg := srv.Provider().NewMessage()
+ buf := NewBuffer(make([]byte, 2))
+ defer msg.Close()
- srv.Receive(msg, 0)
- var _, err = msg.GetData(buf)
- if (err != nil) { fmt.Println(err) }
- fmt.Println("server: Received '", buf.String(), "'")
+ srv.Receive(msg, 0)
+ var _, err = msg.GetData(buf)
+ if err != nil {
+ fmt.Println("server: Error: ", err)
+ }
+ fmt.Printf("server: Received '%v'\n", buf)
- srv.Receive(msg, 0)
- buf.Reset()
- _, err = msg.GetData(buf)
- if (err != nil) { fmt.Println(err) }
- fmt.Println("server: Received '", buf.String(), "'")
+ srv.Receive(msg, 0)
+ buf.Reset()
+ _, err = msg.GetData(buf)
+ if err != nil {
+ fmt.Println("server: Error: ", err)
+ }
+ fmt.Printf("server: Received '%v'\n", buf)
}
func Client(ctx Context, ch chan bool, addr string) {
- defer func(){ ch <- true }()
+ defer func() { ch <- true }()
rt.LockOSThread()
defer rt.UnlockOSThread()
@@ -49,28 +54,34 @@ func Client(ctx Context, ch chan bool, addr string) {
cl.Connect(addr)
fmt.Println("client: Connected")
- buf := NewBufferString("!PING!PING!PING!")
- msg := cl.Provider().NewMessage()
- defer msg.Close()
-
- var _, err = msg.SetData(buf)
- if (err != nil) { fmt.Println("client: ", err) }
- fmt.Println("client: Sending '", buf.String(), "'")
- cl.Send(msg, 0)
-
- buf.Reset()
- buf.WriteString("XXX")
- _, err = msg.SetData(buf)
- if (err != nil) { fmt.Println("client: ", err) }
- fmt.Println("client: Sending '", buf.String(), "'")
- cl.Send(msg, 0)
+ buf := NewBufferString("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
+ msg := cl.Provider().NewMessage()
+ defer msg.Close()
+
+ fmt.Printf("client: Sending '%v'\n", buf.String())
+ var _, err = msg.SetData(buf)
+ if err != nil {
+ fmt.Println("client: Error: ", err)
+ }
+ cl.Send(msg, 0)
+
+ buf.WriteString("XXX")
+ fmt.Printf("client: Sending '%v'\n", buf.String())
+ _, err = msg.SetData(buf)
+ if err != nil {
+ fmt.Println("client: Error: ", err)
+ }
+ cl.Send(msg, 0)
}
func main() {
- ctx := LibZmqProvider().NewContext(DefaultInitArgs())
+ ctx := LibZmqProvider().NewContext(DefaultInitArgs())
defer ctx.Close()
- ch := make(chan bool)
- bchan := make(chan bool)
+ ctx2 := LibZmqProvider().NewContext(DefaultInitArgs())
+ defer ctx2.Close()
+
+ ch := make(chan bool)
+ bchan := make(chan bool)
if len(os.Args) != 3 {
fmt.Println(os.Args[0], "srv|cl|all addr")
@@ -79,18 +90,23 @@ func main() {
mode := os.Args[1]
addr := os.Args[2]
switch {
+ default:
+ fmt.Println(os.Args[0], "srv|cl|all addr")
+ os.Exit(1)
case mode == "srv":
- Server(ctx, ch, bchan, addr)
- <- bchan
+ go Server(ctx, ch, bchan, addr)
+ <-bchan
+ // syscall.Sleep(10 * 1000 * 1000 * 1000)
case mode == "cl":
- Client(ctx, ch, addr)
+ go Client(ctx, ch, addr)
case mode == "all":
- go Server(ctx, ch, bchan, addr)
- <- bchan
+ go Server(ctx2, ch, bchan, addr)
+ <-bchan
go Client(ctx, ch, addr)
- <- ch
+ <-ch
}
fmt.Println("main: Waiting to finish")
- <- ch
+
+ <-ch
}
}
View
@@ -3,7 +3,6 @@ package zmq
import "os"
import rt "runtime"
import "strconv"
-import X "unsafe"
// #include "get_errno.c"
import "C"
@@ -40,7 +39,6 @@ func (p Thunk) Syncing(ch chan interface{}, msg interface{}) Thunk {
}
-
// ******** Configuration ********
// Integer value of environment variable GOMAXPROCS if > 1, 1 otherwise
@@ -81,7 +79,7 @@ func CondCatchError(cond bool, errnoFun ErrnoFun) {
// Gets errno from C and converts it into an os.Error using errnoFun.
// Requires that the executing go routine has been locked to an OSThread.
func CatchError(errnoFun ErrnoFun) {
- CatchErrno(errno(), errnoFun)
+ CatchErrno(errno(), errnoFun)
}
// Converts c_errno into an os.Error using errnoFun.
@@ -100,46 +98,13 @@ func CatchErrno(c_errno os.Errno, errnoFun ErrnoFun) {
// Converts c_errno into an os.Error using errnoFun.
// Requires that the executing go routine has been locked to an OSThread.
func FetchError(c_errno os.Errno, errnoFun ErrnoFun) os.Error {
- if (c_errno == 0) { return nil }
-
- error := errnoFun(c_errno)
- if error == nil {
- return os.Error(c_errno)
- }
- return error
-}
-
-
-// ******** cgo interaction ********
-
-// Transplants Reader interface on *byte
-type ptrReader struct {
- seek int
- size int
- ptr X.Pointer
-}
-
-func (p *ptrReader) Read(dst []uint8) (n int, err os.Error) {
- dstCap := len(dst)
- if (dstCap <= 0) { return 0, os.EINVAL }
- avail := p.size - p.seek
- if (avail <= 0) { return 0, os.EOF }
- n = avail
- if (n > dstCap) { n = dstCap }
- C.memmove(X.Pointer(&dst[0]), X.Pointer(uintptr(p.ptr) + uintptr(p.seek)), len2size(n))
- p.seek = p.seek + n
- avail = avail - n
- if (avail > 0) { return n, nil }
- return n, os.EOF
-}
-
-// Simple converters between size_t (used by C) and int (bytecount used by Go)
+ if c_errno == 0 {
+ return nil
+ }
-func len2size(length int) C.size_t {
- if (length < 0) { panic(os.EINVAL) }
- return C.size_t(length)
+ error := errnoFun(c_errno)
+ if error == nil {
+ return os.Error(c_errno)
+ }
+ return error
}
-
-func size2len(sz C.size_t) int { return int(sz) }
-
-// {}
Oops, something went wrong.

0 comments on commit 414165a

Please sign in to comment.