-
Notifications
You must be signed in to change notification settings - Fork 0
Hello Trema! in ruby
Let us get started with the first Trema application "Hello trema". The message "Hello trema" is printed out, and the related source code is as follows:
class HelloController < Controller
def start
info "Hello trema!"
end
end
"Hello trema!" is the simplest Trema program, and it also contains the mandatory part for a Trema application. It is a program with only five lines of source code.
Let us give a brief explanation on it.
All of the Trema applications inherit from class Controller. Various methods are reserved or defined and the implicit initialization is also implemented in class Controller.
For example, start method is defined and invoked automatically at runtime.
def start
info "Hello trema!"
end
info() is one of the functions of Trema for printing out messages. It is used as follows.
info( "Hello trema!" );
In the above example, "Hello trema!" is passed as an argument. It will be printed out to stdout or the log file in a specific format.
According to their severity levels, the functions of Trema can be divided into six categories as follows. We will explain how to set the severity level of a message and how to set the output location in Trema later.
- critical()
- error()
- warn()
- notice()
- info()
- debug()
OK, let us try to run it. It is noted that the command of trema run can be used to run any Trema applications.
% ./trema run hello_trema.rb
Hello trema! # it can be terminated by Ctrl-c.
How do you like Trema now? Hope you can enjoy developing your Trema applications in this way.
We have shown the basic format of a Trema program. In the following part, we try to develop another Trema application, connect it to a OpenFlow switch, and print out the unique ID of the OpenFlow switch ( it is also called Datapath unique ID, datapath_id ). If you know the unique ID of the OpenFlow switch, you may further control it.
Wait a minute. Is it OK even I do not have an OpenFlow switch?
Don't worry about it. Supporting virtual network is one of the powerful features provided by Trema. Trema virtual network consists of virtual OpenFlow switches and virtual hosts. You can run your programs directly on the Trema virtual network. It means that you can develop, test and run your Trema programs without real OpenFlow switches. Moreover, your Trema programs can be used on the real hardware platform without any modification further. It sounds cool, right? OK, let us create a virtual switch first.
To launch a virtual switch, you should add the following lines to the configure file trema.conf, which lies in the same directory as command trema. Then the defined virtual switch launches automatically when trema run is executed.
vswitch {
datapath_id "0xabc"
}
datapath_id is a 64-bit number in hexadecimal format, you can assign any proper value to it as you like.
Once a Trema program connects to an OpenFlow switch, the switch_ready event is emitted; so that you can confirm the connection by setting the event handler for switch_ready event.
class HelloController < Controller
def switch_ready datapath_id
info "Hello %#x from #{ ARGV[ 0 ] }!" % datapath_id
end
end
The function info() prints out the datapath_id which is got in the event handler switch_ready.
OK, let us run it. The execution method is the same as explained before.
Please make sure the configure file named trema.conf and command trema lie in the same directory before running the program.
% ./trema run hello_trema.rb
Hello 0xabc from hello_trema.rb! # it can be terminated by Ctrl-c.
You can see the datapath_id of the virtual switch is printed out correctly. We show how to use the datapath_id to control a real OpenFlow switch later.
In this tutorial, we have developed a program named "Hello trema!". It contains mandatory parts for all Trema applications. We have also created another program, and it prints out the datapath_id correctly. What we learned is summarized as follows.
- All of the Trema applications written in Ruby inherit from class Controller.
- info() is used to print out messages.
- How to use vswitch to define a virtual switch in the Trema configure file trema.conf.
- How to define a event handler for switch_ready event by invoking set_switch_ready_handler() function.