Chitika

Friday, June 12, 2015

ubuntu - merge MP4 files

Tried mencoder as well as avconv, all failed

Find following:

MP4Box -cat file_01.m4v -cat file_02.m4v -cat file_03.m4v -new final.m4v

works like charm.


Source: http://kheyali.blogspot.com/2012/09/joining-mp4-files.html

Tuesday, January 6, 2015

Spring MVC Hibernate entity update

Following is the steps involved in updating an entity, in Spring MVC:

function(Entity ent) {
....
// where ent is the entity passed through spring mvc.

1. First get entity from repo
   Entity savedEntity = repo.getById(entId);

2. Merge savedEntity with ent passed from spring MVC
    savedEntity.setA(ent.getA());
    savedEntity.setB(ent.getB());
    // store last field going to delete, do not delete it yet (if any)
    Ent2 ent = savedEntity.getEnt2();
    // repopulate with new field
    savedEntity.setEnt2(new Ent2());

3. Save (update) entity
    repo.save(savedEntity);

4. Delete last field (if any), from step 2
    repoEnt2.delete(ent);

Not following above step will result in following exceptions:

org.hibernate.ObjectDeletedException: deleted instance passed to merge.
java.lang.IllegalStateException: An entity copy was already assigned to a different entity.

Tuesday, December 30, 2014

Failed to read candidate component class

I was having following error,

Failed to read candidate component class: file [/a/b/c/z$1.class]; nested exception is java.lang.SecurityException: Prohibited package name: java.lang: Prohibited package name: java.lang applicationContext-jpa.xml /im4/src/main/resources/META-INF/spring line 5 Spring Beans Problem

When clicked on it, it opens applicationContext-jpa.xml, with line <repositories base-package="a.b.c" />

Looking into the code of z.java, it is found out that it is util class having package a.b.c, which spring scans for beans.

Changing the package to a.b.z resolves the issue.

Thursday, November 20, 2014

hibernate reverse engineering database - composite key, no many-to-many relationship

I was trying to generate hibernate code through hibernate tools but I was getting issue with association table being created as entity and another id entity was created, and many-to-many relationship was not recognized.

Most of the places, it is written that for many-to-many relationship, association table must have just two foreign keys and no other field. But they forget to mention that these two fields but be the composite primary key of that table.

By setting the foreign keys as composite primary key, hibernate successfully recognized it as many-to-many table along with neither entity of association table is created nor id entity was created. It works like a charm.

Monday, October 27, 2014

Windows ffmpeg split mp3 file into pieces

The other day I was listening a long (more than an hour long) audio file on car stereo using memory stick. I had to stop in between playing, after reaching destination. When I again take the driving seat, the recording started from start. I came to know that there is no memory feature in most of car stereos for storing last playback position.

The solution come to my mind is to split the recording into 10 minutes pieces each. I used following commands for it:

for /L %i IN (1, 600, 9999) DO (ffmpeg -ss %i -i recording.mp3 -c copy -t 600 recording-%i.mp3)

Where 600, is the pieces playback time, i.e. 10 minutes * 60 seconds = 600
9999, is the total playback time in seconds, keep it extra past the playback time, say if your playback time is 1 hour 30 minutes, set it to 1 = 60 mins + 30 minutes = 90 mins (add extra 10 minutes, make it 100 minutes), 100 * 60 = 6000, you can use 6000.

sources:
http://superuser.com/questions/525210/splitting-an-audio-file-into-chunks-of-a-specified-length
http://serverfault.com/questions/59014/for-loop-counting-from-1-to-n-in-a-windows-bat-script

Friday, August 1, 2014

Linux interface Bonding

Bonding is a tool/technique in linux to implement network load balancing or fault tolerance.


Install module

apt-get install ifenslave-2.6


Load module with options

modprobe bonding mode=active-backup miimon=100 updelay=200 downdelay=200

active-backup or 1 mode for fault tolerance
balance-rr or 0 (default) mode for load balance


Enlist interfaces for balancing/fallback

ifenslave bond0 wlan0 eth0


Obtain ip on bond0

dhclient bond0
or
ifconfig bond0 192.168.1.5


Remove bond0 interface

rmmod bonding

Thursday, April 3, 2014

assign IPv6 address

sudo ifconfig eth0 inet6 add 2001:0db8:100:1:2:3:4:5/48

Wednesday, January 29, 2014

easy grails + spring module integration

Dependencies:

Enable pom for maven dependency resolving:

In BuildConfig.groovy, before repository, set pom to true, as:

 pom true
    repositories {
// ...
mavenRepo "http://repository.codehaus.org"
//...

make sure maven repository path for spring module is enabled.

create pom.xml at same directory level as grails-app, at root folder, with contents as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ANY_GROUP_ID_DOESNT_MATTER</groupId>
    <artifactId>ANY_PROJECT_ID_DOESNT_MATTER</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>
    </dependencies>
</project>

Make sure module version matches with spring version comes package with grails, you may know by creating war, i.e. grails war, and extracting the war in folder and see file names of spring jars package in war.

Spring Beans:

Create resources.xml, at grails-app/conf/spring/, and define spring beans, as done in any normal spring application (beans.xml):

<beans xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean class="org.apache.activemq.command.ActiveMQQueue" id="destinationQueue">
<constructor-arg value="TEST.Q1"></constructor-arg>
</bean>

<bean class="org.apache.activemq.ActiveMQConnectionFactory" id="connectionFactoryQueue">
<property name="brokerURL">
<value>nio://localhost:61616</value>
</property>
</bean>

<bean class="org.springframework.jms.core.JmsTemplate" id="producerTemplate">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="defaultDestination" ref="destinationQueue"></property>
</bean>

<bean class="com.test.activemq.MessageSender" id="activeMQMsgSender">
<property name="jmsTemplate" ref="producerTemplate"></property>
</bean>

<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"
id="jmsContainer">
<property name="connectionFactory" ref="connectionFactoryQueue"></property>
<property name="destination" ref="destinationQueue"></property>
<property name="messageListener" ref="messageReceiverService"></property>
</bean>

</beans>

Above bean configuration connects to activemq at transport "nio://localhost:61616/", and send/receive message on queue "TEST.Q1"

Message Sender:

Create message sender bean, MessageSender.java, in src/java/com/test/activemq/, with following content:
(Note: its declaration in bean configuration file, resources.xml, above)

package com.test.activemq;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class MessageSender {
protected JmsTemplate jmsTemplate;

public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void sendMessages(final String msg) throws JMSException {
System.out.println("PRODUCER [" + msg + "]");
MessageCreator messageCreator = new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session
.createTextMessage(msg);
return message;
}
};
jmsTemplate.send(messageCreator);
}
}

