diff options
Diffstat (limited to 'src/main/java/org/gnunet/voting/SubmitCommand.java')
-rw-r--r-- | src/main/java/org/gnunet/voting/SubmitCommand.java | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/src/main/java/org/gnunet/voting/SubmitCommand.java b/src/main/java/org/gnunet/voting/SubmitCommand.java index d00ea69..43e10c9 100644 --- a/src/main/java/org/gnunet/voting/SubmitCommand.java +++ b/src/main/java/org/gnunet/voting/SubmitCommand.java | |||
@@ -21,18 +21,27 @@ | |||
21 | package org.gnunet.voting; | 21 | package org.gnunet.voting; |
22 | 22 | ||
23 | 23 | ||
24 | import com.google.common.base.Charsets; | ||
25 | import com.google.common.io.Files; | ||
24 | import org.gnunet.mesh.Mesh; | 26 | import org.gnunet.mesh.Mesh; |
25 | import org.gnunet.mesh.MeshRunabout; | 27 | import org.gnunet.mesh.MeshRunabout; |
26 | import org.gnunet.mesh.TunnelEndHandler; | 28 | import org.gnunet.mesh.TunnelEndHandler; |
29 | import org.gnunet.util.AbsoluteTime; | ||
27 | import org.gnunet.util.Configuration; | 30 | import org.gnunet.util.Configuration; |
31 | import org.gnunet.util.CryptoECC; | ||
28 | import org.gnunet.util.PeerIdentity; | 32 | import org.gnunet.util.PeerIdentity; |
33 | import org.gnunet.voting.messages.SubmitFailureMessage; | ||
29 | import org.gnunet.voting.messages.SubmitMessage; | 34 | import org.gnunet.voting.messages.SubmitMessage; |
30 | import org.gnunet.voting.messages.SubmitResponseMessage; | 35 | import org.gnunet.voting.messages.SubmitSuccessMessage; |
31 | 36 | ||
32 | import java.io.File; | 37 | import java.io.File; |
38 | import java.io.IOException; | ||
33 | import java.util.List; | 39 | import java.util.List; |
34 | import java.util.Random; | 40 | import java.util.Random; |
35 | 41 | ||
42 | /** | ||
43 | * Command that submits a ballot with a vote to a remaining authority. | ||
44 | */ | ||
36 | public class SubmitCommand extends MeshRunabout implements TunnelEndHandler { | 45 | public class SubmitCommand extends MeshRunabout implements TunnelEndHandler { |
37 | private final String ballotFilename; | 46 | private final String ballotFilename; |
38 | private Ballot ballot; | 47 | private Ballot ballot; |
@@ -40,6 +49,7 @@ public class SubmitCommand extends MeshRunabout implements TunnelEndHandler { | |||
40 | private Mesh mesh; | 49 | private Mesh mesh; |
41 | private Mesh.Tunnel<Void> tunnel; | 50 | private Mesh.Tunnel<Void> tunnel; |
42 | private boolean submitted = false; | 51 | private boolean submitted = false; |
52 | private PeerIdentity currentAuthority; | ||
43 | 53 | ||
44 | @Override | 54 | @Override |
45 | public void onTunnelEnd(Mesh.Tunnel tunnel) { | 55 | public void onTunnelEnd(Mesh.Tunnel tunnel) { |
@@ -47,9 +57,23 @@ public class SubmitCommand extends MeshRunabout implements TunnelEndHandler { | |||
47 | throw new AssertionError(); | 57 | throw new AssertionError(); |
48 | } | 58 | } |
49 | 59 | ||
50 | public void visit(SubmitResponseMessage m) { | 60 | public void visit(SubmitSuccessMessage m) { |
51 | submitted = true; | 61 | submitted = true; |
52 | System.out.println("vote successfully submitted"); | 62 | System.out.println("vote successfully submitted"); |
63 | ballot.addVoucher(currentAuthority, m.voucherSig); | ||
64 | try { | ||
65 | Files.write(ballot.serialize(), new File(ballotFilename), Charsets.UTF_8); | ||
66 | } catch (IOException e) { | ||
67 | System.out.println("could not write ballot file"); | ||
68 | return; | ||
69 | } | ||
70 | tunnel.destroy(); | ||
71 | mesh.destroy(); | ||
72 | } | ||
73 | |||
74 | public void visit(SubmitFailureMessage m) { | ||
75 | submitted = true; | ||
76 | System.out.println("vote not submitted: " + m.reason); | ||
53 | tunnel.destroy(); | 77 | tunnel.destroy(); |
54 | mesh.destroy(); | 78 | mesh.destroy(); |
55 | } | 79 | } |
@@ -74,9 +98,17 @@ public class SubmitCommand extends MeshRunabout implements TunnelEndHandler { | |||
74 | Random r = new Random(); | 98 | Random r = new Random(); |
75 | PeerIdentity authority = remainingAuthorities.get(r.nextInt(remainingAuthorities.size())); | 99 | PeerIdentity authority = remainingAuthorities.get(r.nextInt(remainingAuthorities.size())); |
76 | System.out.println("submitting to authority" + authority.toString()); | 100 | System.out.println("submitting to authority" + authority.toString()); |
101 | currentAuthority = authority; | ||
77 | mesh = new Mesh(cfg, null, this); | 102 | mesh = new Mesh(cfg, null, this); |
78 | tunnel = mesh.createTunnel(authority, TallyAuthorityDaemon.MESH_PORT, true, true, null); | 103 | tunnel = mesh.createTunnel(authority, TallyAuthorityDaemon.MESH_PORT, true, true, null); |
79 | SubmitMessage m = new SubmitMessage(); | 104 | SubmitMessage m = new SubmitMessage(); |
105 | if (ballot.voterPub == null) { | ||
106 | throw new InvalidBallotException("no voter in ballot"); | ||
107 | } | ||
108 | m.voterPub = ballot.voterPub; | ||
109 | // FIXME: implement certs | ||
110 | m.groupCertExpiration = AbsoluteTime.FOREVER.asMessage(); | ||
111 | m.groupCert = CryptoECC.Signature.randomSignature(); | ||
80 | m.ballotGuid = ballot.getBallotGuid(); | 112 | m.ballotGuid = ballot.getBallotGuid(); |
81 | m.choiceId = ballot.choiceId; | 113 | m.choiceId = ballot.choiceId; |
82 | tunnel.send(m); | 114 | tunnel.send(m); |