-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package app2 | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/rpc" | ||
|
||
"github.com/skycoin/dmsg/cipher" | ||
"github.com/skycoin/skycoin/src/util/logging" | ||
) | ||
|
||
// Server is a server for app/visor communication. | ||
type Server struct { | ||
log *logging.Logger | ||
sockFile string | ||
rpcS *rpc.Server | ||
} | ||
|
||
// NewServer constructs server. | ||
func NewServer(log *logging.Logger, sockFile string) *Server { | ||
return &Server{ | ||
log: log, | ||
sockFile: sockFile, | ||
rpcS: rpc.NewServer(), | ||
} | ||
} | ||
|
||
// ListenAndServe starts listening for incoming app connections via unix socket. | ||
func (s *Server) ListenAndServe() error { | ||
l, err := net.Listen("unix", s.sockFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go s.serveConn(conn) | ||
} | ||
} | ||
|
||
// serveConn instantiates RPC gateway for an application. | ||
func (s *Server) serveConn(conn net.Conn) { | ||
var appKey cipher.PubKey | ||
if _, err := io.ReadFull(conn, appKey[:]); err != nil { | ||
s.closeConn(conn) | ||
s.log.WithError(err).Error("error reading app key") | ||
return | ||
} | ||
|
||
appKeyHex := appKey.Hex() | ||
|
||
gateway := newRPCGateway(logging.MustGetLogger(fmt.Sprintf("rpc_gateway_%s", appKeyHex))) | ||
if err := s.rpcS.RegisterName(appKeyHex, gateway); err != nil { | ||
s.closeConn(conn) | ||
s.log.WithError(err).Errorf("error registering rpc gateway for app with key %s", appKeyHex) | ||
return | ||
} | ||
|
||
go s.rpcS.ServeConn(conn) | ||
} | ||
|
||
// closeConn closes connection and logs error if any. | ||
func (s *Server) closeConn(conn net.Conn) { | ||
if err := conn.Close(); err != nil { | ||
s.log.WithError(err).Error("error closing conn") | ||
} | ||
} |