Create controller ActiveMQController.groovy, as grails controller with action "send" as follows:
(Note: activeMQMsgSender bean usage, with related to MessageSender.java and resources.xml)

        def activeMQMsgSender
def send = {
def msg = params.msg
activeMQMsgSender.sendMessages(msg)
respond msg, model:[msg:msg]
}

Create corresponding view send.gsp as grails view with following content:
send [${msg}]

Message Receiver:

Create message receive bean, MessageReceiverService.goovy, as grails service as follows:
(please note, how it is specified in beans configuration file, resources.xml, above)

package com.test.activemq

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import grails.transaction.Transactional

@Transactional
class MessageReceiverService implements MessageListener {


public void onMessage(Message message) {
System.out.print("RECEIVER ");
TextMessage msg = (TextMessage) message;
try {
System.out.print("MESSAGE: [" + msg.getText() + "]");
} catch (JMSException e) {
e.printStackTrace();
}

System.out.println();
}
}

Testing

Run grails app for testing, sending message to queue.
On browser, run: localhost:8080/test/activeMQ/send?msg=abcd
which would place abcd text message on activemq, queue TEST.Q1
and, if everything went well, output on console will show sending message to queue and receiving message from queue.

(sources: http://grails.org/doc/latest/guide/spring.htmlhttp://icodingclub.blogspot.com/2011/07/introduction-of-spring-jms-with.html)

Friday, January 17, 2014

network scan for hosts

NMap utility is used to scan network for different criteria, such as all alive hosts on network can be get using following command:

nmap -sn 192.168.2.0/24

where network is 192.168.2.0/24, and -sn switch tells no ports to consider.

Friday, January 3, 2014

convert offline web to pdf

htmldoc --webpage -f example.pdf example_path/toc.html example_path/*.html
source: http://kmkeen.com/mirror/2009-02-05-14-00-00.html 

wget website subsection fetching

Following "wget" command can be used to fetch portion of website:


wget -r -l3 -k -p -I /docs,/static/images http://www.example.com/man/index.html

Where:
-r   : fetch recursively
-l3 : depth of 3, fist/index page is at depth 1
-k  : convert links to local links for offline viewing
-p  : prerequisites downloads for perfect page viewing (css/images)
-I   : comma seperated list of base path, to only fetch items containing uri starting from these base paths

Tuesday, November 12, 2013

my grails cheat-sheet

Environment

set $GRAILS_HOME=path to grails
set $JAVA_HOME=path to java
set $PATH=include $JAVA_HOME/bin and $GRAILS_HOME/bin

Init

grails create-app App1

Run

grails run-app

Create Domain

grails create-domain-class d1

path: grails-app/domain/d1.groovy

Data Constraints

class d1 {

String name
String address
Date dob
d2 linkedData

static constraints = {
   // order of fields in constraints are used in scaffolding
   name(size:1..48, unique:false)
   address(maxSize:128,blank:true)
   dob(blank:true)
   // linkedData of custom type nullable as in the case linked data doesn't exist,
    // then d1 can't be created
   linkedData(nullable:true)
}
}

Create controller

grails create-controller c1

path: grails-app/controller/c1.groovy

Scaffolding

class c1Controller {
    def scaffold = d1
}

Testing

grails test-app -unit
grails test-app -integration

Friday, November 1, 2013

mongo db

Setup:

run as root > mkdir -p /data/db

run as root > mongod
(--rest switch starts with rest interface)

create database abcdb:

> mongo abcdb
>_ (prompt will appear)
(db won't be persist unless data inserted)
insert data, into a collection coll:

> db.coll.insert( "lang": "english", "alphabets": ["a","b","c"...], "style":"ltr")
json object will be created in db as follows:
"book":{
                "coll":[
                              { "lang": "english", "alphabets": ["a","b","c"...], "style":"ltr" }
                            ]
              }

to see rows in collections:

> db.coll.find()

to see all the functions of any object use help() function

> db.help()
> db.coll.help()

to see all databases

> show dbs

to see the definition of function, enter function name without parenthesis

>db.coll.find

custom function
> function myFunctionLanguage(lang, style) {
... db.towns.insert(lang:lang, style:style);
... }

find function may take object Id as first argument or condition filter, and columns to show in result in second argument

find time taken by query to execute (need of index?)

> find(...).explain()

create index:

>db.coll.ensureIndex(...)

queries profiling:

> db.setProfilingLevel(2)
>db.system.profile.find()

Thursday, October 24, 2013

Wednesday, October 2, 2013

mplayer side by side to anaglyph viewing

mplayer -vf stereo3d,scale sbs.mp4

where, sbs.mp4 is side-by-side stereoscopic movie

linux 3D side-by-side to anaglyph video conversion


mencoder -vf stereo3d,scale sbs.mp4 -o anaglyph.mp4 -oac pcm -ovc x264

where:
sbs.mp4 is input file
anaglyph.mp4 is output file

Wednesday, September 18, 2013

ffmpeg encode dvd to mp4

ffmpeg -i "concat:tmpDev/VIDEO_TS/VTS_01_1.VOB|tmpDev/VIDEO_TS/VTS_01_2.VOB|tmpDev/VIDEO_TS/VTS_01_3.VOB|tmpDev/VIDEO_TS/VTS_01_4.VOB" -acodec copy -vcodec libx264 ./out.mp4

Monday, September 16, 2013

gnu c/gcc exception handling

man setjmp states:

"setjmp() and longjmp(3) are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp() saves the stack context/environment in env for later use by longjmp(3). The stack context will be invalidated if the function which called setjmp() returns."

man lngjmp states:

"longjmp() and setjmp(3) are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. longjmp() restores the environment saved by the last call of setjmp(3) with the corresponding env argument. After longjmp() is completed, program execution continues as if the corresponding call of setjmp(3) had just returned the value vallongjmp() cannot cause 0 to be returned. If longjmp() is invoked with a second argument of 0, 1 will be returned instead."

Example at wikipedia:

#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
 
enum { SOME_EXCEPTION = 1 } exception;
jmp_buf state;
 
int main(void)
{
  if(!setjmp(state))                      // try
  {
    if(/* something happened */)
    {
      exception = SOME_EXCEPTION;
      longjmp(state, 0);                  // throw SOME_EXCEPTION
    }
  } 
  else switch(exception)
  {             
    case SOME_EXCEPTION:                  // catch SOME_EXCEPTION
      puts("SOME_EXCEPTION caught");
      break;
    default:                              // catch ...
      puts("Some strange exception");
  }
  return EXIT_SUCCESS;
}


