Sunday, August 7, 2011

Pair (Generics)


package com.evs.objava33.class12;

public class Pair<KeyType, ValueType> {

private KeyType key;
private ValueType value;

public Pair(KeyType key, ValueType value) {
this.key = key;
this.value = value;
}

/**
* @return the key
*/
public KeyType getKey() {
return key;
}

/**
* @param key
*            the key to set
*/
public void setKey(KeyType key) {
this.key = key;
}

/**
* @return the value
*/
public ValueType getValue() {
return value;
}

/**
* @param value
*            the value to set
*/
public void setValue(ValueType value) {
this.value = value;
}

public String toString() {
return "Pair [key=" + key + ", value=" + value + "]";
}
}

Try LinkedList


package com.evs.objava33.class12;

import com.evs.objava33.class7.PalmTree;
import com.evs.objava33.class7.Tree;

public class TryLinkedList {

public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.addItem("One");
list.addItem("Two");
list.addItem("Three");

System.out.println(list);

LinkedList<Integer> intList = new LinkedList<Integer>();
LinkedList<PalmTree> palmList = new LinkedList<PalmTree>();

LinkedList<LinkedList<LinkedList<String>>> lst = new LinkedList<LinkedList<LinkedList<String>>>();
LinkedList<Pair<String, Integer>> newList = new LinkedList<Pair<String, Integer>>();
// newList.getFirst().getKey()
// newList.getFirst().getValue()

LinkedList<?>[] listArray = { list, intList, palmList, lst, newList };

print(list);
print(intList);
}

private static void print(LinkedList<?> list) {
StringBuilder buf = new StringBuilder();
buf.append("Linked List: ");
for (Object item = list.getFirst(); item != null; item = list.getNext()) {
buf.append(item);
buf.append(",");
}
System.out.println(buf.toString());
}
}

Linked List (Generics)


package com.evs.objava33.class12;

public class LinkedList<T> {

private ListItem start, end, current;

public LinkedList() {
start = end = current = null;
}

public LinkedList(T item) {
addItem(item);
}

public T getFirst() {
current = start;
return start == null ? null : start.item;
}

public T getNext() {
if (current != null) {
current = current.next;
}
return current == null ? null : current.item;
}

public void addItem(T[] items) {
if (items != null) {
for (T s : items) {
addItem(s);
}
}
}

public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("Linked List: ");
for (T item = getFirst(); item != null; item = getNext()) {
buf.append(item);
buf.append(",");
}
return buf.toString();
}

public void addItem(T item) {
ListItem newItem = new ListItem(item);
if (start == null) {
start = end = newItem;
} else {
end.next = newItem;
end = newItem;
}
}

private class ListItem {
T item;
ListItem next;

public ListItem(T item) {
this.item = item;
}

@Override
public String toString() {
return "ListItem [item=" + item + "]";
}
}
}

Test File & Serialization


package com.evs.objava33.class11;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestFile {

public static void main(String[] args) {
File myFile = new File("d:/abc.txt");
// File myDir = new File("c:/abc");
// File myDirFile = new File(myDir, "File.java");
// File myDirFile1 = new File("c:/abc", "File.java");
//
// try {
// File remoteFile = new File(new URI("http://www.yahoo.com/abc.html"));
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }

String userDir = System.getProperty("user.dir");

// File dDrive = new File("D:" + File.pathSeparator + "temp-read");
File dDrive = new File("D:/temp-read");
String[] files = dDrive.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
File[] files1 = dDrive.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return true;
}
});
for (String s : files) {
System.out.println(s);
/*
* abc.txt Solaris10_9-10_VM.zip WorkFlowDAOImpl.class
*/
}

// try {
// BufferedOutputStream out = new BufferedOutputStream(
// new FileOutputStream(myFile, true));
// out.write("this is my first file".getBytes());
// out.close();
//
// // RandomAccessFile raf = new RandomAccessFile(myFile, "rw");
// // raf.write("This is my first File".getBytes());
// // raf.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }

