Skip to content

Commit

Permalink
Merge pull request tronprotocol#45 from Rovak/app-di
Browse files Browse the repository at this point in the history
Add dependency injection, refactor static classes
  • Loading branch information
sasaxie authored Jan 10, 2018
2 parents a72b12e + 44585dc commit b75048d
Show file tree
Hide file tree
Showing 27 changed files with 361 additions and 134 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ dependencies {

compile group: 'org.fusesource.jansi', name: 'jansi', version: '1.16'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.44'

compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
}

tasks.matching { it instanceof Test }.all {
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/tron/application/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.tron.application;

import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {

private Injector injector;
private ServiceContainer services;

private static final Logger logger = LoggerFactory.getLogger("Application");

public Application(Injector injector) {
this.injector = injector;
this.services = new ServiceContainer();
}

public Injector getInjector() {
return injector;
}

public void addService(Service service) {
this.services.add(service);
}

public void run() {
this.services.start();
}

public void shutdown() {
logger.info("shutting down");
this.services.stop();
System.exit(0);
}
}
35 changes: 35 additions & 0 deletions src/main/java/org/tron/application/ApplicationFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.tron.application;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class ApplicationFactory {

/**
* Build a Guice instance
*
* @return Guice
*/
public Injector buildGuice() {
return Guice.createInjector(
new Module());
}

/**
* Build a new application
*
* @return
*/
public Application build() {
return new Application(buildGuice());
}

/**
* Build a new cli application
*
* @return
*/
public CliApplication buildCli() {
return new CliApplication(buildGuice());
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/tron/application/CliApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.tron.application;

import com.google.inject.Injector;
import org.tron.peer.Peer;

public class CliApplication extends Application {

private Peer peer;

public CliApplication(Injector injector) {
super(injector);
}

public Peer getPeer() {
return peer;
}

public void setPeer(Peer peer) {
this.peer = peer;
}
}
51 changes: 51 additions & 0 deletions src/main/java/org/tron/application/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.tron.application;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.tron.consensus.client.Client;
import org.tron.consensus.server.Server;
import org.tron.storage.leveldb.LevelDbDataSourceImpl;

import javax.inject.Named;

import static org.tron.core.Constant.BLOCK_DB_NAME;
import static org.tron.core.Constant.TRANSACTION_DB_NAME;

public class Module extends AbstractModule {

@Override
protected void configure() {

}

@Provides
@Singleton
public Client buildClient() {
return new Client();
}

@Provides
@Singleton
public Server buildServer() {
return new Server();
}

@Provides
@Singleton
@Named("transaction")
public LevelDbDataSourceImpl buildTransactionDb() {
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(TRANSACTION_DB_NAME);
db.initDB();
return db;
}

@Provides
@Singleton
@Named("block")
public LevelDbDataSourceImpl buildBlockDb() {
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(BLOCK_DB_NAME);
db.initDB();
return db;
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/tron/application/Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.tron.application;

public interface Service {

void start();
void stop();
}
38 changes: 38 additions & 0 deletions src/main/java/org/tron/application/ServiceContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.tron.application;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

public class ServiceContainer implements Service {

private ArrayList<Service> services;

private static final Logger logger = LoggerFactory.getLogger("Services");

public ServiceContainer() {
this.services = new ArrayList<>();
}

public void add(Service service) {
this.services.add(service);
}

@Override
public void start() {
logger.debug("Starting services");
for (Service service : this.services) {
logger.debug("Starting " + service.getClass().getSimpleName());
service.start();
}
}

@Override
public void stop() {
for (Service service : this.services) {
logger.debug("Stopping " + service.getClass().getSimpleName());
service.stop();
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/tron/command/AccountCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.tron.command;

import org.tron.application.CliApplication;
import org.tron.peer.Peer;
import org.tron.utils.ByteArray;

Expand All @@ -24,8 +25,8 @@ public AccountCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
System.out.println(ByteArray.toHexString(peer.getMyKey().getAddress()));
public void execute(CliApplication app, String[] parameters) {
System.out.println(ByteArray.toHexString(app.getPeer().getMyKey().getAddress()));
}

@Override
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/tron/command/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
package org.tron.command;

import org.tron.peer.Peer;
import org.tron.application.CliApplication;

import java.util.Arrays;
import java.util.Scanner;

Expand All @@ -24,7 +25,7 @@ public Cli() {

}

public void run(Peer peer) {
public void run(CliApplication app) {
Scanner in = new Scanner(System.in);

while (true) {
Expand All @@ -40,30 +41,30 @@ public void run(Peer peer) {

switch (cmdArray[0]) {
case "version":
new VersionCommand().execute(peer, cmdParameters);
new VersionCommand().execute(app, cmdParameters);
break;
case "account":
new AccountCommand().execute(peer, cmdParameters);
new AccountCommand().execute(app, cmdParameters);
break;
case "getbalance":
new GetBalanceCommand().execute(peer, cmdParameters);
new GetBalanceCommand().execute(app, cmdParameters);
break;
case "send":
new ConsensusCommand().execute(peer,cmdParameters);
app.getInjector().getInstance(ConsensusCommand.class).execute(app, cmdParameters);
break;
case "printblockchain":
new PrintBlockchainCommand().execute(peer, cmdParameters);
new PrintBlockchainCommand().execute(app, cmdParameters);
break;
case "listen":
//new ConsensusCommand().getClient(peer);
break;
case "exit":
case "quit":
case "bye":
new ExitCommand().execute(peer, cmdParameters);
new ExitCommand().execute(app, cmdParameters);
case "help":
default:
new HelpCommand().execute(peer, cmdParameters);
new HelpCommand().execute(app, cmdParameters);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/tron/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/
package org.tron.command;

import org.tron.peer.Peer;
import org.tron.application.CliApplication;

public abstract class Command {
public abstract void execute(Peer peer, String[] parameters);
public abstract void execute(CliApplication app, String[] parameters);

public abstract void usage();

Expand Down
37 changes: 23 additions & 14 deletions src/main/java/org/tron/command/ConsensusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.application.CliApplication;
import org.tron.consensus.client.Client;
import org.tron.consensus.client.MessageType;
import org.tron.consensus.server.Server;
Expand All @@ -28,37 +29,43 @@
import org.tron.protos.core.TronTransaction;
import org.tron.utils.ByteArray;


import javax.inject.Inject;

import static org.fusesource.jansi.Ansi.ansi;

public class ConsensusCommand extends Command {

private static final Logger logger = LoggerFactory.getLogger("ConsensusCommand");

public void server() {
Server.serverRun();
private Client client;
private Server server;

@Inject
public ConsensusCommand(Client client) {
this.client = client;
}

public void putClient(String[] args) {
Client.putMessage(args);
client.putMessage(args);
}

public void getClient(Peer peer) {

if (Tron.getPeer().getType().equals(PeerType.PEER_SERVER)) {
Client.getMessage(peer,MessageType.TRANSACTION);
Client.getMessage(peer,MessageType.BLOCK);
}else{
Client.getMessage(peer,MessageType.BLOCK);
client.getMessage(peer, MessageType.TRANSACTION);
client.getMessage(peer, MessageType.BLOCK);
} else {
client.getMessage(peer, MessageType.BLOCK);
}
}

public void listen(Peer peer,String type) {
//Client.getMessage(args[0]);
if (type.equals(PeerType.PEER_SERVER)) {
Client.getMessage(peer,MessageType.TRANSACTION);
Client.getMessage(peer,MessageType.BLOCK);
}else{
Client.getMessage(peer,MessageType.BLOCK);
client.getMessage(peer, MessageType.TRANSACTION);
client.getMessage(peer, MessageType.BLOCK);
} else {
client.getMessage(peer, MessageType.BLOCK);
}
}

Expand Down Expand Up @@ -95,7 +102,9 @@ public void usage() {
}

@Override
public void execute(Peer peer, String[] parameters) {
public void execute(CliApplication app, String[] parameters) {
Peer peer = app.getPeer();

if (check(parameters)) {
String to = parameters[0];
long amount = Long.valueOf(parameters[1]);
Expand All @@ -105,7 +114,7 @@ public void execute(Peer peer, String[] parameters) {
if (transaction != null) {
Message message = new Message(ByteArray.toHexString
(transaction.toByteArray()), Type.TRANSACTION);
Client.putMessage1(message);
client.putMessage1(message);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/tron/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@
*/
package org.tron.command;

import org.tron.peer.Peer;
import org.tron.application.CliApplication;

import static org.fusesource.jansi.Ansi.Color.MAGENTA;
import static org.fusesource.jansi.Ansi.Color.RED;
import static org.fusesource.jansi.Ansi.ansi;

public class ExitCommand extends Command {
public ExitCommand() {
}

@Override
public void execute(Peer peer, String[] parameters) {
System.exit(0);
public void execute(CliApplication app, String[] parameters) {
app.shutdown();
}

@Override
Expand Down
Loading

0 comments on commit b75048d

Please sign in to comment.