#include <stdio.h>
#include <setjmp.h>

#define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
#define CATCH } else {
#define ETRY } }while(0)
#define THROW longjmp(ex_buf__, 1)

int
main(int argc, char** argv)
{
   TRY
   {
      printf("In Try Statement\n");
      THROW;
      printf("I do not appear\n");
   }
   CATCH
   {
      printf("Got Exception!\n");
   }
   ETRY;

   return 0;
}


Advanced Try...Catch...Finally: (source: http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html)

#include <stdio.h>
#include <setjmp.h>

#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
#define CATCH(x) break; case x:
#define FINALLY break; } default:
#define ETRY } }while(0)
#define THROW(x) longjmp(ex_buf__, x)

#define FOO_EXCEPTION (1)
#define BAR_EXCEPTION (2)
#define BAZ_EXCEPTION (3)

int
main(int argc, char** argv)
{
   TRY
   {
      printf("In Try Statement\n");
      THROW( BAR_EXCEPTION );
      printf("I do not appear\n");
   }
   CATCH( FOO_EXCEPTION )
   {
      printf("Got Foo!\n");
   }
   CATCH( BAR_EXCEPTION )
   {
      printf("Got Bar!\n");
   }
   CATCH( BAZ_EXCEPTION )
   {
      printf("Got Baz!\n");
   }
   FINALLY
   {
      printf("...et in arcadia Ego\n");
   }
   ETRY;

   return 0;
}

