Differences

This shows you the differences between two versions of the page.

iot:labs:02 [2017/09/11 09:33]
iot:labs:02 [2017/09/11 09:33] (current)
Line 1: Line 1:
-===== Lab 2: Web Services ​=====+====== Lab 2: Linux ======
+Embeded boards run Linux, this is why when interracting with them you might need some basic notions concerning this operating system in case you haven'​t come across it so far.

-===== 1) Chat ===== +===== The Shell=====
-Using the what you learned in course 11 continue ​the chat app you started in laboratory 10. +The Shell is a window that allows ​you to interact with the boardIt waits for you to enter a command and it executes itOnce you open a Shell, ​you will see the prompt. That means everything works fine and the Shell is waiting for your command.
-For this you have an api at +
-http://api.my-sandbox.co/​chat/​chat.php +
-When you call get on it you will receive all the messages +
-When you send via post the variables username ​and msg to it, it will add a message to the chat. +
-For visibility show only last 5-10 messages. Each message is separated by "​\n"​+

-In order to fetch the messages ​you need to use the ajax get method from jquery and +The prompt also offers you some information. First of all, it shows you the user currently logged in. The user's name is that you see before //@//. It also shows the host name of your board.
-replace ​the \n with <​br> ​in order to display them+The most important information ​the prompt displays is the working directory. That is the directory where you are currently working ​in. It is displayed right after the colon in the prompt. You will notice that the default working directory is //~//. That is the user's home directory and its equivalent is ///​home/​username//​.
+===== Paths =====

-  $.get('​http://api.my-sandbox.co/chat/chat.php', function(msgs){ +In order to access a certain file or directory, you have to take into account the path to itThere are two different paths you can useabsolute and relative.\\ - msgs = msgs.split("​\n"​).slice(-5).join("<​br>"​);​ +In Linux, the directories'​ structure is like a tree. The root directory is ///// and it contains all the other directories and files.\\ - //​display msgs in some container +If you use an absolute path to a file or a directory, that means that you build the path to it starting with the root directoryThus, you can say that any path that starts with // / // is an absolute path.\\ - })+On the other hand, you can use a relative path, which means that you build it starting from the directory you are working in, your working directoryThus, all the files and directories are relative to it.\\ +When building paths, there are three symbols you should be familiar with: -The split method applied on a string splits the string into an array of strings using the given separator+ * - current directory -The slice method is used to select certain elements of an array , here the last 5+ ​* ​.. -parent directory -The join method applied on an array returns of string generated by concatenating the elements of the array + * // ~ // - home directory (/​home/​username) -of delimiting them by the given separator.+ -In order to send a message you will need to use the post ajax call:+===== pwd ===== -$.post('​http:​//api.my-sandbox.co/chat/chat.php',​{username:"​some user name", msg:"the contents of the msg"}, function(result){ +The //pwd// command makes the Shell print the working directory. It is important ​to know which directory you are working in and sometimes it is difficult ​to get it from the prompt. Soanytime ​you feel lost, use //pwd//.
-    //​code ​to run when msg sent successfully  +
-  }) +
-   +
-In order to make the chat from the last laboratory to workall you need to do is code the chatSend method to post +
-the latest message to the server as shown above  and retrieve the messages every second by calling the get +
-method shown above every 1000 ms with the help of an interval (setInterval).+

