Montag, 14. Dezember 2009

simpler chat server and client in golang

server:

package main

import (
"fmt";
"net";
"log";
"os";
"container/list";
"strings";
"bytes";
"flag";
)

// flag for debuging info. or a simple log
var debug = flag.Bool("d", false, "set the debug modus( print informations )")

type ClientChat struct {
Name string; // name of user
IN chan string; // input channel for to send to user
OUT chan string; // input channel from user to all
Con *net.Conn; // connection of client
Quit chan bool; // quit channel for all goroutines
ListChain *list.List; // reference to list
}

// read from connection and return true if ok
func (c *ClientChat) Read(buf []byte) bool{
nr, err := c.Con.Read(buf);
if err!=nil {
c.Close();
return false;
}
Log("Read(): ", nr, " bytes");
return true;
}

// close the connection and send quit to sender
func (c *ClientChat) Close() {
c.Quit<-true;
c.Con.Close();
c.deleteFromList();
}

// compare two clients: name and network connection
func (c *ClientChat) Equal(cl *ClientChat) bool {
if bytes.Equal(strings.Bytes(c.Name), strings.Bytes(cl.Name)) {
if c.Con == cl.Con {
return true;
}
}
return false;
}

// delete the client from list
func (c *ClientChat) deleteFromList() {
for e := c.ListChain.Front(); e != nil; e = e.Next() {
client := e.Value.(ClientChat);
if c.Equal(&client) {
Log("deleteFromList(): ", c.Name);
c.ListChain.Remove(e);
}
}
}

// func Log(v ...): loging. give log information if debug is true
func Log(v ...) {
if *debug == true {
ret := fmt.Sprint(v);
log.Stdoutf("SERVER: %s", ret);
}
}

// func test(): testing for error
func test(err os.Error, mesg string) {
if err!=nil {
log.Stderr("SERVER: ERROR: ", mesg);
os.Exit(-1);
} else
Log("Ok: ", mesg);
}

// handlingINOUT(): handle inputs from client, and send it to all other client via channels.
func handlingINOUT(IN <-chan string, lst *list.List) {
for {
Log("handlingINOUT(): wait for input");
input := <-IN; // input, get from client
// send to all client back
Log("handlingINOUT(): handling input: ", input);
for value := range lst.Iter() {
client := value.(ClientChat);
Log("handlingINOUT(): send to client: ", client.Name);
client.IN<- input;
}
}
}



// clientreceiver wait for an input from network, after geting data it send to
// handlingINOUT via a channel.
func clientreceiver(client *ClientChat) {
buf := make([]byte, 2048);

Log("clientreceiver(): start for: ", client.Name);
for client.Read(buf) {

if bytes.Equal(buf, strings.Bytes("/quit")) {
client.Close();
break;
}
Log("clientreceiver(): received from ",client.Name, " (", string(buf), ")");
send := client.Name+"> "+string(buf);
client.OUT<- send;
for i:=0; i<2048;i++ {
buf[i]=0x00;
}
}

client.OUT <- client.Name+" has left chat";
Log("clientreceiver(): stop for: ", client.Name);
}

// clientsender(): get the data from handlingINOUT via channel (or quit signal from
// clientreceiver) and send it via network
func clientsender(client *ClientChat) {
Log("clientsender(): start for: ", client.Name);
for {
Log("clientsender(): wait for input to send");
select {
case buf := <- client.IN:
Log("clientsender(): send to \"", client.Name, "\": ", string(buf));
client.Con.Write(strings.Bytes(buf));
case <-client.Quit:
Log("clientsender(): client want to quit");
client.Con.Close();
break;
}
}
Log("clientsender(): stop for: ", client.Name);
}

// clientHandling(): get the username and create the clientsturct
// start the clientsender/receiver, add client to list.
func clientHandling(con *net.Conn, ch chan string, lst *list.List) {
buf := make([]byte, 1024);
con.Read(buf);
name := string(buf);
newclient := &ClientChat{name, make(chan string), ch, con, make(chan bool), lst};

Log("clientHandling(): for ", name);
go clientsender(newclient);
go clientreceiver(newclient);
lst.PushBack(*newclient);
ch<- name+" has joinet the chat";
}

func main() {
flag.Parse();
Log("main(): start");

// create the list of clients
clientlist := list.New();
in := make(chan string);
Log("main(): start handlingINOUT()");
go handlingINOUT(in, clientlist);

// create the connection
netlisten, err := net.Listen("tcp", "127.0.0.1:9988");
test(err, "main Listen");
defer netlisten.Close();

for {
// wait for clients
Log("main(): wait for client ...");
conn, err := netlisten.Accept();
test(err, "main: Accept for client");
go clientHandling(&conn, in, clientlist);
}
}


Server has three part which are running as goroutines and communicate via channels.
1) handlingINOUT() simple wait for input of clientreceiver() and send to all clientsender() which are in the list.
2) clientreceiver() wait for his data from client via networkconnection and send it to a inputchannel to handlingINOUT
3) clientsender() wait for data from channel and send it to client

every client connection get a his own clientreceiver/sender and a list entry. on disconnection the list entry will be deleted.


client:

package main

import (
"fmt";
"net";
"log";
"os";
"bytes";
"bufio";
"strings";
"time";
"flag";
)

var running bool; // global variable if client is running

var debug = flag.Bool("d", false, "set the debug modus( print informations )")

// func Log(v ...): loging. give log information if debug is true
func Log(v ...) {
if *debug == true {
ret := fmt.Sprint(v);
log.Stdoutf("CLIENT: %s", ret);
}
}

// func test(): testing for error
func test(err os.Error, mesg string) {
if err!=nil {
log.Stderr("CLIENT: ERROR: ", mesg);
os.Exit(-1);
} else
Log("Ok: ", mesg);
}

// read from connection and return true if ok
func Read(con *net.Conn) string{
var buf [4048]byte;
_, err := con.Read(&buf);
if err!=nil {
con.Close();
running=false;
return "Error in reading!";
}
str := string(&buf);
fmt.Println();
return string(str);
}

// clientsender(): read from stdin and send it via network
func clientsender(cn *net.Conn) {
reader := bufio.NewReader(os.Stdin);
for {
fmt.Print("you> ");
input, _ := reader.ReadBytes('\n');
if bytes.Equal(input, strings.Bytes("/quit\n")) {
cn.Write(strings.Bytes("/quit"));
running = false;
break;
}
Log("clientsender(): send: ", string(input[0:len(input)-1]));
cn.Write(input[0:len(input)-1]);
}
}

// clientreceiver(): wait for input from network and print it out
func clientreceiver(cn *net.Conn) {
for running {
fmt.Println(Read(cn));
fmt.Print("you> ");
}
}

func main() {
flag.Parse();
running = true;
Log("main(): start ");

// connect
destination := "127.0.0.1:9988";
Log("main(): connecto to ", destination);
cn, err := net.Dial("tcp", "", destination);
test(err, "dialing");
defer cn.Close();
Log("main(): connected ");

// get the user name
fmt.Print("Please give you name: ");
reader := bufio.NewReader(os.Stdin);
name, _ := reader.ReadBytes('\n');

//cn.Write(strings.Bytes("User: "));
cn.Write(name[0:len(name)-1]);

// start receiver and sender
Log("main(): start receiver");
go clientreceiver(&cn);
Log("main(): start sender");
go clientsender(&cn);

// wait for quiting (/quit). run until running is true
for ;running; {
time.Sleep(1*1e9);
}
Log("main(): stoped");
}


client start two goroutines: for getting input from stdin and send it to network. and get from network and print it out.

Freitag, 11. Dezember 2009

simple server/client

a simple server and client in golang.

server wait for a client and handle him, simple read and write, then close.

client connect to server, write then read to/from server.

server:

package main

import (
. "fmt";
"net";
"os"; "log";
"strings";
)

func test(err os.Error, mesg string) {
if err!=nil {
log.Stderr("SERVER: ERROR: ", mesg);
os.Exit(-1);
} else
log.Stdout("SERVER: OK: ", mesg);
}


func main() {
buf := make([] byte, 1500);

Println("starting server...");

netlisten, err := net.Listen("tcp", "127.0.0.1:9988");
test(err, "Listen");
defer netlisten.Close();

for{
Println("server wait for client ...");
con, err := netlisten.Accept();
test(err, "Accept for client");
Println("client has connect to server");

con.Read(buf);

Println("readed: ", string(buf));

con.Write(strings.Bytes("response from server"));
}
}


client:

package main

import (
. "fmt";
"net";
"os"; "log";
)

func test(err os.Error, mesg string) {
if err!=nil {
log.Stderr("CLIENT: ERROR: ", mesg);
os.Exit(-1);
} else
log.Stdout("CLIENT: OK: ", mesg);
}

func main() {
buf := make([] byte, 1500);

Println("starting client");
con, err := net.Dial("tcp", "", "127.0.0.1:9988");
test(err, "dialing");
defer con.Close();
Println("client has connected to server");

con.Write(strings.Bytes("das ist ein test"));

con.Read(buf);

Println("readed: ", string(buf));
}

Donnerstag, 10. Dezember 2009

Golang: crypto/aes

Here we test the golang aes encryption decryption. We simple encrypt and decrypt a text message with AES-128. That is a key with 16 byte length.

"NewCipher creates and returns a new Cipher. The key argument should be the AES key, either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256."


package main

import (
"crypto/aes";
. "fmt";
"os";
"strings";
)

func main() {
// a 32 byte long message
msg := "This is long message text. len32";
// some key, 16 Byte long
key := []byte{0x2c, 0x88, 0x25, 0x1a, 0xaa, 0xae, 0xc2, 0xa2, 0xaf, 0xe7, 0x84, 0x8a, 0x10, 0xcf, 0xe3, 0x2a};

Println("len of message: ", len(msg));
Println("len of key: ", len(key));
// create the new cipher
c, err := aes.NewCipher(key);
if err != nil {
Println("Error: NewCipher(%d bytes) = %s", len(key), err);
os.Exit(-1);
}

// now we convert our string into 32 long byte array
msgbuf := strings.Bytes(msg);
out := make([]byte, len(msg));

c.Encrypt(msgbuf[0:16], out[0:16]); // encrypt the first half
c.Encrypt(msgbuf[16:32], out[16:32]); // encrypt the second half

Println("len of encrypted: ", len(out));
Println(">> ", out);

// now we decrypt our encrypted text
plain := make([]byte, len(out));
c.Decrypt(out[0:16], plain[0:16]); // decrypt the first half
c.Decrypt(out[16:32], plain[16:32]); // decrypt the second half

Println("msg: ", string(plain));
}

output:

len of message: 32
len of key: 16
len of encrypted: 32
>> [0 173 125 174 152 165 226 181 32 25 196 177 81 243 134 79 55 152 215 72 239 85 123 219 82 30 231 89 185 194 67 203]
msg: This is long message text. len32

Mittwoch, 9. Dezember 2009

Container in Go: ring

Ring:

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.


package main

import (
. "fmt";
"container/ring";
"strings";
"bytes";
)

// print the elements of the ring
func PrintRing(r *ring.Ring) {
Println("------------------------------");
for value := range r.Iter() {
Println("value: ", value);
}
Println("------------------------------");
}

