Skip to content

Commit

Permalink
update goproxy api
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Feb 26, 2015
1 parent 33cc3d2 commit 6ea163e
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 64 deletions.
10 changes: 7 additions & 3 deletions liteidex/src/api/liteapi/liteapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,10 +615,14 @@ class IGoProxy : public QObject
public:
IGoProxy(QObject *parent) : QObject(parent) {}
virtual bool isValid() const = 0;
virtual bool isRunning(const QByteArray &id) const = 0;
virtual bool isRunning() const = 0;
virtual QByteArray commandId() const = 0;
virtual void writeStdin(const QByteArray &data) = 0;
signals:
void error(const QByteArray &id, int err);
void done(const QByteArray &id, const QByteArray &args);
void started();
void stdoutput(const QByteArray &data);
void stderror(const QByteArray &data);
void finished(int code, const QByteArray &msg);
public slots:
virtual void call(const QByteArray &id, const QByteArray &args = QByteArray()) = 0;
};
Expand Down
42 changes: 33 additions & 9 deletions liteidex/src/liteapp/goproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void cdrv_callback(char *id, int id_size, char *reply, int len, int err,
GoProxy::GoProxy(QObject *parent) :
LiteApi::IGoProxy(parent)
{
m_isRuning = false;
}

bool GoProxy::isValid() const
Expand All @@ -64,23 +65,46 @@ bool GoProxy::hasProxy()
return godrv_call_fn != 0;
}

bool GoProxy::isRunning(const QByteArray &id) const
bool GoProxy::isRunning() const
{
return m_runMap[id];
return m_isRuning;
}

QByteArray GoProxy::commandId() const
{
return m_id;
}

void GoProxy::writeStdin(const QByteArray &data)
{
godrv_call("stdin",data,&cdrv_callback,this);
}

void GoProxy::call(const QByteArray &id, const QByteArray &args)
{
m_runMap[id] = true;
{
m_id = id;
m_isRuning = false;
godrv_call(id,args,&cdrv_callback,this);
}

void GoProxy::callback(char *id, int id_size, char *reply, int reply_size, int err)
void GoProxy::callback(char *id, int id_size, char *reply, int reply_size, int flag)
{
if (err != 0) {
emit error(QByteArray(id,id_size),err);
} else {
emit done(QByteArray(id,id_size),QByteArray(reply,reply_size));
if (m_id != QByteArray(id,id_size)) {
return;
}
if (flag == 0) {
m_isRuning = true;
emit started();
} else if(flag == 1) {
emit stdoutput(QByteArray(reply,reply_size));
} else if (flag == 2) {
emit stderror(QByteArray(reply,reply_size));
} else if (flag == 3) {
m_isRuning = false;
emit finished(0,"");
} else if (flag == 4) {
m_isRuning = false;
emit finished(2,QByteArray(reply,reply_size));
}
}

Expand Down
9 changes: 6 additions & 3 deletions liteidex/src/liteapp/goproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ class GoProxy : public LiteApi::IGoProxy
Q_OBJECT
public:
explicit GoProxy(QObject *parent = 0);
virtual bool isValid() const;
static bool hasProxy();
virtual bool isRunning(const QByteArray &id) const;
virtual bool isValid() const;
virtual bool isRunning() const;
virtual QByteArray commandId() const;
virtual void writeStdin(const QByteArray &data);
public slots:
virtual void call(const QByteArray &id, const QByteArray &args = QByteArray());
public:
void callback(char *id, int id_size, char *reply, int reply_size, int err);
protected:
QMap<QByteArray,bool> m_runMap;
bool m_isRuning;
QByteArray m_id;
};

#endif // GOPROXY_H
6 changes: 3 additions & 3 deletions liteidex/src/liteapp/liteapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ LiteApp::LiteApp()

//m_projectManager->addFactory(new FolderProjectFactory(this,this));

connect(m_goProxy,SIGNAL(done(QByteArray,QByteArray)),this,SLOT(goproxyDone(QByteArray,QByteArray)));
connect(m_goProxy,SIGNAL(stdoutput(QByteArray)),this,SLOT(goproxyDone(QByteArray)));
connect(this,SIGNAL(key_escape()),m_mainwindow,SLOT(hideOutputWindow()));
connect(m_mainwindow,SIGNAL(fullScreenStateChanged(bool)),m_fullScreent,SLOT(setChecked(bool)));
}
Expand Down Expand Up @@ -917,7 +917,7 @@ void LiteApp::dbclickLogOutput(QTextCursor cur)
}
}