Monday, September 2, 2013

grails db-reverse-engineer

Create application:

> grails create-app

Reverse engineer DB, following is the great tutorial for it:

http://grails-plugins.github.io/grails-db-reverse-engineer/docs/manual/guide/single.html

> grails db-reverse-engineer

Remember following points:

1. every thing in db should be in small case, separating words with underscore ("_"), instead of camel notation.
2. when joining tables, foreign key should be named as follows: table_name + underscore + id, e.g. foreign key for table "data" should be named: data_id, in all the tables joined with data table

Generate controllers:

> grails generate-all "*"

Run application:

> grails run-app

NOTE: if error occurred, set JDK path:

> export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

Wednesday, August 21, 2013

linux: secure, quick and easy sock5 proxy over ssh

Connect to remote server using following ssh command:

> ssh -D 12345 remote-server.com

a sock5 server will started on localhost:12345, port is 12345.

install tsocks package, as sock5 client

> sudo apt-get install tsocks

edit /etc/tsocks.conf

> vi /etc/tsocks.conf

remove everything, and enter following lines:

server = 127.0.0.1
server_type = 5
server_port = 12345

save and quit

Now to run firefox (or any other network application), using sock5, execute following command:

> tsocks firefox

enjoy, secure, quick and easy sock5 proxy over ssh.

Tuesday, July 2, 2013

C/C++ Profiling

Compilation
compile and link program using gcc/g++, without using ld command, with flag -pg

Autotools
for auto tools, clean project (make clean),
execute $> CFLAGS=-pg ./configure && make && sudo make install

Generation
execute the program /bin/prog.exe (or test suite), gmon.out file will be created in working directory

NOTE: gmon.out file will be created on program exit, killing the process, would not generate gmon.out

Post Processing
In order to generate profiling information from gmon.out, execute following command:
$> gprof /bin/prog.exe gmon.out > analyse.txt

Profiling information will be generated in analyse.txt file.

NOTE: provide complete path to program and complete path to gmon.out (here gmon.out is present in current directory)

Tuesday, June 25, 2013

GDB debugging

Following are the useful commands to debug:

debug with program arguments:

    gdb --args prog arg1 arg2 arg3 

Debugging an application with complex configuration files setup in different directories:

    compile the program with -g switch, and install it
    debug the installed program as follows:
    sudo gdb --args prog arg1 arg2 arg3

add break point to function:

    b func_foo

add break point to line in file:

    b file:line_num

step into:

    s

step over:

    n

continue program execution:

    c

start/restart program:

    r

print variable:

    p var
    x/s str_var

print 10 bytes in hex

    x/10x str_var

list program statements being in debug
  
    l

get call stack: (backtrace)
 
    bt

get value of some variable in function call in back in call stack at row 2:

    f 2
    p var

get all break points (info break):

    i b

delete a break point 3:

    d 3

watch a variable for change:

    wa var

get threads list (info threads):

    i th

change to different thread context (e.g. thread 2):

    t 2

attaching to an already running process for debugging (first get process id using ps -ef):

    at process_id

detaching to debugging process:

    det

debugging save state (checkpoint, only for single threaded application):

   checkpoint

   info checkpoint

restore program state to the checkpoint (e.g. 2):

   restart 2

GDB hex dump

Create file in home folder, named '.gdbinit', and put following contents in it:

define xxd
dump binary memory dump.bin $arg0 $arg0+$arg1
shell xxd dump.bin
end


during debugging execute following command to get hex dump of any memory area, by specifying starting address (or name of variable) and length:

(gdb) xxd var 112
0000000: aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000010:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000020:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000030:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000040:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000050:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................
0000060:
aabb ccdd eeff 0011 2233 4455 6677 8899 ................



Ref:

http://stackoverflow.com/questions/9233095/memory-dump-formatted-like-xxd-from-gdb
http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_chapter/gdb_18.html#SEC195