Wednesday, March 19, 2008

1. Liberating the Usr

One of the most frequently used programs available today is a simple chat program - a piece of software that relays text between two terminals (such as AIM, Pidgin, iChat, ICQ, etc). My instruction code is an object-oriented class written in Java that handles client side commands - commands handled locally within the user's terminal. The basic idea is that there are two threads: one that handles the "listening" and one that handles the "talking". The meat of the algorithm is in the run methods in the Reader and Writer inner classes. The Writer is constantly listening to the users keyboard for a line of text to send to the server, while the Reader is constantly listening for text sent from the server. This "constant listening" takes the form of two while loops, which just loop constantly until the boolean flag isConnected tells them to stop.

I've been using AIM since I was eleven or twelve. I went through a dozen screen names, socialized, got to know people, flirted, fought, argued - things that any adolescent would do. I still use the AIM protocol (through gChat, iChat and Pidgin), and it is still my primary means of communicating with friends from high school. Before taking my first computer science course last year, I had never given any thought to how the AIM protocol worked. I never realized that my messages were being relayed through some phantom server, through some intermediary.

The AIM protocol is a set of numbers - ports, addresses, user names - all represented in ASCII or Unicode so they can more easily read in machine language. It most likely operates in the TCP protocol, using the Client/Server model to more easily relay messages over the Internet, which is a group of protocols itself. Moreover, I chat in English, a natural language protocol that expresses my thoughts. I say that this is a protocol because it passes through so many intermediaries - through my terminal, the server, various hubs throughout the Internet, and finally the terminal of the receiver. I still don't know how secure it is - probably extremely insecure (in fact, I bet I could write a program that listens in on sockets with a certain address and port). Regardless, all of my computerized communication throughout my teenage years was reduced to a large set of protocols that didn't include human to human interaction. The English language protocol conveyed through AIM didn't include gestures or tone - signifiers that one has to pick up in RL communication. Galloway argues that control, and not freedom lies at the heart of the Internet. He says on page 25-26 of the introduction to IN: Protocol: How Control Exists After Decentralization, "I argue that the Internet is distributed not decentralized and that it is in fact highly controlled despite having few if any central points of control... In fact one might go so far as to say that Empire is the social theory and protocol the technical... Like Empire, if protocol dared to centralize, or dared to hierarchize, or dared to essentialize, it would fail." It would follow his argument that this protocol only restricts the users of the Internet, likening it to the attributes of a modern day Empire. I'm inclined to disagree. I feel that the power of the Internet lies in the agreement its contributers have made - the agreement on the type of protocol or set of protocols to be used. This agreement has empowered software developers to create programs that provide services and distribute information at unthinkable speeds. This agreement empowered my thirteen year old self to start flirting with girls, which eventually poured over into real life. Most importantly, this set of protocols, while restrictive to some, allows others to create objects that serve the layman, which provides freedom to everyone.

I would say that 90% of my friends haven't given any thought to how a computer program executes. I would also speculate that 100% of my friends used or actively use AIM, Facebook and Email. Do I benefit more from these programs than they do because I have a specific idea of how programs work? I don't think so. Writing the algorithm for a simple chat program enlightened me to the fact that it doesn't matter how it works if it just works. The black-box or idea of encapsulation in object-oriented programming relates to this. If I want to incorporate a piece of code my friend wrote, I just need to know what goes in, and what comes out. If he or she wrote it well, I don't need to know what's happening on the inside (and indeed, I have no specific knowledge of the inner workings of Java's Socket class, which I used in my ChatClient object). Ultimately the concrete understanding of the fabric of the program - the code - is no more liberating than using the program itself.

Simple Chat Client


package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
* Here's a chat client (meant for two way or group chatting).
* It has two threads so it can simultaneously read
* what the server sends it, and can write and send to the server.
* Not included is the server code, which handles another client or group of clients.
* The server receives everything sent by the clients, and then sends it to every client
* to be written (which is why you see what you just said).
* It's very simple, and sort of the base of text communication over a server.
* It employs the java.net.Socket class, which uses the TCP protocol (meaning that 100% of
* packets are received in the order in which they were sent).
* @author amiller
*
*/
public class ChatClient {
//address and port information for all users
private static final int PORT = 1445;
private static final String SERVER_ADDRESS = "chatAddress";

private Socket socket;
private String userName;
private boolean isConnected;

public ChatClient(String usrName) throws UnknownHostException, IOException{
//string to identify user
userName = usrName;
//establish the connection and start the threads
establishConnection();
}

public void establishConnection() throws UnknownHostException, IOException{
//establishes communication with the server
socket = new Socket(SERVER_ADDRESS, PORT);
isConnected = true;

Writer writeThread = new Writer();
Reader readThread = new Reader();

//this starts the reading and the writing.
writeThread.start();
readThread.start();
}

/**
* To break from the while loops and kill the
* reader and writer threads
*/
public void disconnect(){
isConnected = false;
}

private class Reader extends Thread {
//reads from the socket connection
private BufferedReader reader;
public Reader() throws IOException{
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public void run(){
String lineFromServer;
while(isConnected){
lineFromServer = null;
try {
lineFromServer = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(lineFromServer != null)
System.out.println(lineFromServer);
}
}

}
private class Writer extends Thread {
//writes to socket
private PrintWriter writer;
//listens to the local computer's keyboard
private BufferedReader reader;

public Writer() throws IOException{
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(System.in));
}
/**
* This thread is constantly listening for the user's input (locally)
* When the user has entered a line, it writes it to the socket
* that has established a connection
*/
public void run(){
while(isConnected){
try {
String line = reader.readLine();
if(line != null)
writer.println(userName + ": " + line);
} catch(Exception e){
e.printStackTrace();
}
}
}
}

}