func main() {
// create a new ring with 10 items
r := ring.New(10);

Println("len of ring: ", r.Len());
PrintRing(r);

// set some value
r.Value = "value 1";
// take the next

r = r.Next();
r.Value = "value 2";

r = r.Next();
r.Value = "value 3";

r = r.Next();
r.Value = "value 4";

PrintRing(r);


// create a second ring with 3 item and set his value
r2 := ring.New(3);
r2.Value = "r2 value 1";
r2 = r2.Next();
r2.Value = "r2 value 2";
r2 = r2.Next();
r2.Value = "r2 value 3";
PrintRing(r2);

// merge ring2 into ring 1
r = r.Link(r2);
PrintRing(r);

// remove a value in ring
for p:=r.Next(); p!=r; p=p.Next() {
if p.Value != nil {
value := p.Value.(string);
if bytes.Equal(strings.Bytes(value),strings.Bytes("r2 value 2")) {
p.Value=nil;
}
}
}

PrintRing(r);
}

output:

len of ring: 10
------------------------------
value:
value:
value:
value:
value:
value:
value:
value:
value:
value:
------------------------------
------------------------------
value: value 4
value:
value:
value:
value:
value:
value:
value: value 1
value: value 2
value: value 3
------------------------------
------------------------------
value: r2 value 3
value: r2 value 1
value: r2 value 2
------------------------------
------------------------------
value:
value:
value:
value:
value:
value:
value: value 1
value: value 2
value: value 3
value: value 4
value: r2 value 3
value: r2 value 1
value: r2 value 2
------------------------------
------------------------------
value:
value:
value:
value:
value:
value:
value: value 1
value: value 2
value: value 3
value: value 4
value: r2 value 3
value: r2 value 1
value:
------------------------------

Montag, 7. Dezember 2009

Container in Go: list

List: (a doubly linked list)

The list is a double linked list of Elements. Element is an element in the linked list with Next() and Prev() pointer to other elements. Begining and ending of Elements are nil.


package main

import (
. "fmt";
"container/list";
"strings";
"bytes";
)

// print the elements of the list
func PrintList(l *list.List) {
Println("------------------------------");
for value := range l.Iter() {
Println("element: ", value);
}
Println("------------------------------");
}

func main() {
// create a new list
li := list.New();

// add elements at end
li.PushBack("list1");
li.PushBack("list2");

// add elements at begining
li.PushFront("list3");

PrintList(li);

// get the first element from the list
element := li.Front();
str := element.Value;
Println("value: ", str);

// add a element before the first element
li.InsertBefore("list4", element);

// add after the element
li.InsertAfter("list5", element);

PrintList(li);

// get the next element
element = element.Next();
Println("value: ", element.Value);

// move the first element to back
element = li.Front();
li.MoveToBack(element);

PrintList(li);

// remove the first element
element = li.Front();
li.Remove(element);

PrintList(li);

// find the element with "list2" and remove it
for element := li.Front(); element != nil; element = element.Next() {
// don't use li.Iter(), because the interface chanel
// is geting the Value from the element back. not the element.
value := element.Value.(string);

if bytes.Equal(strings.Bytes(value),strings.Bytes("list2")) {
li.Remove(element);
}
}

PrintList(li);

}


the output is:

------------------------------
element: list3
element: list1
element: list2
------------------------------
value: list3
------------------------------
element: list4
element: list3
element: list5
element: list1
element: list2
------------------------------
value: list5
------------------------------
element: list3
element: list5
element: list1
element: list2
element: list4
------------------------------
------------------------------
element: list5
element: list1
element: list2
element: list4
------------------------------
------------------------------
element: list5
element: list1
element: list4
------------------------------

Montag, 30. November 2009

misunderstanding on casting in go

huch, have needed one day to understand that a function is in really was a cast.
look:
path.Walk(p.String(), pathIter(c), nil);

you can think pathIter(c) is a function. but:

type pathIter chan<- Path

pathIter is a typedefinition. Therefore is pathIter(c) not a function with parameter c, rahter a casting from c (is a Path channel) to pathIter.

Sonntag, 29. November 2009

input from stdion in go


package main

import (
"os"; "fmt"; "bufio";
)

func main() {

fmt.Print("input your name: ");

br := bufio.NewReader(os.Stdin);

name, _ := br.ReadString('\n');

fmt.Println();
fmt.Println("your name is: ", name);

}



func NewReader(rd io.Reader) *Reader NewReader returns a new Reader whose buffer has the default size.

func (b *Reader) ReadString(delim byte) (line string, err os.Error) read until delim byte.
here we read until enter.

Samstag, 28. November 2009

urlescap in http


url := "http://blub.org/b.html?v1=23&v2=bla&v=sdl+sdo";
surl := http.URLEscape(url);

fmt.Println(url);
fmt.Println(surl);

url2 := "http://это.порусс.ки/здесь.хтмл";
surl2 := http.URLEscape(url2);

fmt.Println(url2);
fmt.Println(surl2);


output:
http://blub.org/b.html?v1=23&v2=bla&v=sdl+sdo
http://blub.org/b.html%3fv1%3d23%26v2%3dbla%26v%3dsdl%2bsdo

http://это.порусс.ки/здесь.хтмл
http://%d1%8d%d1%82%d0%be.%d0%bf%d0%be%d1%80%d1%83%d1%81%d1%81.%d0%ba%d0%b8/%d0%b7%d0%b4%d0%b5%d1%81%d1%8c.%d1%85%d1%82%d0%bc%d0%bb


http.URLUnescape(url string) do it back.

read a file and print the count of lines


package main

import (
"io";
"fmt";
"os";
"strings";
)

func main() {
// read the file in bytes array
b, err := io.ReadFile("readfile.go");
if err!=nil {
fmt.Println("error on read file");
os.Exit(-1);
}

// convert from bytes to string
strbuffer := string(b);

// split into lines
lines := strings.Split(strbuffer, "\n", 0);

// iterate each
for i, line := range lines {
fmt.Printf("%d: %s\n", i, line);
}
}

Freitag, 27. November 2009

strings package in golang


package main

import (
"strings";
"fmt";
)

func main() {
data := "bla blu blub";
fmt.Println(data);
// return: bla blu blub

// convert string in bytes
b := strings.Bytes(data);
fmt.Println(b); // print every byte as array
// return: [98 108 97 32 98 108 117 32 98 108 117 98]


// test if the string begin with the prefix
if strings.HasPrefix(data, "bl") {
fmt.Println("data: \"", data, "\" begin with the prefix \"bl\"");
}

// test if the string end with the suffix
if strings.HasSuffix(data, "blub") {
fmt.Println("data: \"", data, "\" end with \"blub\"");
}

// Index returns the index of the first instance of sep in s,
// or -1 if sep is not present in s.
fmt.Println("index: ", strings.Index(data, "blu"));
// return: index: 4

fmt.Println("index: ", strings.Index(data, "b"));
// return: index: 0
fmt.Println("last index: ", strings.LastIndex(data, "b"));
// return: last index: 11
// LastIndex return the last occurency of seeking string

// with Repeat you can repeat ;)
fmt.Println("r: ", strings.Repeat("x", 10));
// return: r: xxxxxxxxxx

// split the string in array of string
as := strings.Split("root:alsdfjl:230:/blub/blub:lalalala", ":", 0);

fmt.Println(as);
// return: [root alsdfjl 230 /blub/blub lalalala]

fmt.Println(strings.Join(as, "|"));
// return: root|alsdfjl|230|/blub/blub|lalalala

fmt.Println(strings.ToLower("UASFalsdfuADSFWE$$"));
// return: uasfalsdfuadsfwe$$

fmt.Println(strings.ToUpper("owenadsfoadsfj"));
// return: OWENADSFOADSFJ

fmt.Println("<", strings.TrimSpace(" asdf sdowe "), ">");
// return: < asdf sdowe >
}

base64 in golang


package main

import (
"encoding/base64";
"bytes";
"fmt";
)

func main() {
// create the buffer
bb := &bytes.Buffer{};
bbURL := &bytes.Buffer{};

// create two base64 encoder (standard and for url)
encoder := base64.NewEncoder(base64.StdEncoding, bb);
encoderURL := base64.NewEncoder(base64.URLEncoding, bbURL);

data := "hallo this is a test, a=23&var2=blabla+-?!%$/\\";
fmt.Println("data : ", data);

// to encode data, use Write([]bytes),
// therefore you must convert string to bytes
// with string.Bytes(d string)
encoder.Write([]byte(data));
encoder.Close();

encoderURL.Write([]byte(data));
encoderURL.Close();

// voila
fmt.Println("encoded Std: ", bb.String());
fmt.Println("encoded URL: ", bbURL.String());
}

Mittwoch, 25. November 2009

time in go


package main

import "fmt"
import "time"

func main() {

fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());

fmt.Println("sleep 1 second.");

time.Sleep(1*1000*1000*1000);

fmt.Println("time in seconds : ", time.Seconds());
fmt.Println("time in nanoseconds: ", time.Nanoseconds());

fmt.Println("wait sleep 1 second.");
before := time.Nanoseconds();
time.Sleep(1*1000*1000*1000);
after := time.Nanoseconds();
fmt.Println("difference of nanoseconds: ", (after-before));

t:=time.LocalTime();
fmt.Println("time.String(): ", t.String());
}


time.Seconds() get the time as unix time in seconds
time.Nanoseconds() dito
time.Sleep(x) sleep for x nanoseconds
to sleep 1 second do time.Sleep(1e9)


to get any type back from function
let look:

package main

import "fmt"
import "rand"
import "time"

func my() interface{} {

rand.Seed(time.Nanoseconds()/1000000);

switch rand.Intn(3) {
case 0: var i int; i=4; return i;
case 1: var f float; f=2.3; return f;
case 2: var s string; s="blub"; return s;
}
var b byte;
b='c';
return b;
}

func main() {
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
time.Sleep(1e9);
fmt.Println(my());
}


with func my() interface{} {...} we declare a function which return an interface. This is a void interface, which can implement all types. Thus we can back every typ back. The my() function simple init the rand and get randomly different types back.

Dienstag, 24. November 2009

golang networking

a simple get for http:

package main

import (
"io";
"strings";
"log";
"http";
"fmt";
)

func main() {
r, _, err := http.Get("http://www.google.com/robots.txt");
var b []byte;
if err == nil {
b, err = io.ReadAll(r.Body);
r.Body.Close();
}

if err != nil {
log.Stderr(err)
} else {
fmt.Println(string(b));
}
}

ok, it was not very simple.
r, _, err := http.Get("http://www.google.com/robots.txt");
get the Response in r. Response has: a StatusCode, Body and Header.

type Response struct {
Status string; // e.g. "200 OK"
StatusCode int; // e.g. 200
Header map[string]string;
Body io.ReadCloser;
}

Header is a simple string hash. Body is an interface to ReadCloser: all structures with Read and Close.
func Get(url string) (r *Response, finalURL string, err os.Error) is doing followings:
- do a redirection loop (maximal 10 times)
- parse the url
- send the request
- if should redirect, get the new location in header
- if should not redirect: return the result from send()

b, err = io.ReadAll(r.Body);
read the data from response body (which has a reader from connection) in a byte array.
fmt.Println(string(b));
printout the bytes as string

create a simple webserver

package main

import (
"http";
"io";
)

// hello world, the web server
func HelloServer(c *http.Conn, req *http.Request) {
io.WriteString(c, "hello, world!\n");
}

// call a html page
func MyServerPage(c *http.Conn, req *http.Request) {
http.ServeFile(c, req, "myhtml.html");
}

func main() {
http.Handle("/hello", http.HandlerFunc(HelloServer));
http.Handle("/my.html", http.HandlerFunc(MyServerPage));
err := http.ListenAndServe(":8080", nil);
if err != nil {
panic("ListenAndServe: ", err.String())
}
}

http.Handle("/hello", http.HandlerFunc(HelloServer));
set the hadler function HelloServer with "/hello".

err := http.ListenAndServe(":8080", nil);
start the TCP server on port 8080, that handle the registered handler.

io.WriteString(c, "hello, world!\n"); write a simple string.

http.ServeFile(c, req, "myhtml.html"); read the file and print it out.

Samstag, 21. November 2009

learning go

so there will be some golang tips.

 
type My struct {
a, b int;
name string;
}

create a struct with three variables, a and b are integers. name is a string. The struct name is My.


m := My{1,2,"ray"};
fmt.Println(m);

m:= XXX{inputs} create and initialize the struct to variable m.
Println(m) print out: {1 2 ray}.

to create you own methods for the stuct do: func (v *Mystruct) method(...) returntyps { ... }

func (m *My) String() string {
ret := fmt.Sprintf("a: %d, b: %d, name: %s\n", m.a, m.b, m.name);
return ret;
}

here we create the String() method for printing our struct as a string. Access variables are done through m.var.

to define a simple array of integers: a:=[3]int{1,2,3}
if you don't know the length: a:=[]int{1,2,3,4,5,1,2,3}

create an assoziative array or dictionary or hash: hash := map[string]int{"eins":1, "zwei":2, "drei":3}; where map[keytyp]valuetyp.

hash2 := map[string]string{"blub":"aodu", "a":"ousdf", "ouew":"gofdu"};

// iterate through the hash, range return the key and value of the hash.
for k, v := range hash2 {
fmt.Println("key: ",k," value: ",v);
}

print out:
key: blub value: aodu
key: ouew value: gofdu
key: a value: ousdf


Read a directory:

package main

import (
"io";
"fmt";
)

func main() {
dir, err := io.ReadDir("/home/ray/daten");

fmt.Println("err: ", err);
fmt.Println(dir);

for _, v := range dir {
fmt.Println(">>", v);
}
}


let look the source on src/pkg/io/utils.go:

// ReadDir reads the directory named by dirname and returns
// a list of sorted directory entries.
func ReadDir(dirname string) ([]*os.Dir, os.Error) {
f, err := os.Open(dirname, os.O_RDONLY, 0); // open you directory
if err != nil {
return nil, err
}
list, err := f.Readdir(-1); // f is a file handler
f.Close();
if err != nil {
return nil, err
}
dirs := make(dirList, len(list));
for i := range list {
dirs[i] = &list[i]
}
sort.Sort(dirs);
return dirs, nil;
}

func ReadDir(dirname string) ([]*os.Dir, os.Error) : ReadDir reads the directory named by dirname and returns a list of sorted directory entries.

func Open(name string, flag int, perm int) (file *File, err Error): Open opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. It returns the File and an Error, if any.

Open return a File pointer. internal Open called a syscall to open

func (file *File) Readdir(count int) (dirs []Dir, err Error): Readdir reads the contents of the directory associated with file and returns an array of up to count Dir structures, as would be returned by Stat, in directory order. Subsequent calls on the same file will yield further Dirs. A negative count means to read until EOF. Readdir returns the array and an Error, if any.

our io.ReadDir() open the dir, read it through the file, create a list, sort and return the dir list.


for _, v := range dir {
if v.IsDirectory() {
fmt.Println(v.Name," is directory");
}
}

print out all direcotries name.

more info about dir is in: src/pkg/os/types.go or http://golang.org/pkg/os/#Dir

Read a file

fileObj, err := os.Open( "echo.go", os.O_RDONLY, 0666 );
if err != nil {
log.Stderr( err );
return;
}

//open the file and test for errors. log.Stderr print the message with a timestamp as a log.

// create a buffered reader
r := bufio.NewReader( fileObj );

// read each line
for {
line, err := r.ReadString('\n');

Dienstag, 17. November 2009

some go libraries























PackagePurposeExamples
fmtformatted I/OPrintf, Sprintf
osOS interface Open, Read, Write
strconvnumbers <-> stringsAtoi, Atof, Itoa
iogeneric I/O Copy, Pipe
flagflags: --help etc.Bool, String
logevent logging Log, Logf, Stderr
regexpregular expressions Compile, Match
templateHTML, etc.Parse, Execute
bytesbyte arraysCompare, Buffer


further libs: bignum, crypto, encoding, exec, hash, http, image, json, net, math, rand, regexp, sort, syscall, xml, time, unicode,

go pakcages

package is a collection of source files begining with "package nameofpackage".


import "fmt"
// now useable fmt outputs
fmt.Print(...)

import my "fmt" // with my own name. (for purpose if namespace coliders ;)
my.Print(...)


each package can have an init() for initalization data (single threaded).

let see:
a.go

package a

import "fmt"

var A int

func init() {
A = 4;
fmt.Print("A init\n");
}


b.go

package b

import "fmt"

var B int

func init() {
B=15;
fmt.Print("B init\n");
}


mainab.go

package main

import (
"fmt";
"./a";
"./b";
)

func main() {
fmt.Print("call main()\n");
var x= a.A;
var y= b.B;
var z = x + y;
fmt.Print(z);
}


output:

./6.out
B init
A init
call main()


at first there will be called init. later the main.

"To build a program, the packages, and the files within them, must be compiled in the correct order.
Package dependencies determine the order in which to build packages. "

defer in go

"The defer statement executes a function (or method)
when the enclosing function returns. The arguments
are evaluated at the point of the defer; the function
call happens upon return." GocourseDay1.pdf

leet look folowing programm:

package main

import "fmt"

func b() {
fmt.Printf("called b()\n");
}

func a() int {
fmt.Printf("start a()\n");
defer b();
fmt.Printf("ended a()\n");
return 0;
}

func main() {
fmt.Printf("start main\n");
fmt.Printf("call a()\n");
a();
fmt.Printf("after a()\n");
fmt.Printf("\n");
}


a() call with defer b(). But the outprint show:

./6.out
start main
call a()
start a()
ended a()
called b()
after a()



we can see that b() will be called before a() return.

=> defer is useful for closing fds, unlocking mutexes, etc.

defer is simmilar to atexit() for main() in C.

futher example:

package main

import "fmt"

func trace(s string) string {

fmt.Print("entering:", s, "\n");
return s
}
func un(s string) {
fmt.Print("leaving:", s, "\n")
}
func a() {

defer un(trace("a"));
fmt.Print("in a\n")
}
func b() {

defer un(trace("b"));
fmt.Print("in b\n");
a()
}
func main() { b() }


the output is:

./6.out
entering:b
in b
entering:a
in a
leaving:a
leaving:b

as we can see: defer f1(f2()) f2() is called immediately but f1() is called at ending.

further go

Go doesn't have references.

all private functions begin with small character.
all public functions begin with big character.

To generate some Unit test:
name: mylib_test.go ( xxx_test.go )
the function must be called: TestXXX(t *testing.T) { ...
and import: "testing"

send Fatal error

if err != nil {
t.Fatalf("Myerror %q: %s", path, err)
}
// or
t.Errorf("bla bla %q ", path)


The T struct is:

type T struct {
errors string;
failed bool;
ch chan *T;
}


There are some functions XXXf are with format string

Fail(), Failed(): set the failed variable or ask them. (don't exit)
FailNow() set the fail and exit.

Log()/Logf(): set the errors string.
Error()/Errorf(): Log with Fail
Fatal()/Fatalf(): Log with FailNow


To test a package, write a set of Go source files
within the same package; give the files names of the
form *_test.go.

testing tool: gotest

func TestXxxx(t *testing.T)

create the file. later call gotest my_test.go

Montag, 16. November 2009

trapped go

we create first a file (package) Person
Person.go:

package Person

type Person struct {
vorname string;
name string;
alter int;
}

func NewPerson(v string, n string, a int) *Person {
return &Person{v, n, a}
}

func (pers *Person) String() string {
return "Name: "+pers.name+" Vorname: "+pers.vorname;
}


then the main package with main()

Main.go

package main

import (
prs "./Person";
"fmt";
)


func main() {

person := prs.NewPerson("blue", "blub", 42);


fmt.Printf("person is: \n");
fmt.Printf(person.String());
fmt.Printf("\n");
}


compile and link:
6g Person.go
6g Main.go
6l -o Main Main.6

execute: ./Main
>person is:
>Name: blub Vorname: blue

At first we must import our file as "./Person"

At second the function that are created for outside must begin with Capitalized Character.


package Person

type Person struct {
vorname string;
name string;
alter int;
}

func newPerson(v string, n string, a int) *Person {
return &Person{v, n, a}
}


Person.newPerson(...) doesnot work.

"For clients (importers) of the package, names must
be upper case to be visible: global variables,
functions, types, constants, plus methods and
structure fields for global variables and types.

const hello = "you smell" // package visible
const Hello = "you smell nice" // globally visible
const _Bye = "stinko!" // _ is not upper
"

Sonntag, 15. November 2009

Go

Here is some introduction to new Google programing language.

At first go has his design to build a fast compiling system programming language for fast application. There is no mess with Java or C#/C++. go has no inheritance, but interfaces.

simple tutorial can be found on golang.org

At first the question: why yet another language?
google need a language which is fast in compiling and runing. Has the advantages of typing but not overloaded wiht oop stuff.

simple example:

// define the namespace
package main

// import the formated print package
// import fmt "fmt";

// import a couple of packages
import (
"flag"; // flag package is a good replacament for main(int argc, char **args) and
// replace the argument calculation with getopt() and switch. then create
// a help function automatic.
"fmt";
)

// create a boolean variable from package flag and set the values
var blub = flag.Bool("g", false, "blub lubn blub")

// g => -g
// false => set it at first to false
// "blub..." => description in help switch


func main() {
flag.Parse();
// parse the arguments

// ask if the
if *blub {
fmt.Printf("blub was setted\n");
} else {
fmt.Printf("blub was not setted\n");
}
}


as you has see variables are defined with var

var v1 int;
var v2 string;
var v3 float;


for printing is there the package fmt: fmt.Printf(..)
then exists other methods as in C.

to compile:
x64:
6g file.go
6l file.6 -o file

for x86
8g file.go
8l file.8 -o file

to be continued ...

...

Mittwoch, 11. November 2009

cools news

Google new language Go link

coole eclipse code highstyling plugin: link

Dienstag, 10. November 2009

JFace snippets and Images

some information about Eclipse (3.3) images can be found here: shinkarenko.

JFaceSnippets is a collection of code snippets for JFace.

Donnerstag, 5. November 2009

Eclipse RCP Forms part one

Eclipse Forms allow you to achieve the Web look. Eclipse Editors for Manifest files (f.e) are coded in Forms.

tutorials links:
vogella tutorial about forms
forms tutorial 2
forms tutorial 3

Forms use Views:


public class FormView extends ViewPart {
public void createPartControl(Composite parent) {
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createForm(parent);
form.setText("Hello, Eclipse Forms");


it create the form where you can set other widgets.

Therefore forms use GridLayot, GridData, Buttons and other similar as SWT.


GridLayout layout = new GridLayout();
form.getBody().setLayout(layout);

layout.numColumns = 2;
GridData gd = new GridData();
gd.horizontalSpan = 2;
link.setLayoutData(gd);
Label label = new Label(form.getBody(), SWT.NULL);
label.setText("Text field label:");
Text text = new Text(form.getBody(), SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button button = new Button(form.getBody(), SWT.CHECK);
button.setText("An example of a checkbox in a form");
gd = new GridData();
gd.horizontalSpan = 2;
button.setLayoutData(gd);



Hyperlink link = toolkit.createHyperlink(form.getBody(), "Click here.", SWT.WRAP);
link.addHyperlinkListener(new HyperlinkAdapter() {
public void linkActivated(HyperlinkEvent e) {
System.out.println("Link activated!");
}
});

create a hyperlink as a HTML hyperlink.


for more information look tutorials

Montag, 2. November 2009

RCP Tree Viewer part 2

here is the continuation of part one.

tutorial to JFace TreeViewer very good details.


At first we change our staticaly views and root objects.
change in ViewPart1 (thus was it called my TreeViewer ViewPart)

private TreeViewer viewer;
private RootNames root;

public RootNames getTreeRoot() {
return root;
}

public TreeViewer getTreeViewer() {
return viewer;
}


now change the create the Command Add and Del and add them to menu. (see Vogella )


public class CommandAdd extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

System.out.println("start");
ViewPart1 view = (ViewPart1) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(ViewPart1.ID);

RootNames root = view.getTreeRoot();

HostsNames hn = new HostsNames("212.40.32.55", "212.40.32.55");
root.addChild(hn);
hn.addChild(new Commandos("syslog", "syslog"));
hn.addChild(new Commandos("top", "top -d 1"));
hn.addChild(new Commandos("network", "network"));

view.getTreeViewer().refresh();
return null;
}
}

public class CommandDel extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

ViewPart1 view = (ViewPart1) HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().findView(ViewPart1.ID);

RootNames root = view.getTreeRoot();

HostsNames hn = root.getChildren()[0];

Commandos c = hn.children.get(0);

hn.removeChild(c);

view.getTreeViewer().refresh();
return null;
}

}


Now we create a second View where we simple put some text.

The commands simple add and del some item of tree.

public class OutputView extends ViewPart {
public static final String ID = "zzz.zmytest.Tree.Views.OutputView";
public Text text;

@Override
public void createPartControl(Composite parent) {
text = new Text(parent, SWT.BORDER);
text.setText("Imagine a fantastic user interface here");
}

@Override
public void setFocus() {
}
}

we will simple alter the text.

selection Listener
selection Listener

we implement ISelectionListener:

public class OutputView extends ViewPart implements ISelectionListener{

@Override
public void createPartControl(Composite parent) {
...

getSite().getPage().addSelectionListener((ISelectionListener) this);
}

@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
text.setText(selection.toString());
}
}


add the view in the perspective:


public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);

layout.addStandaloneView(ViewPart1.ID, false, IPageLayout.LEFT, 0.25f, editorArea);
IFolderLayout folder = layout.createFolder("outputs", IPageLayout.TOP, 0.5f, editorArea);
folder.addPlaceholder(OutputView.ID + ":*");
folder.addView(OutputView.ID);

layout.getViewLayout(ViewPart1.ID).setCloseable(false);
}
}


set the tree view to selection provider:

public class ViewPart1 extends ViewPart {
...

public void createPartControl(Composite parent) {
...

// add to selection provider
getSite().setSelectionProvider(viewer);

}


improve the listener:

@Override
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection instanceof IStructuredSelection) {
Object obj = ((IStructuredSelection) selection).getFirstElement();
if (obj != null) {
if (obj instanceof HostsNames) {
HostsNames hn = (HostsNames)obj;
text.setText("hostname: "+hn.name);
} else if (obj instanceof Commandos) {
Commandos co = (Commandos)obj;
HostsNames hn = co.parent;
text.setText("commando: "+co.name+ " von host: "+hn.name);
}
}
}
}


the listener look for IStructuredSelection, get the object and test the object for our class instances.


Now we can add further views provider and listener without to handle the wired code.s

RCP Tree Viewer

here is some info about JFace TreeViewer.

The Model is worked with MVC


  • Viewer: TreeViewer, LabelProvider

  • Model: Your own model, ContentProvider. The Objects should be Singleton

  • Control: create the Model, change the Model, refresh Viewer



Create a RCP Application (for tutorials see: Vogella).

Add a view: Manifest -> Extensions -> All Extensions -> Add ( views)
give an ID, name and create a class.

At first create the Model. The Model is a tree with root and nodes.

root
\
node
\node
\
node
\node
\node


This example create a tree of hosts an commands for each host.

The root class. This object should be a singleton and have access to hostnames objects.
The hostnames object should have access to commands objects.


RootNames
/ | \
/ | \
host1 host2 host3
/ | \ | \ |
c1 c2 c3 c1 c2 command1


create package: xxx.model

The model package will be have: RootNames, HostsNames and Commands class models.


public class RootNames {
private static RootNames root=null;
public String name;
public ArrayList children;

private RootNames(String name) {
this.name=name;
children = new ArrayList();

}

public static RootNames getRootNames(String name) {
if(root!=null)
return root;
RootNames r = new RootNames(name);
root=r;
return root;
}

public String toString() {
return name;
}
}

RootNames is a singleton, has only a name and children of HostsNames. For simplicity we do the access public. Thus we don't need implement setter or getter.

Then we implement following help methods in RootNames:

public void addChild(HostsNames child) {
children.add(child);
child.parent = this;
}

public void removeChild(HostsNames child) {
children.remove(child);
child.parent = null;
}

public HostsNames[] getChildren() {
return (HostsNames[]) children.toArray(new HostsNames[children.size()]);
}

public boolean hasChildren() {
return children.size()>0;
}

- add/removeChild() add/remove a child in a list and set/unset the parent of child.
- getChildren() return an array of objects of children. Here a HostsNames array.

HostsNames:

public class HostsNames {
public String name;
public String host;
public RootNames parent;
public ArrayList children;

public HostsNames(String name, String host) {
this.name = name;
this.host = host;
children = new ArrayList();
}

public void addChild(Commandos child) {
children.add(child);
child.parent = this;
}

public void removeChild(Commandos child) {
children.remove(child);
child.parent = null;
}

public Commandos[] getChildren() {
return (Commandos[]) children.toArray(new Commandos[children.size()]);
}

public boolean hasChildren() {
return children.size()>0;
}

public String toString() {
return name;
}
}


HostsNames: have a name, hostname, parent to root and children of Commandos. And helpers for Children.

Commandos:

public class Commandos {
public String name;
public String commando;
public HostsNames parent;

public Commandos(String name, String commando) {
this.name = name;
this.commando = commando;
}

public String toString() {
return name;
}
}

Commandos have name, commando and reference to HostsNames via parent.


Implement Provider:

create package xxx.provider


public class ViewLabelProvider extends LabelProvider {

public String getText(Object obj) {
return obj.toString();
}

public Image getImage(Object obj) {
String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
if (obj instanceof HostsNames)
imageKey = ISharedImages.IMG_OBJ_FOLDER;
return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);
}
}

The Label Provider is a View in MVC. it give the name and the image of the Tree back, based on model.

Create a View Content Provider with implemented Interfaces: IStructuredContentProvider and ITreeContentProvider.


public class ViewContentProvider implements IStructuredContentProvider, ITreeContentProvider {

@Override
public Object[] getElements(Object inputElement) {
return null;
}

@Override
public void dispose() {
}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}

@Override
public Object[] getChildren(Object parentElement) {
return null;
}

@Override
public Object getParent(Object element) {
return null;
}

@Override
public boolean hasChildren(Object element) {
return false;
}
}


The Content Provider is the model part of MVC. It provide how the TreeViewer can access the model.
following methods must be implemented: getElements(), getParent(), getChildren(), hasChildren().

Implementation:

@Override
public Object[] getElements(Object parent) {
return getChildren(parent);
}
@Override
public Object[] getChildren(Object parent) {
if (parent instanceof RootNames) {
return ((RootNames)parent).getChildren();
}
if (parent instanceof HostsNames) {
return ((HostsNames)parent).getChildren();
}
return new Object[0];
}

@Override
public Object getParent(Object child) {
if (child instanceof HostsNames) {
return ((HostsNames)child).parent;
}
if (child instanceof Commandos) {
return ((Commandos)child).parent;
}
return null;
}

@Override
public boolean hasChildren(Object element) {
if (element instanceof RootNames)
return ((RootNames)element).hasChildren();
if (element instanceof HostsNames)
return ((HostsNames)element).hasChildren();
return false;
}

getElements and getChildren is here the same.

getChildren() return for RootNames or HostsNames his children list as an array. The same is do getParent() and hasChildren().


Now set the ViewPart:

private TreeViewer viewer;
private RootNames root;

@Override
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
root=ModelCreator.createModel();
viewer.setInput(root);
}

Create the TreeViewer.
Set our Content and Label Provider. Call the Model Creator.

ModelCreator:

public class ModelCreator {

public static RootNames createModel() {
RootNames root = RootNames.getRootNames("root");

HostsNames hn = new HostsNames("localhost", "localhost");
root.addChild(hn);
hn.addChild(new Commandos("syslog", "syslog"));
hn.addChild(new Commandos("top", "top -d 1"));

hn = new HostsNames("134.108.68.107", "134.108.68.107");
root.addChild(hn);
hn.addChild(new Commandos("syslog", "syslog"));
hn.addChild(new Commandos("top", "top -d 1"));

return root;
}
}

createModel() create a root with two hostnames and his commandos.

Then we must set the view in perspective

String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);

layout.addStandaloneView(ViewPart1.ID, false, IPageLayout.LEFT, 0.95f, editorArea);

layout.getViewLayout(ViewPart1.ID).setCloseable(false);


to add some context to the tree (here an example with a Command:

public class CommandAdd extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
System.out.println("blub");
RootNames root = ViewPart1.getTreeRoot();
System.out.println("blub: "+root);

HostsNames hn = new HostsNames("212.40.32.55", "212.40.32.55");
root.addChild(hn);
hn.addChild(new Commandos("syslog", "syslog"));
hn.addChild(new Commandos("top", "top -d 1"));
hn.addChild(new Commandos("network", "network"));

ViewPart1.getTreeViewer().refresh();

return null;
}
}


simple get the singleton and add some childs. Then refresh the view.

Mittwoch, 14. Oktober 2009

OSGi book summary part one

Summary for book "Die OSGI Service Platform-Eine Einführung mit Eclipse Equinox" link

The book is a good introduction to OSGi. It has 24 chapters, from OSGi introduction to OSGi and Equinox Standard Services. The book is divided in three part: part one: OSGi Framework, part two: OSGi Standard Services, part three: Equinox Services(Extensions)

Following chapters are discused in first part:
Bundles, Bundles lifcycles, Packages dependencies, OSGi Services, dynamic Services, Fragment Bundles, Security, Management of OSGi Service Platform, Packaging and Deployment


this blog summary the summary of the book:

* how to install Eclipse PDE (download eclipse-equinox-SDK-3.xxxx.zip)

* describe how OSGi work: bundles, layers, lifecycles, deployment, services, avaiable implementations(felix, equinox), example to start a http game services.


* Bundle Manifest have descriptive information about bundles, the manifest will be readed and interpreted by OSGi platform.
* Bundle-Classpath have locale paths
* Bundle-Activator is the start and stop point of a bundle with org.osgi.framework.BundleActivator
* BundleContext (parameter on start()/stop()), allow access to OSGi framework. Through context you can install new bundles, register listener, register and ask for services.