-Your chatSend method should now look like this: +===== ls =====
-  chatSend:​function(){ +
-    $.post('​http://​api.my-sandbox.co/​chat/​chat.php',​ + - ​{username:​$("#​username"​).val(),​ msg:​$("#​msg"​).val()} + - ); + - } + -===== 2) Newsfeed ​=====+ -Continue ​the newsfeed you started in laboratory 10. +//ls// makes the Shell print all the files and directories located in the working directory. If you want to see la contents ​of some other directory, you can pass that directory as an argument to the command. For instance, if you want to print all the files and directories in /, you will write: ​ //ls / // .\\ -Implement support for reading ​the whole article bu showing it on a new page + -and support for liking articles and display ​the number ​of likes.+ -For this you will have an api at:+===== cd ===== -http://api.my-sandbox.co/news/get.php +You already know that once you open a Shell, the working directory is your home directoryHowever, you will need to work in other directories tooIn order to change the working directory, you will have to use // cd// followed by the directory you want to go to.\\ -used to fetch latest news and +For example, if your home directory contains a directory called //​homework// ​and you want to have that as the working directory, you use //cd homework//. You can notice that you used an absolute pathSome other alternatives would be //cd /​home/​pi/​homework//​ or //cd ~/homework//. In the last two examples you used an absolute path to refer to //​homework//​ directory.\\ -http://api.my-sandbox.co/news/get.php?​id=PRIMARY_KEY_OF_ARTICLE + -to fetch the full article+ -and for likes+===== cat===== -GET http://api.my-sandbox.co/news/like.php with +//cat// asks the Shell to print the contents ​of a file. However, it must be clear that you can only see its contents, you cannot modify them. For that you need an editor. -with id set in order to get the number ​of likes for an article+ -GET http://api.my-sandbox.co/news/like.php with +Just like with the //cd// command, //cat// gets as an argument the file it should display. -with id and uuid set in order to get if you liked or not an article+ -POST http://api.my-sandbox.co/news/like.php with a JSON +Example: //cat /etc/passwd// - { + - method:"​like",​ + - uuid:"​phone-uuid",​ + - pk:"​article-pk"​ + - } + -in order to like an article+ -POST http://​api.my-sandbox.co/​news/​like.php with a JSON +===== htop ===== - { + - method:"​unlike",​ + - uuid:"​phone-uuid",​ + - pk:"​article-pk"​ + - } + -in order to dislike an article.+ -To get the uuid of the phone just access +By using the //htop// command you can see real-time all the processes that run on your board. Once you entered the command ​you will notice that the prompt does not appear, that is because you cannot enter another command until you are finished with displaying the processesSo, if you want to go back to what you were doing, just hit the //q// key. -device.uuid + -The device plugin must be installed (it should be installed in your apps by default but you will have to move the config.xml from the root folder in the www folder)+ -Fetching of news is identical to fetching the text in exercise oneThis is also true for fetching ​the +{{ :​iot2015:​labs:​htop.png?500 |}} -like count with or without id.+For each process displayed, you can see its PID (Process ID), the user who launched the process, how much CPU and memory it is using, ​the command that started the process and other information. -I order to set JSON encoded messages though ​you will need to use JSON.stringify method like in the following example: +What you are most interested in is the PID. That is because each process can be identified by its PID and if you want to interact with it, you have to know its process ID. - var json_object = method:"​like",​ uuid:device.uuid, pk:1 +===== kill ===== - ​$.post(URL,JSON.stringify(json_object), function(response)//post respone action }); +
-===== 3) Comunicate ​with webservers from your boards ​=====+You know that you can use //htop// to find a process'​ ID in order to be able to interact with it. //kill// is the command that allows us to interact with another process.
+
+Two processes can interact by using signals. A signal is a number a process sends to another. Both processes know that each number represents an action. you can refer to a signal either by the number or by its name.
+{:iot2015:labs:kill.png?500 |}
+
+The format of the //kill// command is the following: //kill -signal pid//where signal is the number representing the action you want to process to do and pid is the process ID.
+
+The two signals you are most interested in are //​SIGTERM// ​(number 15and //​SIGKILL// ​(number 9).
+
+SIGTERM tells the process to stop its execution. Normally, the process should save all its data and stop running. However, this signal can be ignored by the process. There are times when you cannot kill a process by using SIGTERM.
+
+On the other hand, SIGKILL, kills the process no matter what. The downside is that the process does not have the opportunity to save its data, so killing it like this can result in loss of data. Neverthless,​ if something happened and your process must be forced to stop, you have to use SIGKILL.
+
+In case the running process has a Shell attached and you can access it, you can simply use a key combination to send the SIGTERM signal to it and make it stop, //Ctrl+C// .
+
+===== killall =====
+
+//killall// has the same effect as //kill//, except that you do not have to know the PID of the process, but its name. Instead of passing the process ID as an argument, you have to pass the process name.
+
+===== Tips & Tricks =====
+
+Getting used to working ​with a Linux Shell is not difficult, especially if you know the following tricks:
+  *  Whenever you are typing a command use the //TAB// key. It will auto complete what you wanted to type, thus eliminating spelling errors. In case there are multiple possibilities,​ press TAB once more and they will be displayed. If by pressing TAB the command or the argument you want to type is not automatically filled in, it means the command is not valid.
+  * The most important command you should know is //man//. By using //man// followed by another command name, you have access to that command'​s manual and you can find how to use it and all it can do.
+===== Exercises =====
+
+  - Create a new folder // Folder1 // and a file inside it //My file //. Create a new folder // Folder 2//. Move it inside the first folder and rename it to // Folderception//​. List the content of Folder1. Delete the first folder and its content using a single command. \\ **Hint** you can use the manual pages to find and understand the commands. Also use apropos to find the needed command (eg. \$ apropos "move files" )
+  - Open a new project in Wyliodrin and run it. Find the process and its PID. Kill the process. \\ **Hint** Some commands may need root permissions to run. Using //sudo// in the beginning of the respective command gives you the needed permissions. ​

-Depending on what you made your boards try to make the phone communicate with the webserver from the boards.