29.07.2013 Views

Master of Science thesis Fighting Spam

Master of Science thesis Fighting Spam

Master of Science thesis Fighting Spam

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

import <strong>Spam</strong>Cash.Utilities.*;<br />

import java.util.StringTokenizer;<br />

import java.io.StringReader;<br />

import <strong>Spam</strong>Cash.Client.GUI.Progress;<br />

import <strong>Spam</strong>Cash.Client.GUI.MainFrame;<br />

import <strong>Spam</strong>Cash.Client.Control;<br />

public class NonBlockingProxyConnection<br />

{<br />

private Selector selector;<br />

private SocketChannel serverChannel, clientChannel;<br />

private Vector clientMessageQueue, serverMessageQueue;<br />

private String className;<br />

private boolean disconnecting = false;<br />

private int channelId;<br />

private AbstractNonBlockingProxy proxy;<br />

//Encoder and decoder<br />

private Charset charset;<br />

private CharsetDecoder decoder;<br />

private CharsetEncoder encoder;<br />

// Allocate buffers<br />

private ByteBuffer buffer;<br />

private CharBuffer charBuffer;<br />

private FileReader fileReader;<br />

private BufferedReader bufferedReader;<br />

private Progress progress = new Progress();<br />

private final int bufferSize = 512;<br />

protected NonBlockingProxyConnection(AbstractNonBlockingProxy proxy, String<br />

className, int channelId, Selector selector, SocketChannel clientChannel, String<br />

serverAddress, int serverPort) throws<br />

Exception<br />

{<br />

this.className = className + ":" + "NonBlokingProxyConnection";<br />

this.proxy = proxy;<br />

this.channelId = channelId;<br />

this.selector = selector;<br />

this.clientChannel = clientChannel;<br />

clientMessageQueue = new Vector();<br />

serverMessageQueue = new Vector();<br />

//Encoder and decoder<br />

charset = Charset.forName("US-ASCII");<br />

decoder = charset.newDecoder();<br />

encoder = charset.newEncoder();<br />

// Allocate buffers<br />

buffer = ByteBuffer.allocate(bufferSize);<br />

charBuffer = CharBuffer.allocate(bufferSize);<br />

clientChannel.configureBlocking(false);<br />

clientChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);<br />

// Make connection to server<br />

InetSocketAddress socketAddress = new InetSocketAddress(serverAddress,<br />

serverPort);<br />

serverChannel = SocketChannel.open();<br />

serverChannel.configureBlocking(false);<br />

serverChannel.connect(socketAddress);<br />

serverChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ |<br />

SelectionKey.OP_WRITE);<br />

}<br />

protected boolean containsChannel(SocketChannel channel)<br />

{<br />

return channel.equals(clientChannel) || channel.equals(serverChannel);<br />

279

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!