Skip to content

Commit

Permalink
Refactor to minimise use of static methods in PeerGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Mar 21, 2024
1 parent 974b604 commit e71f78d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 40 deletions.
50 changes: 33 additions & 17 deletions convex-gui/src/main/java/convex/gui/PeerGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public static void main(String[] args) {
// call to set up Look and Feel
Toolkit.init();

launchPeerGUI(DEFAULT_NUM_PEERS, AKeyPair.generate());
}

public static void launchPeerGUI(int peerNum, AKeyPair genesis) {
EventQueue.invokeLater(()->{
try {
PeerGUI.frame = new JFrame();
Expand All @@ -92,7 +96,7 @@ public static void main(String[] args) {
frame.setBounds(100, 100, 1200, 900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

PeerGUI window = new PeerGUI();
PeerGUI window = new PeerGUI(peerNum,genesis);
frame.getContentPane().add(window, BorderLayout.CENTER);
frame.setVisible(true);

Expand All @@ -119,15 +123,23 @@ public void windowClosing(WindowEvent winEvt) {
JPanel accountsPanel;
JTabbedPane tabs;
RESTServer restServer;

AKeyPair genesisKey;

/**
* Create the application.
* @param genesis Genesis key pair
* @param peerNum Numer of peers to initialise in geneis
*/
public PeerGUI() {
public PeerGUI(int peerNum, AKeyPair genesis) {
// Create key pairs for peers, use genesis key as first keypair
genesisKey=genesis;
int peerCount=DEFAULT_NUM_PEERS;
for (int i=0; i<peerCount; i++) {
KEYPAIRS.add(AKeyPair.generate());
}
KEYPAIRS.set(0, genesis);

PEERKEYS=KEYPAIRS.stream().map(kp->kp.getAccountKey()).collect(Collectors.toList());
genesisState=Init.createState(PEERKEYS);
latestState = StateModel.create(genesisState);
Expand Down Expand Up @@ -223,9 +235,11 @@ public void run() {
}
}, "GUI Manager state update thread");

public static DefaultListModel<ConvexLocal> peerList = new DefaultListModel<ConvexLocal>();

public DefaultListModel<ConvexLocal> peerList = new DefaultListModel<ConvexLocal>();

public DefaultListModel<ConvexLocal> getPeerList() {
return peerList;
}

@Override
public void finalize() {
Expand Down Expand Up @@ -256,7 +270,7 @@ public WalletPanel getWalletPanel() {
* @throws IOException If IO error occurs during connection attempt
* @throws TimeoutException If attempt to connect times out
*/
public static Convex makeConnection(Address address,AKeyPair kp) throws IOException, TimeoutException {
public Convex makeConnection(Address address,AKeyPair kp) throws IOException, TimeoutException {
InetSocketAddress host = getDefaultConvex().getHostAddress();
return Convex.connect(host,address, kp);
}
Expand All @@ -283,7 +297,7 @@ public CompletableFuture<Result> execute(WalletEntry we, ACell code) {
* @param trans Transaction to execute
* @return Future for Result
*/
public static CompletableFuture<Result> execute(WalletEntry we, ATransaction trans) {
public CompletableFuture<Result> execute(WalletEntry we, ATransaction trans) {
try {
AKeyPair kp = we.getKeyPair();
Convex convex = makeConnection(we.getAddress(),kp);
Expand All @@ -302,7 +316,7 @@ public static CompletableFuture<Result> execute(WalletEntry we, ATransaction tra
* @param trans Transaction to execute
* @param receiveAction Action to invoke when result is received
*/
public static void execute(WalletEntry we, ATransaction trans, Consumer<Result> receiveAction) {
public void execute(WalletEntry we, ATransaction trans, Consumer<Result> receiveAction) {
execute(we,trans).thenAcceptAsync(receiveAction);
}

Expand All @@ -318,8 +332,8 @@ public StateModel<State> getStateModel() {
return latestState;
}

public static Convex getDefaultConvex() {
return PeersListPanel.getFirst();
public ConvexLocal getDefaultConvex() {
return peerList.getElementAt(0);
}

public static Address getUserAddress(int i) {
Expand All @@ -330,21 +344,21 @@ public AKeyPair getUserKeyPair(int i) {
return KEYPAIRS.get(i);
}

public static Address getGenesisAddress() {
public Address getGenesisAddress() {
return Init.getGenesisAddress();
}

public static Convex connectClient(Address address, AKeyPair keyPair) {
public Convex connectClient(Address address, AKeyPair keyPair) {
try {
return makeConnection(address,keyPair);
} catch (IOException | TimeoutException e) {
throw Utils.sneakyThrow(e);
}
}

private static HashMap<Server,StateModel<Peer>> models=new HashMap<>();
private HashMap<Server,StateModel<Peer>> models=new HashMap<>();

public static StateModel<Peer> getStateModel(Convex peer) {
public StateModel<Peer> getStateModel(Convex peer) {
Server s=peer.getLocalServer();
if (s!=null) {
StateModel<Peer> model=models.get(s);
Expand All @@ -366,7 +380,7 @@ public static StateModel<Peer> getStateModel(Convex peer) {
return null;
}

public static Server getRandomServer() {
public Server getRandomServer() {
Server result=null;
int n=peerList.getSize();
int found=0;
Expand All @@ -383,7 +397,7 @@ public static Server getRandomServer() {
return result;
}

public static Server getPrimaryServer() {
public Server getPrimaryServer() {
int n=peerList.getSize();
for (int i=0; i<n; i++) {
ConvexLocal c=peerList.elementAt(i);
Expand All @@ -395,7 +409,7 @@ public static Server getPrimaryServer() {
return null;
}

public static void runWithLatestState(Consumer<State> f) {
public void runWithLatestState(Consumer<State> f) {
AStore tempStore=Stores.current();
try {
Server s=getPrimaryServer();
Expand All @@ -406,7 +420,7 @@ public static void runWithLatestState(Consumer<State> f) {
}
}

public static void runOnPrimaryServer(Consumer<Server> f) {
public void runOnPrimaryServer(Consumer<Server> f) {
Server s=getPrimaryServer();
runOnServer(s,f);
}
Expand All @@ -420,4 +434,6 @@ public static void runOnServer(Server server,Consumer<Server> f) {
Stores.setCurrent(tempStore);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public PeerComponent(PeerGUI manager, ConvexLocal value) {
DropdownMenu dm = new DropdownMenu(popupMenu);
add(dm, BorderLayout.EAST);

StateModel<Peer> model=PeerGUI.getStateModel(convex);
StateModel<Peer> model=manager.getStateModel(convex);
if (model!=null) {
model.addPropertyChangeListener(e->{
blockView.repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public class WalletComponent extends BaseListComponent {
JPanel buttons = new JPanel();

private Address address;
PeerGUI manager;

public WalletComponent(PeerGUI manager,WalletEntry initialWalletEntry) {
this.walletEntry = initialWalletEntry;
this.manager=manager;
address = walletEntry.getAddress();

setLayout(new MigLayout("fillx"));
Expand Down Expand Up @@ -72,7 +74,7 @@ public WalletComponent(PeerGUI manager,WalletEntry initialWalletEntry) {
buttons.add(replButton);
replButton.setIcon(Toolkit.REPL_ICON);
replButton.addActionListener(e -> {
ConvexClient c= ConvexClient.launch(PeerGUI.connectClient(walletEntry.getAddress(),walletEntry.getKeyPair()));
ConvexClient c= ConvexClient.launch(manager.connectClient(walletEntry.getAddress(),walletEntry.getKeyPair()));
c.tabs.setSelectedComponent(c.replPanel);
});
replButton.setToolTipText("Launch a client REPL for this account");
Expand Down Expand Up @@ -141,7 +143,7 @@ private void resetTooltipTExt(JComponent b) {

private String getInfoString() {
StringBuilder sb=new StringBuilder();
PeerGUI.runWithLatestState(s->{
manager.runWithLatestState(s->{
AccountStatus as=s.getAccount(address);
if (as!=null) {
Long bal=as.getBalance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.concurrent.TimeoutException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
Expand Down Expand Up @@ -44,6 +45,8 @@ public class PeersListPanel extends JPanel {
JPanel peerViewPanel;
JScrollPane scrollPane;

private PeerGUI manager;

private static final Logger log = LoggerFactory.getLogger(PeersListPanel.class.getName());

public void launchAllPeers(PeerGUI manager) {
Expand Down Expand Up @@ -71,7 +74,7 @@ public void launchPeer(PeerGUI manager) {
AKeyPair kp=AKeyPair.generate();

try {
Server base=getFirst().getLocalServer();
Server base=manager.getPrimaryServer();
ConvexLocal convex=Convex.connect(base, base.getPeerController(), base.getKeyPair());
Address a= convex.createAccountSync(kp.getAccountKey());
long amt=convex.getBalance()/10;
Expand Down Expand Up @@ -105,34 +108,32 @@ public void launchPeer(PeerGUI manager) {
}
}

public static ConvexLocal getFirst() {
return PeerGUI.peerList.elementAt(0);
}

/**
* Gets a list of all locally operating Servers from the current peer list.
*
* @return List of local PeerView objects
*/
public List<ConvexLocal> getPeerViews() {
DefaultListModel<ConvexLocal> peerList = manager.getPeerList();
ArrayList<ConvexLocal> al = new ArrayList<>();
int n = PeerGUI.peerList.getSize();
int n = peerList.getSize();
for (int i = 0; i < n; i++) {
ConvexLocal p = PeerGUI.peerList.getElementAt(i);
ConvexLocal p = peerList.getElementAt(i);
al.add(p);
}
return al;
}

private void addPeer(ConvexLocal convex) {
PeerGUI.peerList.addElement(convex);
manager.getPeerList().addElement(convex);
}

/**
* Create the panel.
* @param manager PeerGUI instance
*/
public PeersListPanel(PeerGUI manager) {
this.manager=manager;
setLayout(new BorderLayout(0, 0));

JPanel toolBar = new ActionPanel();
Expand Down Expand Up @@ -164,15 +165,16 @@ public PeersListPanel(PeerGUI manager) {

});

ScrollyList<ConvexLocal> scrollyList = new ScrollyList<>(PeerGUI.peerList,
ScrollyList<ConvexLocal> scrollyList = new ScrollyList<>(manager.getPeerList(),
peer -> new PeerComponent(manager, peer));
add(scrollyList, BorderLayout.CENTER);
}

public void closePeers() {
int n = PeerGUI.peerList.getSize();
DefaultListModel<ConvexLocal> peerList = manager.getPeerList();
int n = peerList.getSize();
for (int i = 0; i < n; i++) {
Convex p = PeerGUI.peerList.getElementAt(i);
Convex p = peerList.getElementAt(i);
try {
p.getLocalServer().close();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public WalletPanel(PeerGUI manager) {
JButton btnNew = new JButton("New Account");
toolBar.add(btnNew);
btnNew.addActionListener(e -> {
Convex convex=PeerGUI.getDefaultConvex();
Convex convex=manager.getDefaultConvex();
AKeyPair newKP=AKeyPair.generate();
try {
Address addr=convex.createAccountSync(newKP.getAccountKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import java.awt.GridLayout;
import java.net.InetSocketAddress;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

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

import convex.api.Convex;
import convex.core.Result;
import convex.core.crypto.WalletEntry;
Expand All @@ -32,7 +33,6 @@
import convex.core.lang.impl.Fn;
import convex.core.transactions.ATransaction;
import convex.core.transactions.Invoke;
import convex.gui.PeerGUI;
import convex.gui.components.AccountChooserPanel;
import convex.gui.components.BaseListComponent;
import convex.gui.components.CodeLabel;
Expand Down Expand Up @@ -106,7 +106,7 @@ public SmartOpComponent(ActorInvokePanel parent, Address contract, Symbol sym) {
}

private void execute() {
InetSocketAddress addr = PeerGUI.getDefaultConvex().getHostAddress();
InetSocketAddress addr = parent.manager.getPrimaryServer().getHostAddress();

AVector<ACell> args = Vectors.empty();
for (int i = 0; i < paramCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PeerWindow(PeerGUI manager, ConvexLocal peer) {
}
tabbedPane.addTab("Observation", null, new JScrollPane(new ObserverPanel(server)), null);
}
tabbedPane.addTab("Stress", null, new StressPanel(peer), null);
tabbedPane.addTab("Stress", null, new StressPanel(manager,peer), null);
tabbedPane.addTab("Info", null, new PeerInfoPanel(peer), null);

PeerComponent pcom = new PeerComponent(manager, peer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public class StressPanel extends JPanel {

private JComboBox<String> txTypeBox;

public StressPanel(Convex peerView) {
private PeerGUI manager;

public StressPanel(PeerGUI manager, Convex peerView) {
this.peerConvex = peerView;
this.manager=manager;
this.setLayout(new BorderLayout());

actionPanel = new ActionPanel();
Expand Down Expand Up @@ -343,7 +346,7 @@ protected void connectClients(AVector<Address> clientAddresses) throws IOExcepti
Address clientAddr = clientAddresses.get(i);
Convex cc;
if (distCheckBox.isSelected()) {
InetSocketAddress pa=PeerGUI.getRandomServer().getHostAddress();
InetSocketAddress pa=manager.getRandomServer().getHostAddress();
cc=Convex.connect(pa,clientAddr,kp);
} else {
cc=Convex.connect(sa,clientAddr,kp);
Expand Down
3 changes: 2 additions & 1 deletion convex-gui/src/test/java/convex/gui/GUITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.Test;

import convex.core.State;
import convex.core.crypto.AKeyPair;
import convex.core.exceptions.InvalidDataException;

/**
Expand All @@ -14,7 +15,7 @@ public class GUITest {
* Manager is the root panel of the GUI. Everything else is built in its
* constructor.
*/
static final PeerGUI manager = new PeerGUI();
static final PeerGUI manager = new PeerGUI(3,AKeyPair.generate());

@Test
public void testState() throws InvalidDataException {
Expand Down

0 comments on commit e71f78d

Please sign in to comment.