Last active
December 16, 2015 16:39
-
-
Save tolinwei/5464429 to your computer and use it in GitHub Desktop.
The 4th assignment of Computer Network: My Routing Protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
//net.InetAddress; | |
//net.DatagramPacket; | |
//net.DatagramSocket; | |
import java.util.*; | |
import java.io.*; | |
//io.File; | |
class Receiving extends Thread { | |
//constructor | |
private String ipAddr; | |
private String broadcastAddr; | |
public Receiving(String ia, String ba) { | |
ipAddr = ia; | |
broadcastAddr = ba; | |
} | |
public void run() { | |
try { | |
System.out.println("Begin Receiving!"); | |
//keep receiving | |
//receive data | |
byte[] buffer = new byte[65535]; | |
DatagramSocket receiver = new DatagramSocket(12001); | |
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); | |
while(true) { | |
receiver.receive(packet); | |
String stringBuffer = new String(packet.getData(), 0, packet.getLength()); | |
//deal only when receive non-self packet | |
if(!stringBuffer.equals(MyRoutingProto.tempString)) { | |
//System.out.println("received: " + stringBuffer);//Should not be printed if run on one machine | |
//update native table | |
String[] info = stringBuffer.split(":"); | |
//info[0] is "hello", info[3] is useless sequence number | |
if(!MyRoutingProto.disTable.containsKey(info[1])) { | |
MyRoutingProto.disTable.put(info[1], new Integer(1)); | |
} else {//existed!!! | |
if(new Integer(1).compareTo(MyRoutingProto.disTable.get(info[1])) == -1) { | |
MyRoutingProto.disTable.put(info[1], new Integer(1)); | |
} | |
} | |
//iterately the String[] to deal with the left part: 0.itself, 1.add non-existed, 2.compare to the existed ones | |
for(int i = 3; i <= info.length - 1; i++) { | |
//!!forgot to add 1!!! | |
String[] keyValue = info[i].split(","); | |
if(ipAddr.equals(keyValue[0])) { | |
//0: pass itself | |
//do nothing | |
} else if(!MyRoutingProto.disTable.containsKey(keyValue[0])) { | |
//1 | |
MyRoutingProto.disTable.put(keyValue[0], Integer.valueOf(Integer.valueOf(keyValue[1]).intValue() + 1)); | |
} else { | |
//2 | |
if(Integer.valueOf((Integer.valueOf(keyValue[1]).intValue() + 1)).compareTo(MyRoutingProto.disTable.get(keyValue[0])) == -1) { | |
//update, write directly | |
MyRoutingProto.disTable.put(keyValue[0], Integer.valueOf(Integer.valueOf(keyValue[1]).intValue() + 1)); | |
} | |
} | |
} | |
StringBuffer sf = new StringBuffer(); | |
//Show the new table | |
System.out.println("** My table: " + ipAddr + " **"); | |
sf.append("** My table: " + ipAddr + " **"); | |
sf.append("\n"); | |
Iterator iterator = MyRoutingProto.disTable.keySet().iterator(); | |
while(iterator.hasNext()) { | |
Object key = iterator.next(); | |
System.out.println(" -- " + String.valueOf(key) + " -- " + MyRoutingProto.disTable.get(key)); | |
sf.append(" -- " + String.valueOf(key) + " -- " + MyRoutingProto.disTable.get(key)); | |
sf.append("\n"); | |
} | |
//Print new table | |
File file = new File("/tmp/topo_" + ipAddr + ".out"); | |
FileWriter fw = new FileWriter(file); | |
fw.write(sf.toString(), 0, sf.length()); | |
fw.close(); | |
} | |
} | |
} catch(Exception e) { | |
System.out.println(e); | |
} | |
} | |
} | |
class Broadcasting extends Thread{ | |
private String ipAddr; | |
private String broadcastAddr; | |
public Broadcasting(String ia, String ba) { | |
ipAddr = ia; | |
broadcastAddr = ba; | |
} | |
public void run() { | |
//loop and cast every 10 minutes; | |
System.out.println("Begin Broadcasting!"); | |
int i = 1; | |
try { | |
DatagramSocket sender = new DatagramSocket(); | |
sender.setBroadcast(true);//function unknown | |
byte[] data = null; | |
/* | |
String str1 = "1234"; | |
String str2 = ""; | |
byte[] b; | |
b = str1.getBytes(); | |
str2 = String.valueOf(b); | |
*/ | |
//data = i.toString().getBytes(); | |
/* | |
String s = String.valueOf(i); | |
String s = Integer.toString(i); | |
*/ | |
while(true) { | |
String stringData = "hello:" + ipAddr + ":" + i; | |
Iterator iterator = MyRoutingProto.disTable.keySet().iterator(); | |
while(iterator.hasNext()) | |
{ | |
Object key = iterator.next(); | |
stringData += ":" + String.valueOf(key) + "," + MyRoutingProto.disTable.get(key); | |
} | |
//System.out.println("sent: " + stringData); | |
MyRoutingProto.tempString = stringData; | |
data = stringData.getBytes(); | |
//build the packet, content: data | |
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName(broadcastAddr), 12001); | |
sender.send(packet); | |
i++; | |
Thread.sleep(10000); | |
} | |
} catch(Exception e) { | |
System.out.println(e); | |
} | |
} | |
} | |
public class MyRoutingProto { | |
public static HashMap<String, Integer> disTable; | |
public static String tempString; | |
public static void main(String[] args) { | |
//System.out.println("Hello World!"); | |
disTable = new HashMap<String, Integer>(); | |
tempString = null; | |
String localAddr = null; | |
if (args.length != 2) | |
{ | |
System.out.println("Must input the IP and broadcast Address!"); | |
return; | |
} | |
String ipAddr = args[0]; | |
String broadcastAddr = args[1]; | |
/* | |
try { | |
//remark | |
//getByName():return InetAddress by hostname, //or ip? | |
//getByAddress(byte[] addr) | |
InetAddress local = InetAddress.getLocalHost();//Return InetAddress of Local | |
localAddr = local.getHostAddress(); | |
System.out.println("MyRoutingPro for " + localAddr + " start!"); | |
} catch(Exception e) { | |
System.out.println("Unknown Host!"); | |
} | |
if (!localAddr.equals(ipAddr)) | |
{ | |
System.out.println("Wrong IP Address!"); | |
return; | |
} | |
*/ | |
try { | |
//create second thread for keep receiving String | |
new Receiving(ipAddr, broadcastAddr).start(); | |
//Create one thread for broadcast every 10 seconds | |
new Broadcasting(ipAddr, broadcastAddr).start(); | |
} catch(Exception e) { | |
System.out.println("Create thread wrong!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment