{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

project1-2 - Project 1 A test driven perspec1ve Test driven...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Project 1 A test driven perspec1ve Test driven development Your program is good enough if it passed all tests Usually this is done by using automated test system Although you can use [email protected] tests as well Give you a [email protected] idea what you program should do before start to program Test driven development Incrementally develop your program You can have different way to incrementally program Example start with a top down approach Which means step 1: Starts with a server and a client They run Step 1 test Server test code public static void main(String args) { } SipServer server = new SipServer(); server.start(); Client Test code public static void main(String args) { } SipClient server = new SipClient(); SipClient.start(); Step 1 fix Implementa1on is trivia Everyone knows how to create class and objects. Right? Step 2 test Client should be the one that ini1ate a communica1on (C/S model) public static void main(String args) { } SipClient server = new SipClient(); SipClient.send(); TEST: the client should send a string through UDP Step 2 fix UDP: datagram socket programming Create a client socket, create a packet, send Fix: what's the IP address you are sending to? Get IP of the host machine: ipconfig/ifconfig Create InetAddress from IP string Fix: How to convert String to byte array? Side note: character encoding Please use ISO-8859-1 for this project. // Create the encoder and decoder for ISO-8859-1 Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); try { // Convert a string to ISO-LATIN-1 bytes in a ByteBuffer // The new ByteBuffer is ready to be read. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("a string")); // Convert ISO-LATIN-1 bytes in a ByteBuffer to a character // ByteBuffer and then to a string. // The new ByteBuffer is ready to be read. CharBuffer cbuf = decoder.decode(bbuf); String s = cbuf.toString(); } catch (CharacterCodingException e) { } } Step 2 fix Fix: How to handle excep1ons? Try... catch... finally try { clientSocket = new DatagramSocket(); socket.send(); } catch (SocketException e) { e.printStackTrace(); } finally{ socket.close(); } Throw Handle all expected excep1ons gracefully Step 2 fix DatagramSocket clientSocket; try { clientSocket = new DatagramSocket(); byte sendData = msg.getBytes(); InetAddress IPAddress; IPAddress = InetAddress.getByName(ip); DatagramPacket sendPacket = new DatagramPacket(...); clientSocket.send(sendPacket); } catch { } Step 3 test Test: s1ll fails, sending data is not received. Create the server For tes1ng purpose, create a logging func1on you find convenient. Print out the received data (decoded) Step 3 fix while (this.isRunning) { int bufsize = sock.getReceiveBufferSize(); byte message = new byte[bufsize]; DatagramPacket packet = new DatagramPacket(message, bufsize); sock.receive(packet); String receivedString = new String(packet.getData()); //System.out.println(receivedString); } Step 4 TEST: Client send a client id with the message Whenever server got a message, print out all the history message from the same client (same id) Step 5 test Server test code public static void main(String args) { } SipStack stack1= new SipStack(); stack1.start(); Client Test code public static void main(String args) { } SipStack stack2= new SipStack (); stack2.start(); stack2.send(whatever); Step 5 fix The server is a while(true) loop, remember?How to handle send code when you program is looping? Threading : put server code in a separate thread. Keep your class structure! Add more class. public class ServerThread implements Runnable { } public void run() { while (this.isRunning) { } } Step 6 TEST: Let's do the SIP protocol. Client send test messages, server should responds with correct answer. FIX: state machine. Same state machine code, keep a state for each dialog . State classes for each state? Or state field in each dialog class? Abstract class State{ Abstract void Run();} Init_state extends State{ Void run(){ Process incoming messages} } Class SipStack{ State current_state;} Step 7 Test: SipListener interface SipStack hold a SipLisener reference Fix: The interface code is trivia. Call the SipListener.handleSession The actual interface is up to you. Side note: observer [email protected] Step 8 TEST: user program SipApp(){ serverStack = new ServerStack(selfPort); serverStack .addListener(this); serverStack .start(); } FIX: now complete your tes1ng program with all kinds of scenarios. Create a test mode/app, let you see the current state, manually send messages. ...
View Full Document

{[ snackBarMessage ]}