Android Open Source - bitcoin-wallet Connection






From Project

Back to project page bitcoin-wallet.

License

The source code is released under:

Copyright (C) 2011 by Caleb Anderson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the ...

If you think the Android project bitcoin-wallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.dirtyfilthy.bitcoin.protocol;
/*from w  w w  .j  a  v a  2 s  .c o m*/
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ConcurrentLinkedQueue;

import net.dirtyfilthy.bitcoin.core.Address;
import net.dirtyfilthy.bitcoin.util.MyHex;

public class Connection implements Runnable {
  private Socket socket;
  private Address address;
  private DataInputStream in;
  private DataOutputStream out;
  private PacketFactory packetFactory;
  private ConnectionHandler connectionHandler;
  private long version=ProtocolVersion.version();
  private boolean hasReceivedVersion=false;
  private boolean hasReceivedVerack=false;
  private Thread mainLoop;
  private PacketQueue packetQueue; 
  private boolean close=false;
  
  
  Connection(ConnectionHandler ch, Address address) throws IOException{
    this.address=address;
    this.connectionHandler=ch;
    this.packetFactory=new PacketFactory();
    this.packetQueue=new PacketQueue(this,connectionHandler);
  }
  
  private void startMainLoop(){
    mainLoop=new Thread(this);
    mainLoop.start();
  }
  
  public void connect() {
    
    startMainLoop();
    
  }
  
  
  public Packet createPacket(PacketType type){
    return packetFactory.create(type);
  }
  
  public void sendPacket(Packet p) {
    packetQueue.offer(p);
  }

  public void run() {
    try {
      socket=new Socket(address.getIp(),address.getPort());
      in=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
      out=new DataOutputStream(socket.getOutputStream());
      if(in==null || out==null){
        throw new IOException();
      }
      this.packetQueue.start();
    }
    catch (IOException e1) {
      connectionHandler.handleConnectionError(this, e1);
      return;
    }
    while(!close){
      try {
        
        if(in.available()==0){
          Thread.sleep(500);
          continue;
        }
        Packet p=packetFactory.readPacket(in);
        connectionHandler.handlePacket(this,p);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        connectionHandler.handleConnectionError(this, e);
      } catch (InterruptedException e) {
        break;
      }
      
    }
    
  }
  
  public void close(){
    this.close=true;
    this.mainLoop.interrupt();
    this.packetQueue.interrupt();
    try {
      if(this.socket!=null)
        this.socket.close();
    } catch (IOException e) {
      // don't care do nothing
    }
  }

  public void setVersion(long negotiatedVersion) {
    this.version = negotiatedVersion;
    packetFactory.setVersion(negotiatedVersion);
  }

  public long getVersion() {
    return version;
  }

  public void hasReceivedVersion(boolean hasReceivedVersion) {
    this.hasReceivedVersion = hasReceivedVersion;
  }

  public boolean hasReceivedVersion() {
    return this.hasReceivedVersion;
  }
  
  public Address getAddress(){
    return this.address;
  }

  public void hasRecievedVerack(boolean hasReceivedVerack) {
    this.hasReceivedVerack = hasReceivedVerack;
  }

  public boolean hasRecievedVerack() {
    return hasReceivedVerack;
  }
  
  private class PacketQueue extends Thread {
    private DataOutputStream out;
    private ConcurrentLinkedQueue<Packet> queue;
    private Connection c;
    private ConnectionHandler ch;
    private boolean close=false;
    
    public PacketQueue(Connection c, ConnectionHandler ch){
      this.c=c;
      this.ch=ch;
      this.queue=new ConcurrentLinkedQueue<Packet>();
    }
    
    public boolean offer(Packet p){
      queue.offer(p);
      return true;
    }
    
