01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

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.

merging changes<br />

Most version control tools will try<br />

and solve problems for you<br />

Suppose you had a version control system in place before the great BeatBox debacle<br />

<strong>of</strong> ’08. You’d check in your code (with commit) to implement Send Poke, and then<br />

Bob would change his code, and try to commit his work on Send Picture:<br />

Bob tries to check in his code...<br />

public class RemoteReader implements Runnable {<br />

boolean[] checkboxState = null;<br />

String nameToShow = null;<br />

Object obj = null;<br />

public void run() {<br />

try {<br />

while ((obj = in.readObject()) != null) {<br />

System.out.println("got an object from server");<br />

System.out.println(obj.getClass());<br />

String nameToShow = (String) obj;<br />

checkboxState = (boolean[]) in.readObject();<br />

if (nameToShow.equals(PICTURE_START_SEQUENCE)) {<br />

receiveJPEG();<br />

}<br />

else {<br />

otherSeqsMap.put(nameToShow, checkboxState);<br />

listVector.add(nameToShow);<br />

incomingList.setListData(listVector);<br />

// now reset the sequence to be this<br />

}<br />

} // close while<br />

} catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

} // close run<br />

} // close inner class<br />

192 Chapter 6<br />

public class RemoteReader implements Runnable {<br />

boolean[] checkboxState = null;<br />

String nameToShow = null;<br />

Object obj = null;<br />

public void run() {<br />

try {<br />

while ((obj = in.readObject()) != null) {<br />

System.out.println("got an object<br />

from server");<br />

System.out.println(obj.getClass());<br />

String nameToShow = (String) obj;<br />

checkboxState = (boolean[]) in.readObject();<br />

if (nameToShow.equals(PICTURE_START_<br />

SEQUENCE)) {<br />

receiveJPEG();<br />

}<br />

else {<br />

otherSeqsMap.put(nameToShow, checkboxState);<br />

listVector.add(nameToShow);<br />

incomingList.setListData(listVector);<br />

// now reset the sequence to be this<br />

}<br />

} // close while<br />

} catch (Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

} // close run<br />

Bob’s picture sending<br />

implementation<br />

...but quickly runs into a problem.<br />

Bob’s code<br />

svn commit -m "Added pictures."<br />

You and Bob both made changes<br />

to the same file; you just got<br />

yours into the repository first.<br />

// ... the code below is from BeatBoxFinal.java buildGUI() ...<br />

JButton sendIt = new JButton("sendIt");<br />

sendIt.addActionListener(new MySendListener());<br />

buttonBox.add(sendIt);<br />

JButton sendPoke = new JButton("Send Poke");<br />

sendPoke.addActionListener(new MyPokeListener());<br />

buttonBox.add(sendPoke);<br />

userMessage = new JTextField();<br />

buttonBox.add(userMessage);<br />

// ... this is new code we need to add to BeatBoxFinal.java ...<br />

public class MyPokeListener implements ActionListener {<br />

public void actionPerformed(ActionEvent a) {<br />

//<br />

We'll create an empty state array here<br />

boolean[] checkboxState = new boolean[256];<br />

try<br />

{<br />

out.writeObject(POKE_SEQUENCE);<br />

out.writeObject(checkboxState);<br />

}<br />

catch (Exception ex) { System.out.println("Failed to poke!"); }<br />

}<br />

}<br />

public class RemoteReader implements Runnable {<br />

boolean[] checkboxState = null;<br />

String nameToShow = null;<br />

Object obj = null;<br />

public void run() {<br />

try {<br />

while((obj=in.readObject()) != null) {<br />

System.out.println("got an object from server");<br />

System.out.println(obj.getClass());<br />

String nameToShow = (String) obj;<br />

checkboxState = (boolean[]) in.readObject();<br />

if (nameToShow.equals(POKE_START_SEQUENCE)) {<br />

playPoke();<br />

nameToShow = "Hey! Pay attention.";<br />

}<br />

otherSeqsMap.put(nameToShow, checkboxState);<br />

listVector.add(nameToShow);<br />

incomingList.setListData(listVector);<br />

} // close while<br />

} catch(Exception ex) {ex.printStackTrace();}<br />

} // close run<br />

private void playPoke() {<br />

Toolkit.getDefaultToolkit().beep();<br />

}<br />

} // close inner class<br />

Here’s your code—safe and<br />

sound in the repository.<br />

The code on the server,<br />

with your changes<br />

Bob’s BeatBox.java BeatBox.java<br />

Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!