Sunday, February 28, 2010

Enumeration in Java5


package enumjava5;

public enum Color {
RED(255, 0, 0){
public String getMood(){
return "live";
}
},
GREEN(0, 255, 0){
public String getMood(){
return "rest";
}

},
BLUE(0, 0, 255){
public String getMood(){
return "sleep";
}
};

Integer red;
Integer green;
Integer blue;

private Color(Integer red, Integer green, Integer blue){
this.red = red;
this.green = green;
this.blue = blue;
}

public String getCode(){
return red + ", " + green + ", " + blue;
}

public abstract String getMood();

@Override
public String toString(){
return "Color #" + this.ordinal() + ", " + this.name();
}
}
  • Use keyword enum - public enum Color
  • Constuctor is private.
  • You can implements yours method: String getCode()
  • You can implements yours abstract method: abstract String getMood()
  • You can use heritage method: String name(), int ordinal(), <T>[] values(), <T> valueOf(String)

package enumjava5;

public class Main {

public static void main(String[] args) {

for (Color color : Color.values()) {

System.out.println("name: " + color.name() + ", code: " + color.getCode() + ", mood: " + color.getMood() + ", asString: " + color.toString());
}

}
}

output:


name: RED, code: 255, 0, 0, mood: live, asString: Color #0, RED
name: GREEN, code: 0, 255, 0, mood: rest, asString: Color #1, GREEN
name: BLUE, code: 0, 0, 255, mood: sleep, asString: Color #2, BLUE

You can use inner enum:


package enumjava5;

public class Outter{

public enum Color{
RED, GREEN, BLUE;
}

public static void main(String[] args){
for (Color m: Color.values()){
System.out.println( m.name() );
}
}
}

Thursday, February 4, 2010

IPv6

IPv6 is not compatible with IPv4.

Address is assign to interface.

Interface has more addresses: e.g. loopback, multicast, local link, global, ...

The length of header is constant. It can use extends header. Extend header have specified order.

Don´t use checksum.

Kinds of address:

unicast
for interface
multicast
for group of computer
anycast
for first computer in group

Don´t use broadcast. In place of broadcast it use multicast.


Notation of IPv6 address

IPv4
147.230.49.73
length 32b
4 x 8b
decimal
IPv6
fedc:ba98:7654:3210:fedc:ba98:7654:3210
length 128b
8 x 16b
hexadecimal

Shortening IPv6 address

  • 0123:0000:0000:0000:fedc:ba98:7654:3210
  • 123:0:0:0:fedc:ba98:7654:3210
  • 123::fedc:ba98:7654:3210 (allow only one "::" in address)

Mapping IPv4 address to IPv6

147.230.49.73
::ffff:93e6:3149 (147=>93, 230=>e6, 49=>31, 73=>49)
::ffff:147.230.49.73

Range of IPv6

  • ::1/128 loopback
  • 2000::/3 Global Unicast
  • FC00::/7 Unique Local Unicast
  • FE80::/10 Link Local Unicast
  • FF00::/8 Multicast

Global Unicast

|_ _ _|_|_ _ _ _|
48b - Public NET topology (from provider)
16b - Local SUBNET topology (your net)
64b - Interfaces (your interfaces)

Automatic IPv6 configuration

  1. Create local link address from MAC address. (fe80::/10 + MAC)
  2. Check collision.
  3. Waiting for router info about networks or ask about router.
  4. Set network parameters.

Security

Ipv6 mandatory use IPsec. It offers authentication, encryption, tunnelling.