void LiteApp::goproxyDone(const QByteArray &id, const QByteArray &reply)
void LiteApp::goproxyDone(const QByteArray &reply)
{
this->appendLog("GoProxy",QString("%1 = %2").arg(QString::fromUtf8(id)).arg(QString::fromUtf8(reply).trimmed()));
this->appendLog("GoProxy",QString("%1 = %2").arg(QString::fromUtf8(m_goProxy->commandId())).arg(QString::fromUtf8(reply).trimmed()));
}
2 changes: 1 addition & 1 deletion liteidex/src/liteapp/liteapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class LiteApp : public IApplication
void setPluginPath(const QString &path);
void setResourcePath(const QString &path);
protected slots:
void goproxyDone(const QByteArray &id,const QByteArray &reply);
void goproxyDone(const QByteArray &reply);
void dbclickLogOutput(QTextCursor);
void projectReloaded();
void currentProjectChanged(LiteApi::IProject *project);
Expand Down
83 changes: 56 additions & 27 deletions liteidex/src/liteidex/liteapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ package main
/*
extern void cdrv_init(void *fn);
extern int cdrv_main(int argc,char** argv);
//extern void cdrv_cb(void *cb, void *id, void *reply, int size, int err, void* ctx);
//extern void cdrv_cb(void *cb, void *id, void *reply, int size, int flag, void* ctx);
static void cdrv_init_ex()
{
extern int godrv_call(void* id,int id_size, void* args, int args_size, void* cb, void* ctx);
cdrv_init(&godrv_call);
}
static void cdrv_cb(void *cb, void *id, int id_size, void *reply, int size, int err, void* ctx)
static void cdrv_cb(void *cb, void *id, int id_size, void *reply, int size, int flag, void* ctx)
{
typedef void (*DRV_CALLBACK)(void *id, int id_size, void *reply, int len, int err, void *ctx);
((DRV_CALLBACK)(cb))(id,id_size,reply,size,err,ctx);
typedef void (*DRV_CALLBACK)(void *id, int id_size, void *reply, int len, int flag, void *ctx);
((DRV_CALLBACK)(cb))(id,id_size,reply,size,flag,ctx);
}
#cgo windows LDFLAGS: -L../../liteide/bin -lliteapp
Expand All @@ -25,6 +25,7 @@ static void cdrv_cb(void *cb, void *id, int id_size, void *reply, int size, int
*/
import "C"
import (
"bytes"
"log"
"strings"
"unsafe"
Expand All @@ -48,35 +49,59 @@ func cdrv_main(args []string) int {
return int(C.cdrv_main(C.int(argc), &cargs[0]))
}

func cdrv_cb(cb unsafe.Pointer, id []byte, reply []byte, err int, ctx unsafe.Pointer) {
C.cdrv_cb(cb, unsafe.Pointer(&id[0]), C.int(len(id)), unsafe.Pointer(&reply[0]), C.int(len(reply)), C.int(err), ctx)
func cdrv_cb(cb unsafe.Pointer, id []byte, reply []byte, flag int, ctx unsafe.Pointer) {
if len(reply) == 0 {
C.cdrv_cb(cb, unsafe.Pointer(&id[0]), C.int(len(id)), nil, 0, C.int(flag), ctx)
} else {
C.cdrv_cb(cb, unsafe.Pointer(&id[0]), C.int(len(id)), unsafe.Pointer(&reply[0]), C.int(len(reply)), C.int(flag), ctx)
}
}

//export godrv_call
func godrv_call(id unsafe.Pointer, id_size C.int, args unsafe.Pointer, size C.int, cb unsafe.Pointer, ctx unsafe.Pointer) C.int {
return C.int(go_call(C.GoBytes(id, id_size), C.GoBytes(args, size), cb, ctx))
}

type writer_output struct {
id []byte
cb unsafe.Pointer
ctx unsafe.Pointer
err int
type app_writer struct {
id []byte
cb unsafe.Pointer
ctx unsafe.Pointer
flag int
}

func (w *writer_output) Write(p []byte) (n int, err error) {
func (w *app_writer) Write(p []byte) (n int, err error) {
n = len(p)
log.Println(string(w.id), string(p))
cdrv_cb(w.cb, w.id, p, w.err, w.ctx)
cdrv_cb(w.cb, w.id, p, w.flag, w.ctx)
return
}

var (
buf bytes.Buffer
)

func init() {
command.Stdin = &buf
}

type Context struct {
buf bytes.Buffer
}

var (
contextMap = make(map[unsafe.Pointer]*Context)
)

func go_call(id []byte, args []byte, cb unsafe.Pointer, ctx unsafe.Pointer) int {
if string(id) == "stdin" {
if context, ok := contextMap[ctx]; ok {
context.buf.Write(args)
}
return 0
}
for _, cmd := range command.CommandList() {
if cmd == string(id) {
go func() {
command.Stdout = &writer_output{id, cb, ctx, 0}
command.Stderr = &writer_output{id, cb, ctx, -1}
var arguments []string
arguments = append(arguments, string(id))
if len(args) > 0 {
Expand All @@ -86,21 +111,25 @@ func go_call(id []byte, args []byte, cb unsafe.Pointer, ctx unsafe.Pointer) int
}
}
}
command.ParseArgs(arguments)
context := &Context{}
contextMap[ctx] = context
//start
cdrv_cb(cb, id, nil, 0, ctx)
//run command
err := command.RunArgs(arguments,
&context.buf,
&app_writer{id, cb, ctx, 1},
&app_writer{id, cb, ctx, 2},
)
//finished
if err == nil {
cdrv_cb(cb, id, nil, 3, ctx)
} else {
cdrv_cb(cb, id, []byte(err.Error()), 4, ctx)
}
}()
return 0
}
}
// return 1
// if fn, ok := cmdFuncMap[string(id)]; ok {
// go func(id, args []byte, cb, ctx unsafe.Pointer) {
// rep, err := fn(args)
// if err != nil {
// cdrv_cb(cb, id, []byte{0}, -1, ctx)
// }
// cdrv_cb(cb, id, append(rep, 0), 0, ctx)
// }(id, args, cb, ctx)
// return 0
// }
return -1
}
27 changes: 9 additions & 18 deletions liteidex/src/liteidex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ import (
"fmt"
"os"

"github.com/visualfc/gotools/astview"
"github.com/visualfc/gotools/command"
"github.com/visualfc/gotools/docview"
"github.com/visualfc/gotools/finddoc"
"github.com/visualfc/gotools/goapi"
"github.com/visualfc/gotools/goimports"
"github.com/visualfc/gotools/gopresent"
"github.com/visualfc/gotools/jsonfmt"
"github.com/visualfc/gotools/pkgs"
"github.com/visualfc/gotools/runcmd"
"github.com/visualfc/gotools/types"
)

func main() {
Expand All @@ -27,14 +18,14 @@ var (
)

func init() {
command.Register(types.Command)
command.Register(jsonfmt.Command)
command.Register(finddoc.Command)
command.Register(runcmd.Command)
command.Register(docview.Command)
command.Register(astview.Command)
command.Register(goimports.Command)
command.Register(gopresent.Command)
command.Register(goapi.Command)
// command.Register(types.Command)
// command.Register(jsonfmt.Command)
// command.Register(finddoc.Command)
// command.Register(runcmd.Command)
// command.Register(docview.Command)
// command.Register(astview.Command)
// command.Register(goimports.Command)
// command.Register(gopresent.Command)
// command.Register(goapi.Command)
command.Register(pkgs.Command)
}

0 comments on commit 6ea163e

Please sign in to comment.