29
29
import io .helidon .webserver .WebServer ;
30
30
import io .helidon .webserver .grpc .GrpcRouting ;
31
31
import java .io .IOException ;
32
+ import java .util .concurrent .ConcurrentHashMap ;
32
33
33
34
/** Main class for the block node server */
34
35
public class Server {
@@ -54,60 +55,61 @@ private Server() {}
54
55
*/
55
56
public static void main (final String [] args ) {
56
57
57
- try {
58
+ // Set the global configuration
59
+ final Config config = Config .create ();
60
+ Config .global (config );
61
+
62
+ // Build the gRPC service
63
+ final GrpcRouting .Builder grpcRouting = buildGrpcRouting (config );
64
+
65
+ // Start the web server
66
+ WebServer webServer = WebServer .builder ().port (8080 ).addRouting (grpcRouting ).build ();
67
+
68
+ webServer .start ();
69
+ // .start();
70
+
71
+ }
72
+
73
+ private static GrpcRouting .Builder buildGrpcRouting (final Config config ) {
58
74
59
- // Set the global configuration
60
- final Config config = Config .create ();
61
- Config .global (config );
62
-
63
- // Get Timeout threshold from configuration
64
- final long consumerTimeoutThreshold =
65
- config .get (BLOCKNODE_SERVER_CONSUMER_TIMEOUT_THRESHOLD_KEY )
66
- .asLong ()
67
- .orElse (1500L );
68
-
69
- // Initialize the reader and writer for the block storage
70
- final BlockWriter <BlockItem > blockWriter =
71
- new BlockAsDirWriter (BLOCKNODE_STORAGE_ROOT_PATH_KEY , config );
72
- final BlockReader <Block > blockReader =
73
- new BlockAsDirReader (BLOCKNODE_STORAGE_ROOT_PATH_KEY , config );
74
-
75
- final BlockStreamService blockStreamService =
76
- new BlockStreamService (
77
- consumerTimeoutThreshold ,
78
- new ItemAckBuilder (),
79
- new LiveStreamMediatorImpl (
80
- new FileSystemPersistenceHandler (blockReader , blockWriter ),
81
- (streamMediator ) -> {
82
- LOGGER .log (
83
- System .Logger .Level .ERROR ,
84
- "Shutting down the server due to an error." );
85
- }));
86
-
87
- // Start the web server
88
- WebServer .builder ()
89
- .port (8080 )
90
- .addRouting (
91
- GrpcRouting .builder ()
92
- .service (blockStreamService )
93
- .bidi (
94
- com .hedera .block .protos .BlockStreamService
95
- .getDescriptor (),
96
- SERVICE_NAME ,
97
- CLIENT_STREAMING_METHOD_NAME ,
98
- clientBidiStreamingMethod )
99
- .serverStream (
100
- com .hedera .block .protos .BlockStreamService
101
- .getDescriptor (),
102
- SERVICE_NAME ,
103
- SERVER_STREAMING_METHOD_NAME ,
104
- serverStreamingMethod ))
105
- .build ()
106
- .start ();
107
-
108
- } catch (IOException e ) {
109
- LOGGER .log (System .Logger .Level .ERROR , "An exception was thrown starting the server" , e );
110
- throw new RuntimeException (e );
75
+ try {
76
+ final BlockStreamService blockStreamService = buildBlockStreamService (config );
77
+ return GrpcRouting .builder ()
78
+ .service (blockStreamService )
79
+ .bidi (
80
+ com .hedera .block .protos .BlockStreamService .getDescriptor (),
81
+ SERVICE_NAME ,
82
+ CLIENT_STREAMING_METHOD_NAME ,
83
+ clientBidiStreamingMethod )
84
+ .serverStream (
85
+ com .hedera .block .protos .BlockStreamService .getDescriptor (),
86
+ SERVICE_NAME ,
87
+ SERVER_STREAMING_METHOD_NAME ,
88
+ serverStreamingMethod );
89
+ } catch (IOException io ) {
90
+ LOGGER .log (
91
+ System .Logger .Level .ERROR , "An exception was thrown starting the server" , io );
92
+ throw new RuntimeException (io );
111
93
}
112
94
}
95
+
96
+ private static BlockStreamService buildBlockStreamService (final Config config )
97
+ throws IOException {
98
+ // Get Timeout threshold from configuration
99
+ final long consumerTimeoutThreshold =
100
+ config .get (BLOCKNODE_SERVER_CONSUMER_TIMEOUT_THRESHOLD_KEY ).asLong ().orElse (1500L );
101
+
102
+ // Initialize the reader and writer for the block storage
103
+ final BlockWriter <BlockItem > blockWriter =
104
+ new BlockAsDirWriter (BLOCKNODE_STORAGE_ROOT_PATH_KEY , config );
105
+ final BlockReader <Block > blockReader =
106
+ new BlockAsDirReader (BLOCKNODE_STORAGE_ROOT_PATH_KEY , config );
107
+
108
+ return new BlockStreamService (
109
+ consumerTimeoutThreshold ,
110
+ new ItemAckBuilder (),
111
+ new LiveStreamMediatorImpl (
112
+ new ConcurrentHashMap <>(32 ),
113
+ new FileSystemPersistenceHandler (blockReader , blockWriter )));
114
+ }
113
115
}
0 commit comments