// try {
// Process p = Runtime.getRuntime().exec("ping 192.168.0.1");
// } catch (IOException e) {
// e.printStackTrace();
// }

ObjectOutputStream fout = null;
try {
fout = new ObjectOutputStream(new FileOutputStream(myFile));
fout.writeObject(new TestObject(1, "One"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

ObjectInputStream fin = null;
try {
fin = new ObjectInputStream(new FileInputStream(myFile));
Object obj = fin.readObject();
if (obj instanceof TestObject) {
TestObject testObj = (TestObject) obj;
System.out.println(testObj);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private static class TestObject implements Serializable {
private static final long serialVersionUID = -4314877130551524995L;

private String value;
private Integer key;
private transient String anotherString;

public TestObject(Integer key, String value) {
this.key = key;
this.value = value;
this.anotherString = "Unknown";
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestObject [value=" + value + ", key=" + key
+ ", anotherString=" + anotherString + "]";
}

/**
* @return the value
*/
public String getValue() {
return value;
}

/**
* @param value
*            the value to set
*/
public void setValue(String value) {
this.value = value;
}

/**
* @return the key
*/
public Integer getKey() {
return key;
}

/**
* @param key
*            the key to set
*/
public void setKey(Integer key) {
this.key = key;
}
}
}

Thursday, August 4, 2011

Assignment - Week 5

Create one chat solution, which will be used to chat between two users.

- Server component will be used to connect multiple users
- Client Component will be used to connect to server.
- Multiple Client Components will be able to send message to each other via Server.

Server Start Up Flow
- Server Listen to a port (6666)

Connect Flow
- Client Open Socket with Server and then Send a message 'LOGON' which will ensure that this user is connected with Server

Chat with Server
- Client Open Socket with Server
- Client Send 'LOGON' Message to Server
- Client Send Message to 'Chat Message'

Chat with Another Client
- Client Open Socket with Server
- Client Send 'LOGIN' Message to Server
- Client Send Message 'CLIENT1 == Message for Client'
- Server check if CLIENT1 is connected or not.
- If Client1 is connected send this message to him/her, otherwise respond user is not present

Chat Client


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ChatClient {
public static void main(String[] args) {

try {
Socket client = new Socket("127.0.0.1", 6666);
// Here you are connected
BufferedOutputStream out = new BufferedOutputStream(
client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(
client.getInputStream());
BufferedInputStream keyboard = new BufferedInputStream(System.in);

byte[] bKey = new byte[1024];
// byte[] bIn = new byte[1024];
// byte[] bOut = new byte[1024];

while (true) {
int readKey = keyboard.read(bKey); // Read from Keyboard
out.write(bKey, 0, readKey); // Write to client
out.flush();
}
// int readClient = in.read(bIn); // Input Buffer from Client
// System.out.println("Client: " + new String(bIn, 0, readClient));
} catch (IOException e) {
e.printStackTrace();
}

}

}

Chat Server


package com.evs.objava33.class10;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(6666);
// You ensured that there isn't any server socket
Socket client = server.accept();
System.out.println("New Client: " + client);
// Here you are connected
BufferedOutputStream out = new BufferedOutputStream(
client.getOutputStream());
BufferedInputStream in = new BufferedInputStream(
client.getInputStream());
BufferedInputStream keyboard = new BufferedInputStream(System.in);

byte[] bKey = new byte[1024];
byte[] bIn = new byte[1024];
byte[] bOut = new byte[1024];

// int readKey = keyboard.read(bKey); // Read from Keyboard
// out.write(bKey, 0, readKey); // Write to client
// out.flush();
while (true) {
int readClient = in.read(bIn); // Input Buffer from Client
System.out.println("Client: ("
+ new java.util.Date(System.currentTimeMillis())
+ "): " + new String(bIn, 0, readClient));
}
} catch (IOException e) {
e.printStackTrace();
}
}

}