* Equinox console is a simpler Management Agent. You can ask for state of bundles and OSGi environment. A bundle has different states (installed, uninstalled, running, stoped)
* Activator Policies can change the behavior of bundles starts. (time, sequence, ressources). Lazy Activator set the bundle only start if other required him.
* It exists change of states with code too. (not only manifest-file)
* Bundle listener can notify you about state changes. Listener are registered with BundleContext.
* Bundle Cache save the states for persistence beyound the start()/stop() borders.

* For using other bundles you must explizit import/export you bundles in manifest-file.
* That is done with Export-Package and Import-Package labels.
* Required-Bundle specific the whole bundle. Import-Package only classes/packages. '*' import all class in a package.
* With Versioning you can specify which version of a same bundle the other bundle should use.
* With DynamicImport-Package you can import packages which doesn't exists yet.

* OSGi services are simple java objects (POJO Plain Old Java Objects). The services are registered to Service Registry with theirs class names. The services could quered by other bundles.
* The access to Services happen with ServiceReference and BundleContext
* On registry you can send some properties (which is a simple hash map)
* To get a reference: BundleContext.getServiceReferences() and the class name of service. Then you can use filter.
* With start leveln you can affect the start sequence of bundles.

* Sercice Tracker help you to work with one interface, independent from runtime/lifecycle. Tracker help dynamic react to services in/outgoing.
* ServiceTrackerCustomizer help you to react to different changes with call-back methods.
* Central OSGi Service Registry is the Whiteboard-Pattern and allow simple way the traditional listener-patern to replace.

* Fragments bundles expand the classpath of others bundles ( to avoid overloading of code/info for one bundle). It is a simple way to divide logic part from presentation(templates, languages).
* Fragmenst have no lifecycles (no Activators). manifest: Fragment-Host
* The Import/Export/Requirement information from Fragment Bundle will transfer to Host Bundle (which use the Fragment Bundle)
* Extension Bundles you can change the boot-classpath and expand it with additional features.

* Security in OSGi: local access can be setted to avoid execution in OSGI-INF/permissions.perm
* You can run only signed bundles or with certificates
* Conditional Permission Admin Service asign global authorization (Conditions)

* Management Agent implement an access to OSGi framework. For administrate framework from outside.
* It can be done by shell. or with programmatic.
* You can code your own commands with implement a CommandProvider with org.eclipse.osgi.framework.console.CommandProvider and register it to OSGi Service Registry.

* Plug-in Export Wizard is a simple facility to build bundles for extern world (and not only work with eclipse)
* For automatic build: ant4eclipse, Maven
* To start Eclipse Equinox: use org.eclipse.osgi_version.jar. Or start org.eclipse.core.runtime.adapptor.EclipseStarter from an Java application.
* With System properties or config.ini you can manipulate Equinox

Dienstag, 13. Oktober 2009

Scala test

http://www.scalatest.org/getting_started_with_feature_spec

Sonntag, 11. Oktober 2009

Python stuff

functions numbers

int(), long8), float(), round(), hex(), oct(), pow(), cmp()

s1+s2 merge
s1*n s1+s1+s1+s1+...

functions strings:
org(), chr(), str(), repr(), unicode(), len()

s.count(), s.encode(), s.endswith(), s.find(), s.isalnum(), s.iksalpha(), s.isdigi(), s.islower(), s.isspace(), s.join(), s.lstrip(), s.replace(), s.rfind(), s.rstrip(), s.split(), s.startswith(), s.strip(), s.swapcase()


List:
array=[1,2,3,4]
len(array)
array[1]="blub"
array[2]=["bla", [1,2,3], 'ups']

append(), extend(), count(), index(), insert(), remove(), pop(), reverse(), sort()


range()

range(4)
[0,1,2,3]
range(5,10,2]
[5,7,9]

Tuples
same as list but imutable

t1=(1,2,3)


indicies and slices


s[4:]
s[4:7]
s[:-7]
s[-4:]
s[:]
max(), min(), len()

a,b,c = [ 1, 2, 3 ]

del delete a varaible

a= 1
del a


assoziative lists, hash

h = { k:v, k2:v2, k3:v3 }
t={"peter":23450, "anna":39454, "blub":439549}
t["blub"]
items(), keys(), values(), has_key(), update()
t.has_key("anna")
len(), del()


None, type(), in-operator

if 2 in lists:
...
if not "a" in string:
...


Functions:

def p(n):
"bla bla, describe function for help"
for a in range(2,n):
...

p(3)


def fct(a, b="bla"):
...

fct(3, "lala")
fct(3)

several arguments with *

def h(g, *args):
for m in args:
...

h("bl", "blub", "abc", "dbc")


def h(**args):
if len(args==0): return
for n in args.keys():
print args[n]+" : "+ n

h(v1="blub", v2="aga")

def complexparameters(firma, delim="-", *address, **inventars):

def doNothing():
pass


functionale coding

add = lambda x,y: x+y
add(2,3)

map(ord, "string")
[115,116,114,...]

[ord(a) for a in "string"]

map(lambda x:x+1,(1,2,3))
map for tuple (1,2,3) x+1

def p(n):
for a in range(2,n):
if n%a == 0:
return 0
else:
return 1

filter(p, range(100))


reduce(function, collection)

a=5
exec("var" +str(a)+"="+str(a))
=> var5=5

docstrings:
def bla():
""" blal bla"""
...

bla.__doc__

Modules

JAVA stuff

read from input:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
int age = new Integer( br.readLine() ).intValue();

RUBY codes: code blocks and iterations

from "Ruby Cookbook" O'Reilly
for detailed information, look this book


[1,2,3].each { |i| puts i }

[1,2,3].each do |i|
if i%2 == 0
puts "="
else
puts "!"
end
end

1.upto 3 do |x| # same 1.upto(3)

1.upto(3) { |x| ... } # ok
1.upto 3 { |x| ... } # error


log = lambda { |str| puts "[LOG] #{str}" }
log.call("A test log message.")


{1=>2, 2=>4}.each { |k,v| puts "Key #{k}, value #{v}" }


def times_n(n)
lambda { |x| x * n }
end

times_ten = times_n(10)
times_ten.call(5) # => 50

circumference = times_n(2*Math::PI)
[1, 2, 3].collect(&circumference)
# => [6.28318530717959, 12.5663706143592, 18.8495559215388]


Clousure:
Every Ruby block is also a closure.

ceiling = 50
[1, 10, 49, 50.1, 200].select { |x| x < ceiling }
# => [1, 10, 49]

the var celing is context depended.

Lambda and Proc


block = { |x| puts x } # error
block = lambda { |x| puts x } # ok
block.call "bla bla!"


def my_lambda(&aBlock) # a block in function parameters
aBlock
end

b = my_lambda { puts "Hello World My Way!" }
b.call
b.class # => Proc

aBlock = Proc.new { |x| puts x }
aBlock = proc { |x| puts x }
aBlock = lambda { |x| puts x }


add_lambda = lambda { |x,y| x + y }
add_lambda.call(4,5,6) # ArgumentError: wrong number of arguments (3 for 2)

add_procnew = Proc.new { |x,y| x + y }
add_procnew.call(4,5,6) # => 9



Yielding


def myfunc
puts "1"
yield
puts "2"
yield
end

myfunc("3")
# 1
# 3
# 2
# 3


def repeat(n)
if block_given?
n.times { yield }
else
raise ArgumentError.new("I can't repeat a block you don't give me!")
end
end

repeat(4) { puts "Hello." }
# Hello.
# Hello.
# Hello.
# Hello.

repeat(4)
# ArgumentError: I can't repeat a block you don't give me!

Freitag, 9. Oktober 2009

read file in Scala

read file in scala:

