support for user agents

This commit is contained in:
Bas van Kervel
2015-08-07 09:56:49 +02:00
parent 2fcf7f1241
commit f9cbd16f27
19 changed files with 363 additions and 90 deletions

View File

@ -49,7 +49,7 @@ var (
)
type EthereumClient interface {
// Close underlaying connection
// Close underlying connection
Close()
// Send request
Send(interface{}) error

View File

@ -60,7 +60,7 @@ func (self *InProcClient) Send(req interface{}) error {
}
func (self *InProcClient) Recv() (interface{}, error) {
return self.lastRes, self.lastErr
return *shared.NewRpcResponse(self.lastId, self.lastJsonrpc, self.lastRes, self.lastErr), nil
}
func (self *InProcClient) SupportedModules() (map[string]string, error) {

View File

@ -44,35 +44,18 @@ func (self *ipcClient) Close() {
func (self *ipcClient) Send(req interface{}) error {
var err error
if r, ok := req.(*shared.Request); ok {
if err = self.coder.WriteResponse(r); err != nil {
if _, ok := err.(*net.OpError); ok { // connection lost, retry once
if err = self.reconnect(); err == nil {
err = self.coder.WriteResponse(r)
}
if err = self.coder.WriteResponse(req); err != nil {
if _, ok := err.(*net.OpError); ok { // connection lost, retry once
if err = self.reconnect(); err == nil {
err = self.coder.WriteResponse(req)
}
}
return err
}
return fmt.Errorf("Invalid request (%T)", req)
return err
}
func (self *ipcClient) Recv() (interface{}, error) {
res, err := self.coder.ReadResponse()
if err != nil {
return nil, err
}
if r, ok := res.(shared.SuccessResponse); ok {
return r.Result, nil
}
if r, ok := res.(shared.ErrorResponse); ok {
return r.Error, nil
}
return res, err
return self.coder.ReadResponse()
}
func (self *ipcClient) SupportedModules() (map[string]string, error) {
@ -91,7 +74,7 @@ func (self *ipcClient) SupportedModules() (map[string]string, error) {
return nil, err
}
if sucRes, ok := res.(shared.SuccessResponse); ok {
if sucRes, ok := res.(*shared.SuccessResponse); ok {
data, _ := json.Marshal(sucRes.Result)
modules := make(map[string]string)
err = json.Unmarshal(data, &modules)
@ -109,8 +92,8 @@ func NewIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
}
// Start IPC server
func StartIpc(cfg IpcConfig, codec codec.Codec, offeredApi shared.EthereumApi) error {
return startIpc(cfg, codec, offeredApi)
func StartIpc(cfg IpcConfig, codec codec.Codec, initializer func(conn net.Conn) (shared.EthereumApi, error)) error {
return startIpc(cfg, codec, initializer)
}
func newIpcConnId() int {

View File

@ -48,7 +48,7 @@ func (self *ipcClient) reconnect() error {
return err
}
func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
func startIpc(cfg IpcConfig, codec codec.Codec, initializer func(conn net.Conn) (shared.EthereumApi, error)) error {
os.Remove(cfg.Endpoint) // in case it still exists from a previous run
l, err := net.Listen("unix", cfg.Endpoint)
@ -69,6 +69,13 @@ func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
id := newIpcConnId()
glog.V(logger.Debug).Infof("New IPC connection with id %06d started\n", id)
api, err := initializer(conn)
if err != nil {
glog.V(logger.Error).Infof("Unable to initialize IPC connection - %v\n", err)
conn.Close()
continue
}
go handle(id, conn, api, codec)
}

View File

@ -667,7 +667,7 @@ func (self *ipcClient) reconnect() error {
return err
}
func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
func startIpc(cfg IpcConfig, codec codec.Codec, initializer func(conn net.Conn) (shared.EthereumApi, error)) error {
os.Remove(cfg.Endpoint) // in case it still exists from a previous run
l, err := Listen(cfg.Endpoint)
@ -687,6 +687,13 @@ func startIpc(cfg IpcConfig, codec codec.Codec, api shared.EthereumApi) error {
id := newIpcConnId()
glog.V(logger.Debug).Infof("New IPC connection with id %06d started\n", id)
api, err := initializer(conn)
if err != nil {
glog.V(logger.Error).Infof("Unable to initialize IPC connection - %v\n", err)
conn.Close()
continue
}
go handle(id, conn, api, codec)
}