    public void run(){
      this.out=this.c.out;
      while(!this.c.close){
        Packet toSend=queue.poll();
        if(toSend==null){
          try {
            Thread.sleep(200);
            continue;
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            close=true;
            break;
          }
        }
        try {
          
          toSend.writeExternal(this.out);
          System.out.println("sending packet: "+toSend);
          System.out.println("connection: "+this.c);
          if(toSend.getCommand().equals("getheaders")){
            System.out.println("getheaders packet: "+MyHex.encode(toSend.toByteArray()));
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          ch.handleConnectionError(c,e);
        }
    
          
      }
    
    }
    
  }
  
}




Java Source Code List

net.dirtyfilthy.bitcoin.core.Address.java
net.dirtyfilthy.bitcoin.core.Base58Hash160.java
net.dirtyfilthy.bitcoin.core.BlockChain.java
net.dirtyfilthy.bitcoin.core.BlockExistsException.java
net.dirtyfilthy.bitcoin.core.BlockStore.java
net.dirtyfilthy.bitcoin.core.Block.java
net.dirtyfilthy.bitcoin.core.BtcValue.java
net.dirtyfilthy.bitcoin.core.ByteArrayable.java
net.dirtyfilthy.bitcoin.core.InvalidBlockException.java
net.dirtyfilthy.bitcoin.core.OpCode.java
net.dirtyfilthy.bitcoin.core.OpData.java
net.dirtyfilthy.bitcoin.core.OrphanBlockException.java
net.dirtyfilthy.bitcoin.core.Script.java
net.dirtyfilthy.bitcoin.core.TxIn.java
net.dirtyfilthy.bitcoin.core.TxOut.java
net.dirtyfilthy.bitcoin.core.Tx.java
net.dirtyfilthy.bitcoin.protocol.AddressBook.java
net.dirtyfilthy.bitcoin.protocol.AddressPacket.java
net.dirtyfilthy.bitcoin.protocol.BlockPacket.java
net.dirtyfilthy.bitcoin.protocol.ConnectionHandler.java
net.dirtyfilthy.bitcoin.protocol.Connection.java
net.dirtyfilthy.bitcoin.protocol.GetAddressPacket.java
net.dirtyfilthy.bitcoin.protocol.GetBlocksPacket.java
net.dirtyfilthy.bitcoin.protocol.GetDataPacket.java
net.dirtyfilthy.bitcoin.protocol.GetHeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.HeadersPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryPacket.java
net.dirtyfilthy.bitcoin.protocol.InventoryVector.java
net.dirtyfilthy.bitcoin.protocol.IrcBootStrap.java
net.dirtyfilthy.bitcoin.protocol.MalformedPacketException.java
net.dirtyfilthy.bitcoin.protocol.PacketFactory.java
net.dirtyfilthy.bitcoin.protocol.PacketType.java
net.dirtyfilthy.bitcoin.protocol.Packet.java
net.dirtyfilthy.bitcoin.protocol.PingPacket.java
net.dirtyfilthy.bitcoin.protocol.ProtocolVersion.java
net.dirtyfilthy.bitcoin.protocol.ReplyPacket.java
net.dirtyfilthy.bitcoin.protocol.TxPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionAckPacket.java
net.dirtyfilthy.bitcoin.protocol.VersionPacket.java
net.dirtyfilthy.bitcoin.util.Base58.java
net.dirtyfilthy.bitcoin.util.BigIntegerTools.java
net.dirtyfilthy.bitcoin.util.HashTools.java
net.dirtyfilthy.bitcoin.util.KeyTools.java
net.dirtyfilthy.bitcoin.util.MyHex.java
net.dirtyfilthy.bitcoin.wallet.ExposedSQLiteCursor.java
net.dirtyfilthy.bitcoin.wallet.InvalidPasswordException.java
net.dirtyfilthy.bitcoin.wallet.KeyRing.java
net.dirtyfilthy.bitcoin.wallet.SqlBlockStore.java
net.dirtyfilthy.bitcoin.wallet.Wallet.java