Skip to content

Instantly share code, notes, and snippets.

View josemmo's full-sized avatar

José Miguel Moreno josemmo

View GitHub Profile
<FileHeader>
<SchemaVersion>3.2.1</SchemaVersion>
<Modality>I</Modality>
<InvoiceIssuerType>EM</InvoiceIssuerType>
<Batch>
<BatchIdentifier>A000000001234FAC2018</BatchIdentifier>
<InvoicesCount>1</InvoicesCount>
<TotalInvoicesAmount>
<TotalAmount>60.42</TotalAmount>
</TotalInvoicesAmount>
<fe:Facturae xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:fe="http://www.facturae.es/Facturae/2014/v3.2.1/Facturae">
<FileHeader>...</FileHeader>
<Parties>
<SellerParty>...</SellerParty>
<BuyerParty>...</BuyerParty>
</Parties>
<Invoices>
<Invoice>...</Invoice>
</Invoices>
<ds:Signature xmlns:xades="http://uri.etsi.org/01903/v1.3.2#">...</ds:Signature>
private void send(ObjectOutputStream stream, Object message) {
stream.writeObject(message);
stream.flush();
}
public void propagate(Object message) {
new Thread(() -> {
for (InetAddress ip : peers) {
// Abrir socket hacia el nodo
Socket s = new Socket();
s.connect(
new InetSocketAddress(ip, Cutrecoin.PORT),
Cutrecoin.TIMEOUT);
ObjectOutputStream outputStream =
new ObjectOutputStream(s.getOutputStream());
private void sync() {
if (peers.isEmpty()) return;
propagate("getBlock=latest");
propagate("getPendingTransactions");
isSynced = true;
}
void onNewMessage(ObjectOutputStream outputStream, Object message) {
if (message instanceof Transaction) {
// Guardamos la transacción para minería
cutrecoin.addPendingTransaction((Transaction) message);
} else if (message instanceof Block) {
// Guardamos el bloque como candidato
Block b = (Block) message;
// Pedimos el bloque anterior al recibido
if (cutrecoin.addCandidateBlock(b)) {
private void acceptClient(Socket s) {
new Thread(() -> {
// Abrir streams de salida y entrada
// para objetos serializados de Java
ObjectOutputStream outputStream =
new ObjectOutputStream(s.getOutputStream());
ObjectInputStream inputStream =
new ObjectInputStream(s.getInputStream());
private void connectToServer(InetAddress ip) {
try {
Socket s = new Socket();
s.connect(
new InetSocketAddress(ip, Cutrecoin.PORT),
Cutrecoin.TIMEOUT
);
s.close();
// Añadir a la lista de nodos
for (InetAddress ia : peers) {
byte[] ip = subnetLocalhost.getAddress();
for (int i=1; i<255; i++) {
ip[3] = (byte) i;
InetAddress address = InetAddress.getByAddress(ip);
// Comprobar que se añade esta máquina
if (address.equals(localhost)) continue;
// Intentar abrir socket TCP
connectToServer(address);
}
new Thread(() -> {
server = new ServerSocket(Cutrecoin.PORT);
while (isRunning) acceptClient(server.accept());
}).start();