@@ -18,7 +18,8 @@ use std::io;
1818use std:: net:: SocketAddr ;
1919use std:: sync:: Arc ;
2020
21- use crpc:: { start_http, Compatibility , MetaIoHandler , Server } ;
21+ use crpc:: { start_http, start_ipc, HttpServer , IpcServer } ;
22+ use crpc:: { Compatibility , MetaIoHandler } ;
2223use rpc_apis;
2324
2425#[ derive( Debug , PartialEq ) ]
@@ -40,7 +41,7 @@ impl HttpConfiguration {
4041 }
4142}
4243
43- pub fn new_http ( cfg : HttpConfiguration , deps : Arc < rpc_apis:: ApiDependencies > ) -> Result < Server , String > {
44+ pub fn new_http ( cfg : HttpConfiguration , deps : Arc < rpc_apis:: ApiDependencies > ) -> Result < HttpServer , String > {
4445 let url = format ! ( "{}:{}" , cfg. interface, cfg. port) ;
4546 let addr = url. parse ( ) . map_err ( |_| format ! ( "Invalid JSONRPC listen host/port given: {}" , url) ) ?;
4647 let server = setup_http_rpc_server ( & addr, cfg. cors , cfg. hosts , deps) ?;
@@ -52,7 +53,7 @@ pub fn setup_http_rpc_server(
5253 cors_domains : Option < Vec < String > > ,
5354 allowed_hosts : Option < Vec < String > > ,
5455 deps : Arc < rpc_apis:: ApiDependencies > ,
55- ) -> Result < Server , String > {
56+ ) -> Result < HttpServer , String > {
5657 let server = setup_rpc_server ( deps) ;
5758 let start_result = start_http ( url, cors_domains, allowed_hosts, server) ;
5859 match start_result {
@@ -64,6 +65,23 @@ pub fn setup_http_rpc_server(
6465 }
6566}
6667
68+ #[ derive( Debug , PartialEq ) ]
69+ pub struct IpcConfiguration {
70+ pub socket_addr : String ,
71+ }
72+
73+ pub fn new_ipc ( cfg : IpcConfiguration , deps : Arc < rpc_apis:: ApiDependencies > ) -> Result < IpcServer , String > {
74+ let server = setup_rpc_server ( deps) ;
75+ let start_result = start_ipc ( & cfg. socket_addr , server) ;
76+ match start_result {
77+ Err ( ref err) if err. kind ( ) == io:: ErrorKind :: AddrInUse => {
78+ Err ( format ! ( "IPC address {} is already in use, make sure that another instance of a Codechain node is not running or change the address using the --ipc-path options." , cfg. socket_addr) )
79+ } ,
80+ Err ( e) => Err ( format ! ( "IPC error: {:?}" , e) ) ,
81+ Ok ( server) => Ok ( server) ,
82+ }
83+ }
84+
6785fn setup_rpc_server ( deps : Arc < rpc_apis:: ApiDependencies > ) -> MetaIoHandler < ( ) > {
6886 let mut handler = MetaIoHandler :: with_compatibility ( Compatibility :: Both ) ;
6987 deps. extend_api ( & mut handler) ;
0 commit comments