for (file <- new File("/var/log/").listFiles) {


list a directory

if (!file.isDirectory) {

ask is file not a directory

simple print out of a file

import scala.io.Source

for {
(line) <- Source.fromFile("bla.txt").getLines
} print(line)


read a file in an iterator

scala> val lines = scala.io.Source.fromFile("/var/log/syslog")
lines: scala.io.Source = non-empty iterator

scala> lines
res5: scala.io.Source = non-empty iterator


read a file in a string

val lines = scala.io.Source.fromFile("/var/log/syslog").mkString

// for utf-8 encoding
val lines = scala.io.Source.fromFile("file.txt", "utf-8").getLines.mkString



or you can use Java API

val os = new BufferedOutputStream(new FileOutputStream("gen/" + file.getName))

Donnerstag, 8. Oktober 2009

Challenge: pattern syslog

Challenge:

filtern some patern in file /var/log/syslog
and print it out


Shell:
fgrep usb /var/log/syslog


Java:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class JavaFilters {
public static void main(String[] args) {
String path="/var/log/syslog";
try {
BufferedReader input = new BufferedReader(new FileReader(path));

String line;
try {
Pattern p = Pattern.compile("dhclient");
while (( line = input.readLine()) != null){
Matcher m = p.matcher(line);
if(m.find())
System.out.println("found:: "+line);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}


Scala:

import scala.io.Source
import java.util.regex._

var p = Pattern.compile("usb")

for ( line <- Source.fromFile("/var/log/syslog").getLines ){
var m = p.matcher(line);
if( m.find )
print("found::" + line)
}


Perl:

#!/usr/bin/perl

open(IN, "/var/log/syslog") or die "canot open file: $!";

@lines = <IN>;
close IN;

foreach $line(@lines) {
if( $line =~ /dhclient/) {
print "found:: $line";
}
}


Ruby:

#!/usr/bin/ruby

file = File.new("/var/log/syslog", "r") or die "canot open file"

while (line = file.gets)
if ( line =~ /usb/ )
print "found:: "+line
end
end


Python:

#!/usr/bin/python

try:
f = open('/var/log/syslog', 'r')

for line in f.readlines():
if( "usb" in line ):
print("found:: %s"%line)

except:
print "error in file"


PHP:

<?php
$file = "/var/log/syslog";
if (file_exists($file)) {
$lines = file($file);
foreach ($lines as $line_num => $line)
if( preg_match("/usb/", $line) )
echo "$line";
}
?>


C:

#include
#include
#include
#include
#include

FILE *file = NULL;

int main(int argc, char **argv)
{
char line[1024];
int err;
char err_str[200];
char results[500];

regex_t preg;
regmatch_t pmatch[100];

if( (regcomp(&preg, argv[1], 0)) != 0) {
printf("regcomp error\n");
return -1;
}

if((file = fopen("/var/log/syslog", "r")) != NULL) {
while (!feof(file)){
fgets(line, 1024, file);

if( regexec(&preg, line, preg.re_nsub, pmatch, 0) == 0) {
printf("found:: %s", line);
}
}
}

regfree(&preg);
return 0;
}

Dienstag, 6. Oktober 2009

learning Scala 3

this tutorial based on:
http://www.artima.com/scalazine/articles/steps.html


def max(x: Int, y: Int): Int = if (x < y) y else x


def max(x: Int, y: Int) -> define a function, with 2 parameters (integer)
: Int -> return is integer.
if (x < y) y else x -> function body



scala> def greet() = println("Hello, world!")
greet: ()Unit

scala> greet()
Hello, world!

scala> greet
Hello, world!


greet save a Unit -> a function


args.foreach(arg => println(arg))
args.foreach((arg: String) => println(arg))
args.foreach(println)

for (arg <- args)
println(arg)


scala> for (i <- 0 to 5)
| println(i)
0
1
2
3
4
5



val greetStrings: Array[String] = new Array[String](3)

create an array with 3 strings


greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"


important indexes are in brakes () not square brackets[]


for (i <- 0 to 2)
print(greetStrings(i))

learning Scala 2

this tutorial based on:
http://www.thomasknierim.com/104/scala/scala-tutorial-1/
http://www.thomasknierim.com/104/scala/scala-tutorial-2/
http://www.thomasknierim.com/104/scala/scala-tutorial-3/


object Hello {
def main(args: Array[String]) {
for(val arg: String <- args)
System.out.println("Hello, " + arg + "!");
}
}


for(val arg: String <- args) -> an array into each arg as String

similar: args.foreach(arg => println("Hello" + arg + "!"))
"=>" that can be read “with a given argument this do that”

args.foreach(println)


var decrease = (x: Int) => x - 1
decrease(11)


(x: Int) => x - 1 -> anonymous function declaration


var decrease = (x: Int) => x – something

something is not defined yet. decrease is depend on context -> clousure

In terms of computer science, a closure is a function that is evaluated and whose result depend on an outer context.

something = 10
decrease(100)


some examples for higher-order functions

scala> List("alpha", "beta", "crash").map(
p => p.substring(0,1))
res0: List[java.lang.String] = List(a, b, c)

scala> List(1, 2, 3) map (_ * 2)
res1: List[Int] = List(2, 4, 6)

scala> List("Tango", "Mango", "Bobo", "Gogo")
map (_.endsWith("go"))
res2: List[Boolean] = List(true, true, false, true)



List("alpha", "beta", "crash") -> create a list

map() -> map each list element to some condition/function

p => p.substring(0,1) p is the element p.substring(0,1) is the function

_ -> ist the element (similar to perl $_ )
(_*2) is equivalent to (i => i * 2)




object FileMatcher {

private val filesHere=(new java.io.File(".")).listFiles

private def filesMatching(matcher: String => Boolean)=
for (file <- filesHere; if matcher(file.getName))
yield file

def filesEnding(query: String)=
filesMatching(_.endsWith(query))
def filesContaining(query: String)=
filesMatching(_.contains(query))
def filesRegex(query: String)=
filesMatching(_.matches(query))
}

object Main {
def main(args: Array[String]) {
val query = args(0);
var matchingFiles = FileMatcher.filesEnding(query)
matchingFiles = FileMatcher.filesContaining(query)
matchingFiles = FileMatcher.filesRegex(query)
}
}


let look:

scala> (new java.io.File(".")).listFiles
res2: Array[java.io.File] = Array(./FrenchDate$.class, ./FrenchDate.class, ./Complex.scala, ./s1.class, ./s1$.class, ./FrenchDate.scala, ./Timer$$anonfun$main$1.class, ./s1.scala, ./Timer$.class, ./Hello.class, ./ComplexNumbers.class, ./Hello.scala, ./Hello$$anonfun$main$1.class, ./blog_scala2.txt, ./Timer.class, ./Hello$.class, ./Complex.class, ./blog_scala1.txt, ./ComplexNumbers.sca...


new java.io.File(".") use the java api and create a file object File.
listFiles is the same as listFiles() in java. in scala you don't need brakes.

for (file <- filesHere; if matcher(file.getName))
yield file

iterate all filesHere (files in path "."). if name match to query, yield the file. the file get in an array.


scala> var matchingFiles = FileMatcher.filesEnding(query)
matchingFiles: Array[java.io.File] = Array(./Complex.scala, ./FrenchDate.scala, ./s1.scala, ./Hello.scala, ./Main.scala, ./ComplexNumbers.scala, ./Timer.scala)


def filesEnding(query: String)=
filesMatching(_.endsWith(query))


define different functions


filesMatching(matcher: String => Boolean)

define the function with a matcher(String) which return Boolean

learning Scala

This tutorial based on:
http://www.scala-lang.org/docu/files/ScalaTutorial.pdf
http://www.thomasknierim.com/77/scala/scala-tutorial-2/



write a file s1.scala

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}


object -> declare a static singleton object
args: Array[String] -> a variable args as Array of Strings

compile and execute

$ scalac s1.scala
$ ls
s1.class s1$.class s1.scala


you get created standard java class files, but you must use java interpreter with setted parameters.


$ java s1
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
....

$ file /usr/bin/scala
$ /usr/bin/scala: POSIX shell script text executable
$ cat /usr/bin/scala
#!/bin/sh

JAVA_DIR=/usr/share/java
LIB_CLASSPATH=$JAVA_DIR/scala-library.jar:$JAVA_DIR/scala-compiler.jar:$JAVA_DIR/jline.jar

exec ${JAVACMD:=java} ${JAVA_OPTS:=-Xmx256M -Xms16M} \
-Xbootclasspath/a:$LIB_CLASSPATH \
-classpath .:$CLASSPATH:$LIB_CLASSPATH \
scala.tools.nsc.MainGenericRunner \
"$@"

$ scala s1
hallo


As you can see, the scala executable is a simple shell script, which start the java virtual machine with specific parameters and the scala library.


Create Date.scala

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._
object FrenchDate {
def main(args: Array[String]) {
val now = new Date
val df = getDateInstance(LONG, Locale.FRANCE)
println(df format now)
}
}








import java.util.{Date, Locale} import Date and Locale from java.util
import java.text.DateFormat._ DateFormat._ is similar to DateFormat.* in java
val declare an inmutable variable
val now = new Date simmilar to "Date now = new Date();" in java
println(df format now) "df format now" is similar to "df.format(now)"




In scala is everything an object. There is no primitive datatypes (that is similar to ruby).

Because scala based on functional programming functions are objects too.


object Timer {

def oncePerSecond(callback: () => Unit) {
while (true) { callback(); Thread sleep 1000 }
}

def timeFlies() {
println("time flies like an arrow...")
}

def main(args: Array[String]) {
oncePerSecond(timeFlies)
}
}


with functions as object you can very simple code a callback.





timeFlies() our callback function.
callback: () => Unit parameter for a function. ()=>Unit is the type of all functions which take no arguments and return nothing.
Thread sleep 1000 is similar to Thread.sleep(1000)


rather the function timeFlies() you can use anonymous functions ala:

oncePerSecond(() => println("time flies like an arrow..."))

"() => code" declare an anonymous function



Clases:


class Complex(real: Double, imaginary: Double) {
def re() = real
def im() = imaginary
}


define class Complex with two arguments,
def re() and im() are getters. return type of these two methods is not given explicitly


object ComplexNumbers {
def main(args: Array[String]) {
val c = new Complex(1.2, 3.4)
println("imaginary part: " + c.im())
}
}


if you have no arguments in function, write: def re = real


class Complex(real: Double, imaginary: Double) {
def re = real
def im = imaginary
override def toString() =
"" + re + (if (im < 0) "" else "+") + im + "i"
}


class Complex(real: Double, imaginary: Double) -> primary constructor, which is part of the class declaration
override -> for overriding functions

(if (im < 0) "" else "+") have you see it ;)




class Rational(numerator: Int, denominator: Int) {

require(denominator != 0)

private val gcd = greatestCommonDivisor(numerator.abs,
denominator.abs)
val n = numerator / gcd
val d = denominator / gcd

def this(n: Int) = this(n, 1)

private def greatestCommonDivisor(a: Int, b: Int): Int =
if (b == 0) a else greatestCommonDivisor(b, a % b)

def + (that: Rational): Rational =
new Rational(n * that.d + d * that.n, d * that.d)

def - (that: Rational): Rational =
new Rational(n * that.d - d * that.n, d * that.d)

def * (that: Rational): Rational =
new Rational(n * that.n, d * that.d)

def / (that: Rational): Rational =
new Rational(n * that.d, d * that.n)

override def toString = n + "/" + d
}



require(denominator != 0) -> look that variable is not null, throw exception


def this(n: Int) = this(n, 1) -> create from Rational(2) -> Rational(2,1)

you can see +-*/ are objects functions, which can be simple overrided

Sonntag, 4. Oktober 2009

bashing

test -x $BLA || exit 5

find /proc -type s
ls -Rl /proc/[0-9]* | grep ^s

some commands

lpr -Plp Datei # datei ausdrucken

fuser -k /dev/lp0 # alle dienste die den /dev/lp0 benutzen toeten
#/dev/usb/lp0

scsi-scanner /dev/sg0

set -o nochlobber

git-rev-tree --bisect ^good1 ^good2 bad > git/refs/headers/tryN
git checkout tryN
^goodx > v2.6.12 bad > master


movw%bx, (%rsi) <-> recvfrom()


/usr/sbin/snort -b -m 027 -D -N -c /etc/snort/snort.conf -d -u snort -g snort -i eth1


iptables -A INPUT -ptcp -s PVT_IP_HERE -d 0/0


wget -p nur inhalt,
wget -r -np nur unterverzeichniss, nicht von root an

make kpkg, create a linux kernel image

export CONCURRENCY_LEVEL=6

time make-kpkg --initrd --append-to-version 1 --revision 1 kernel_image modules

Samstag, 3. Oktober 2009

trying Scala ... 3

List:

scala> var lst = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)
res29: List[Int] = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)


scala> def odd(inLst:List[Int]):List[Int]={
| if(inLst==Nil) Nil
| else if(inLst.head%2==1) inLst.head::odd(inLst.tail)
| else odd(inLst.tail)
| }
odd: (List[Int])List[Int]


def define a function/method
od() name
inLst:List[Int] => inLst parameter name with a List type of Integers
:List[Int] return a List of Integers
Nil
similar to none, None, null, void
inLst.head first element
inLst.tail all other elements after first element



scala> odd(lst)
res32: List[Int] = List(1, 7, 5, 3, 9)

trying Scala ... 2


scala> val try1=if (1==2) 8 else 9
try1: Int = 9

scala> var total=18
total: Int = 18
scala> while(total < 17) total+=3
scala> total
res15: Int = 18

scala> for(i <- 1 to 4) println("hi five")
hi five
hi five
hi five
hi five

scala> for(i <- 1 until 5 ; j <- 1 to 3) println(i,j)
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3)
(4,1)
(4,2)
(4,3)

scala> for(c<-"hello")println(c)
h
e
l
l
o


hex, octal, bin is the same as in java


scala> val abyte: Byte = 27
abyte: Byte = 27


scala> var chr='A'
chr: Char = A
scala> chr=5
chr: Char =
scala> chr=66
chr: Char = B


define functions:

scala> def max(x: Int, y: Int): Int = {
| if (x > y) x
| else y
| }
max: (Int,Int)Int

scala> max(6,7)
res22: Int = 7

scala> max(3.6, 4)
:6: error: type mismatch;
found : Double(3.6)
required: Int
max(3.6, 4)



define a class


scala> class Point {
| var x=0
| var y=0
| }
defined class Point


scala> val p=new Point
p: Point = Point@18b62e0

scala> p.x=3
scala> p.y=4


scala> class Point( ix:Int,iy:Int){
| var x=ix
| var y=iy
| def vectorAdd(newpt:Point):Point={
| new Point(x+newpt.x,y+newpt.y)
| }
| }
defined class Point

scala> val p1=new Point(3,4)
p1: Point = Point@75455c
scala> val p2=new Point(7,2)
p2: Point = Point@1ba3523
scala> val p3=p1.vectorAdd(p2)
p3: Point = Point@1095c6c
scala> println(p3.x,p3.y)
(10,6)


scala> class Point( ix:Int,iy:Int){
| var x=ix
| var y=iy
| def +(newpt:Point):Point={
| new Point(x+newpt.x,y+newpt.y)
| }
| def -(newpt:Point):Point={
| new Point(x-newpt.x,y-newpt.y)
| }
| override def toString="Point("+x+","+y+")"
| }
defined class Point

scala> val p1=new Point(3,4)
p1: Point = Point(3,4)
scala> val p2=new Point(7,2)
p2: Point = Point(7,2)
scala> val p3=new Point(-2,2)
p3: Point = Point(-2,2)

scala> val p4=p1+p2-p3
p4: Point = Point(12,4)
scala> println(p4.x,p4.y)
(12,4)


there is no switch -> use match

scala> def decode(n:Int){
| n match {
| case 1 => println("One")
| case 2 => println("Two")
| case 5 => println("Five")
| case _ => println("Error")
| }
| }
decode: (Int)Unit









trying Scala ...

install scala, and type scala in console ;)


scala> 4/3.0
res5: Double = 1.3333333333333333

scala> 4/0
java.lang.ArithmeticException: / by zero
at .(:5)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAcce...

scala> var a=3
a: Int = 3

scala> a
res7: Int = 3

scala> a=5
a: Int = 5


scala> val b=3
b: Int = 3

scala> b
res10: Int = 3

scala> b=5
<console>:5: error: reassignment to val
b=5


var and val define variables, val is immutable

For printing is here
Ascala> println(b)
3


operators and control sturctures (if, else, ...) are the same as in java.


scala> val isBook = 6>=3
isBook: Boolean = true

scala> val price=16
price: Int = 16

scala> val vol=10
vol: Int = 10

scala> val sale=if (isBook)price*vol else price/vol
sale: Int = 160

scala> sale
res13: Int = 160

trying Scala

Tutorials: http://www.simplyscala.com/

Book: http://programming-scala.labs.oreilly.com/

further info from http://www.scala-lang.org/

http://www.scala-lang.org/node/1305

RCPLinAdmin

RCPLinAdmin is in planing,

should be an universal admin management tool for linux local/ssh writed in RCP/Eclipse.

http://sites.google.com/site/rcplinadmin

Mittwoch, 23. September 2009

RCPCisCat

RCPCisCat is in planning.

http://sites.google.com/site/rcpciscat/

RCPWire

RCPWire alpha is out.

http://sites.google.com/site/rcpwire/

Dienstag, 22. September 2009

CODE: neon

neon is an HTTP and WebDAV client library, with a C interface.

Sonntag, 5. Juli 2009

SHELL: get server


#!/bin/sh
#
[ $# -lt 1 ] && echo $0 server && exit 1

echo 'GET / HTTP/1.0\n' | nc $1 80 | egrep '^Server:'

SHELL: some commandos

found free loop device:
losetup -f

encryption with losetup:
insmod loop.o
insmod des.o
insmod cryptoloop.o
dd if=/dev/urandom of=crypto.img bs=1M count=50
losetup -e blowfish /dev/loop0 crypto.img
mkfs -t ext2 /dev/loop0
losetup -d /dev/loop0 # delete
mount -o encryption=aes256 crypto.img crypto_home

encryption with bind drive:
losetup -e aes256 /dev/loop0 /dev/sda1
losetup -d /dev/loop0
mount -o encryption=aes256 /dev/sda1 crypto_home

resizing the image:
dd if=/dev/urandom bs=1M count=20 >> crypto.img
losetup -e aes256 /dev/loop0 crypto.img
ext2resize /dev/loop0

encrypt/decrypt with openssl:
openssl enc -aes-256-cbc -salt -in password.txt -out password.txt.enc
openssl enc -d -aes-256-cbc -in password.txt.enc -out password.txt


echo "put /bla/bl/das.log.gz /bls/bl2/dies.log.bz2" >> $TFTPSCRIPT
tftp $TFTPSRV < $TFTPSCRIPT

uuencode $FWLOG/old/$BLA.log.gz $BLA.log.gz | mail -s "FWEXPORT $EXPORTNAME.log.gz" $MAILRECPT

PERL: some perl stuff 2


$arg=shift(@ARGV);
while(defined $arg) { ... }

$h{"bla"}="bla";
@val=values(%h);

<=> cmp -1 less 0 equal 1 greater

while(true) {} until(false){}

if() unless()

local($v) are local for temporary value of a gloabal variable

# modifying parameters
$a=1;
inc($a); # 2
sub inc {
$_[0]+=1;
}

@a=(1,2,3..);
mod(*a);
sub mod {
local (*la)=@_;
@la=reverse(@la);
}

print((split " ",$_)[0]."\n") while(<>);
# split im listenkontext, erster element

perl -ane 'print $F[0];' # analog
# -a autosplit, (split $_ into @F
# -n while loop

while(<>) { print if /head/; }


# cgi html
#!/usr/bin/perl -wT
print "Content-type: text/plain \n\n";
# -T taint Modus, Benutzereingaben in Quarantaene

HTML::Template;
use strict;
use warnings;
HTML::Embperl;
HTML::Mason;

Template;
HTML::Template;
DBI;
CGI;
CGI::Carp qw(fatalsToBrowser);

$q=new CGI;
foreach($q->param) { .. # param GET/POST parameters

$template=new HTML::Template(filename=>'addressbuch_kontakt.html');
$template->param(personen=>\@_pers)


#db
$dbh->selectrow_hashref('SELECT ..');
selectrow_array( .... );

if(exists $bla)
if( not exiss $bla)


#splice: removes and replaces elements in an array
splice(ARRAY, OFFSET, LENGTH, LIST);


%SIG # signal handles hash
sub handler { ... }
$SIG{'INT'}='handler';
$SIG{'INT'}='DEFAULT';


srand(time() | $$); # zeit doer pid einlesen
$l=int(rand(10));

if/ [^123]0 / # 10 20 30

x?? schaltet greedinessaus
\b word bounding
alternation /Ab|cd/ /(ma|pa)+/

$1 $2 ergebnisse der gruppierung
\1 \2 suchmuster
(?: ..) turn off capturing -> $1 void


V filehandle
$v scalar
@v array
%v hash
&v subroutine
*v everything named v

$s.$s2;
$s.=$s2; #concate
$sx4; #4 mal


s::mp('bla');
packages s;
sub mp {
print ("@_\n");
}

use File::Basename;
#look for File/Basename.pm


jeder Modul muss mit 1; beenden

BEGIN { require "bla.p"; }

pragmas: use integer; no integer;

calling methods (OOP)
$obj->meth($param);
meth $obj $param;

sub meth {
my $self=shift; # refernz auf obj
...
}

# constructor
sub new {
my $self={}; # hash anonymous referenz
$self->{ATTRIB1}=undef;
...
# make Referenz
bless($self);
return $self;
}

#Destructor
sub DESTROY {
...
}

###
sub u {
my $self=shift;
if(@_) {
$self->{U}=shift;
}
return $self->{U};
}
$o->('bla');
print $o->u,"\n";

#!!not
print "$o->u\n";
###


use Cwd;
$cur_dir=cwd;

use File::Basename;
(...)=fileparse(..);

use Config;
%Config


$perldoc -l copy
druckt den Pfad aus File::Find



www.pewrl.com/CPAN
HTML::LinkExtor, URI::URL
head() links()

PERL: some perl stuff


($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst)=localtime(time());
print scalar(localtime()); # > Fri Dec 21 11:11:30 2006

# gibt keine leezreilen und zeilen mit "---" oder mehr aus
print unless ( /^\s+$/ or /-{3,}/ );

# sucht nach kapazietaet
$pfad="/proc/acpi/battery/BAT0/state";
if(/remaining capacity: *?(\d*?) mAh/) {
print $1;


# eine mail senden

use Email::Send;
send SMTP=> <<'__MESSAGE__', $host;
To: anfrit00@fht-esslingen.de
From: foo@example.com

bla bla
__MESSAGE__


perl -e 'print "$_\n" foreach(@INC);'
/etc/perl
/usr/local/lib/perl/5.8.8
/usr/local/share/perl/5.8.8
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.8
/usr/share/perl/5.8
/usr/local/lib/site_perl

# formatprobe
open (CON_LIST, '>-') or die " open error\n";
$i=1; $key="klasst"; $result=0.23; $erg=23;

write CON_LIST;

close CON_LIST;

format CON_LIST =
@> @<<<<<<<<<<<<<<< @>>>> = @<<<<<<
$i, $key, $result, $erg
.


# zu datenbank verbinden (mysql)
use DBI();

$dbhost="localhost";
$dbname="mysql";
$dbuser="root";
$dbpass="";

# verbinden
my $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost",
$dbuser, $dbpass, {'RaiseError' => 1});
my $sth = $dbh->prepare("SELECT host, user, Password FROM user"); # anfrage
$sth->execute(); # anfrage senden

while (my $user = $sth->fetchrow_hashref()) { # daten als hash holen
# $user ist eine hash referenz
print "HOST: ".$user->{'host'}."\nUSER: ".$user->{'user'}."\nPASS: ".$user->{'Password'}."\n\n";

}
$dbh->disconnect(); # verbindung zu db schliessen


# einfaches parameter options
use Getopt::Std;
getopts("vr:", \%options);
print "v-> ".$options{'v'}."\n";
print "r-> ".$options{r}."\n";
#./test.pl -v -r bla
#v-> 1
#r-> bla


# dateien globen
$path=".";
while (<$path/*.pl>) {
print $_."\n";
}
# gibt alle dateinamen *.pl aus


# socket
my $sock = new IO::Socket::UNIX (Peer => $ARGV[0], Type => SOCK_STREAM); # socket
print $sock $query; # senden
my $response = <$sock>; # empfangen
close $sock;

# einfacher server
use IO::Socket;
$socket=new IO::Socket::INET (LocalHost=>$ARGV[0],
LocalPort=>6677,
Proto=>'tcp',
Listen=>5, Reuse=>1,);
# max 5 stueck, reuse=1 benutze den port wieder
die "new socket error: $!" unless $socket;

while($cli=$socket->accept()) { # client verbindet
print $cli "Hallo, \n";
$cli->flush(); # flushen
close($cli); # und tschuess
}
close($socket);

# server
user IO::Socket;
$ss=new IO::Socket::INET(LocalPort=>2345, Listen=>$SOMAXCONN, Proto=>'tcp', Reuse=>1);
# loop on incomming connections
while($sc=$ss->accept()) {
$data=<$sc>; # empfang
print $sc "bla"; # senden
$sc->close();
}
close($ss);

# client
$sc=new IO::Socket::INET(PeerAddr=>$hostname, PeerPort=>2345, Type=>SOCK_STREAM, Proto=>'tcp');
# .. aehnlich server

# donot use IO::Socket::INET this package is defined in IO::Socket

# pack unpack
my $query = pack("L L N N S S", 0x2343defe, 0x12345678,
2343343, 1111323, $ARGV[2], $ARGV[4]);
my ($magic, $id, $type, $genre, $detail, $dist, $link, $tos, $fw,
$nat, $real, $score, $mflags, $uptime) =
unpack ("L L C Z20 Z40 c Z30 Z30 C C C s S N", $response);


# alle modulen *.pm ausgeben
perl -e 'print "$_\n" foreach(@INC);' | while read f; do find $f -name \*.pm ; done


# inplace editieren, scritpname:pr2.orig
@ARGV=("pr2.orig");
$^I=".bak"; # backup name
$n=0;
while(<>) {
print "$n: $_";
$n++;
}


# pipe
open IN, "cat mail.pl |" or die "cat??";
print while();
close IN;
$cmd1="wc";
$cmd0='tr -s " " " " ';
open SEND, "| $cmd0 | $cmd1" or die "bla";
print SEND "daasa sdfklasdf jkaldskjfaslkdj ";
close SEND;
open SEND, "| tr '[a-z]' '[A-Z]'";
print SEND "hello"; # leitet die ausgabe ueber pipe nach tr
close SEND;
$encoder="/usr/bin/uuencode";
open SEND, "| $encoder stdout" or die "encoder";
print SEND "dies ist eine probe";
close SEND;


# cgi
use CGI qw/:standard/;
print header,
start_html('-title'=>"Statistik"),
p("Die letzten Tagen"),
h1("Blabla bla");


# datei locken
use IO::File;
use Fcntl qw(:flock);

my $fh=new IO::File(">probe.txt") or die "IO::File\n";

flock $fh, LOCK_EX;
print "get flock datei\n";
print $fh "bla bla";

$eing=<>;

flock $fh, LOCK_UN;
$fh->close;


# ftp benutzen
use Net::FTP;

$host="dx40";
$user="ray";
$pass='';

$ftp=Net::FTP->new($host) or die "Net::FTP->new error:$!\n";
$ftp->login($user, $pass) or die "login error: $!\n",$ftp->message;
$ftp->binary();
$ftp->cwd("/home/ray/daten") or die "cwd error\n";

@alle=$ftp->ls();
#print foreach(@alle);
$ftp->get($_) foreach(@alle);

$ftp->quit();



# holt n-mails von gmx.net ab
use Net::POP3;
$host='pop.gmx.net';
$user='userbla@gmx.net';
$pass='passbla23';
$ort='/home/ray/daten/popmails/mail';

$datum=time();
open OUT, ">>$ort$datum" or die "kann nicht mail oeffnen: $! \n";

($mail=Net::POP3->new($host)) or die "error open $host: $!\n";

$lo=$mail->login($user, $pass);
die "login error: $!\n" unless defined $lo;
print "Login: $user OK\n";
if($lo) {
foreach $nr (1..10) {
print "Mail nr $nr\n";
$inhalt=$mail->get($nr);
print OUT @$inhalt;
$mail->delete($nr);
}
}
close(OUT);
$mail->quit();


# wandelt ip in int um
use Net::IP;
my $src = new Net::IP ("127.0.0.1") or die (Net::IP::Error());
print $src->intip()."\n";


# read a gif file
$A="latest.gif";
open A or die "$A: $!";
read A, $b, 1024;
@c=unpack "C4A40(A/A)4", $b;
open OUT, ">gifinfo.txt";
print OUT for(@c);
close OUT;


# zeit
@monat=qw(jan feb mar apr mai jul jun aug sep okt nov dec);
my ($s,$m,$h,$d,$mo,$y,@r)=localtime();
print "$h:$m:$s\n";
print "$d ".$monat[$mo]." ", 1900+$y,"\n";



$|=1; # ausgabe nicht puffern



# persistente variablen(hash)
use GDBM_File; # persistenter hash
use Fcntl; # O_CREAT, O_RDWR usw
# persistenten hash oeffnen
tie(%MEM, GDBM_File, $pfile, O_CREAT|O_RDWR, 0644) or
die "cannot open $pfile";
# binden den hash MEM an die Datei pfile, ueber GDBM Interface
# der hash MEM erscheint als normaler hash im speicher, liegt aber auf der platte
$MEM($url)="bla"; # schreiben
$d=$MEM('pla'); # lesen
untie(%MEM); # pers. hash schliessen



# web
use LWP::UserAgent;
$ua=LWP::UserAgent->new(); # user agent erzeugen
$request=HTTP::Request->new('GET', $url); # url festlegen
$response=$ua->request($request); # netzzugriff ausfuehren
if($response->is_error()) { ... } # $response->is-success
$response->content()

use LWP::Simple;
$doc=get 'http://www.google.de';

perl -MLWP::Simple -e 'getprint "ftp://...";'

use LWP::Simple;
mirror($url, $localfile);


use HTML::Parse;
use HTML::FormatText;
$html=parse_htmlfile($htmlfile);
$formatter=new HTML::FormatText(leftmargin=>0, rightmargin=>70);
print OUT $formater->format($html);




# checksume berechnen
$chksum=unpack("%16C*", $dat);



# Ping
user Net::Ping;
$po=Net::Ping->new();
if($po->ping($hostname)) {
print "$hostname da";
}
$po->close();


# FTP
use Net::FTP;
login($servername, $passwort);
#ascii, binary
cwd($dir); # dir($dir) pwd quit
$dir=$ftp->dir(); # return a refernce
foreach(@$dir) print "$_\n";
#
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $@";
$ftp->login("anonymous",’\-anonymous@’)
or die "Cannot login ", $ftp->message;
$ftp->cwd("/pub")
or die "Cannot change working directory ", $ftp->message;
$ftp->get("that.file")
or die "get failed ", $ftp->message;
$ftp->quit;
# SEE perldoc Net::FTP


# mail
use Mail::Send;
$mail=Mail::Send->new();
$mail->to($addr);
$mail->cc($copy_to_addr);
$mail->bcc($blind_copy);
$mailhandle=$mail->open();
print $mailhandle << END;
bla bla
....
END;
$mailhandle->close(); # send mail


# MIME
use MIME::Lite;
$msg=MIME::Lite->new(From=>$fromaddr, To=>$toaddr, Subject=>'bla',
Type=>'multipart/mixed');
$msg->attach(Type=>'TEXT', Encoding=>'7bit', Data=>'bla bla');
$msg->attach(Type=>'image/gif', Encoding=>'base64', Path=>'bild.gif', Filename=>'bild.gif');
# convert message to string
$str=$msg->as_string();
$msg->send();

# decoding MIME
use MIME::Parser;
$mime=new MIME::Parser;
$mime->read(\*INPUT); # geoffnete Datei


# SMTP
use Net::SMTP;
$ms=new Net::SMTP($hostname, Debug=>1,);
$ms->mail($from_addr);
$ms->to($addr);
$ms->data($data); # in data steht auch der subject

# POP3
use Net::POP3;
$ms=new Net::POP3($hostname);
$msg_cnt=$ms->login($usrename, $passwd);
$headers=$ms->list(); # reference
foreach $message(keys(%$headers)) {
$header=$ms->top($message);
}
$message=$ms->last();
$contents=$ms->get($message);
$ms->quit();


# DBM
dbmopen(%dbhash, "filename", 0666);
$dbhash{'name'}="bla bla";
print $dbhash{'any'};
dbmclose(%dbhash);

while(($key, $val)=each(%dbhash)) {
print ...
}

use Fcntl;
use SDBM_File;
use Config;
$flags=O_CREAT|O_RDWR;
tie(%dbhash, 'SDBM_File', 'sdmtest', $flags, 0666) or die "cannot open database $!";
$dbhas{localtime()}="bla..";
untie(%dbhash);
# tie bindet eine variable zu etwas, hier zu einer datenbank klasse
# tie(var, classname, file, flags, mode)
# var: %hash, @array, $scalar, HANDLE

tying scalar, array, filehandle, hash when call 'tie', perl execute proper constructor
array ->TIEARRAY
handle->TIEHANDLE
hash ->TIEHASH
scalar->TIESCALAR

TIESCALAR classname, list
FETCH this
STORE this, val
DESTROY this

package Myscalar;

sub TIESCALAR {
my ($class)=$_[0];
my ($self)=0;
return (bless(\$self, $class));
}
sub FETCH {
my($reference_to_self)=$_[0];
return ($$reference_to_self);
}
...
1;

# an object is a reference

tie($myscalar, 'Myscalar');
$myscalar=55;
untie($myscalar);


TIEARRAY classname, list
FETCH this, index
STORE this, index, value
DESTROY this

TIEHANDLE classname, list
PRINT this, list
PRINTF this, list
READLINE this # called by <>
GETC this
READ this, list
DESTROY this

TIEHASH classname, list
FETCH this, key
STORE this, key, value
EXISTS this, key
DELETE this, key
CLEAR this
FIRSTKEY this
NEXTKEY this, lastkey
DESTROY this

store $v=$hash{'key'};
exists if(exists($hash{'key'})) { ..
delete delete $hash{'key'};


Tie::Hash, Tie::StatHash

sub STORE{ $_[0]->{$_[1]}=$_[2]; }
sub FIRSTKEY{ my $a=scalar keys%{$_[0]};
each%{$_[0]} }
sub CLEAR{ %{$_[0]}=() }
# %{..} anonyme hash


package Tie::Stdhash;
@ISA=qw(Tie::Hash); # used to speify which baseclass a new class inherits from
# Tie::StdHash inherits from Tie::Hash
sub STORE { ... } # override the STORE method


#Databases
use DBI;
@drivers=DBI->available_drivers();
$db=DBI->connect('dbi:mSQL:test:localhost);
$db->disconnect();
$cursor->execute();
if($DBI::err) { .. error }


perldoc DBI::FAQ
# extract the data
while(@val=$cursor->fetchrow()) { .. }

use vars qw(@ISA); # pragma to predeclare global variable names (obsolete)


# Zlib
use Compress::Zlib;
binmode STDOUT;
my $gz=gzopen(\*STDOUT, "wb");
while(<>) {
$gz->gzwrite($_);
}
$gz->gzclose();

$gzerrno!=Z_STREAM_END; Z_OK

$gz->gzreadline($_);
$gz->gzread($buffer);

my $x=inflateinit();
($output, $status)=$x->inflate(\$input);
my $x=deflateinit();
($o, $s)=$x->deflate($_);
$x->flush();


use Bla;
$o=Bla::funct() # oder
use Bla qw(funct);
$o=funct();


push @arr, $var; # append $var to @arr
pop @arr; # get the latest element and remove it

use File::Basename;
$path="/usr/lib/perl5/Net/Ping.pm";
@suf=".pm";
($basename, $dir, $suffix)=fileparse($path, @suf);
# Ping /usr/lib...


use Config;
%Config; # -which include a lot of inormation about the configuration of Perl
if($Config{osname}=~/^Windows/) ..

use File::Compare; # compare 2 files
$result=compare($file1, $file2); # 0 equal 1 diff -1 errror

use File::Copy;
copy($sourcefile, $destfile);
move($srcfile, $dstfile);

#Installing new Module
perl Makefile.PL
make
make install



# Image with GD
use GD;
$im=new GD::Image(100,100);
$back=$im->colorAllocate(100,150,255);
$im->fill(1,1,$back);
$outfile=">"."name.gif";
print OUT $im->gif;
$black=$im->colorAllocate(0,0,0);
$im->string(gdLargeFont, 30,40, $message);
# stringUp(..) rotate 90°
$font=gdMediumBoldFont;
$char_width=$font->width;
$char_height=$font->height;
$len=length($message);
$im->line($x, $y, $x2, $y2, $color);
$im->transparent($back);


/\binder\b/ # wortgrenze 'inder' aber kein 'rinder'