יום חמישי, 6 ביוני 2013

Declare ruby struct

A Ruby code that's demonstrating creating  a struct.

  1: myTestArray = []
  2: 
  3: myTestArray[0] = 'Mr Peer Zvika 42'
  4: 
  5: myTestArray[1] = 'Mrs Peer Dalit 42'
  6: 
  7: puts myTestArray
  8: 
  9: Persons = Struct.new(:title, :Family, :name, :age)
 10: 
 11: personsArray = []
 12: 
 13: myTestArray.each do |nextOne|
 14:   title, Family, name, age = nextOne.chomp.split(' ')
 15:   
 16:   personsArray << Persons.new(title, Family, name, age)
 17: end
 18: 
 19: puts personsArray
 20: 

Executing java jar from C-sharp

The following code execute a java jar from C# environment and capture back the standard output the jar generate.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Diagnostics;
  4: using System.IO;
  5: using System.Linq;
  6: using System.Text;
  7: using System.Threading.Tasks;
  8: 
  9: namespace SendTheEvent
 10: {
 11:     class Program
 12:     {
 13:         static void Main(string[] args)
 14:         {
 15:             ProcessStartInfo thePsi = new ProcessStartInfo ();
 16: 
 17:             thePsi.WorkingDirectory = @"C:\projects\myProjectWorkingDirectory";
 18: 
 19:             thePsi.FileName =@"C:\Program Files\Java\jre7\bin\java.exe";
 20: 
 21:             thePsi.RedirectStandardOutput = true;
 22: 
 23:             thePsi.UseShellExecute = false;
 24: 
 25:             thePsi.Arguments = "-jar ActiveMQNotifier.jar";
 26:        
 27:       using (Process process = Process.Start(thePsi))
 28:       {
 29:         //
 30:         // Read in all the text from the process with the StreamReader.
 31:         //
 32:         using (StreamReader reader = process.StandardOutput)
 33:         {
 34:           string result = reader.ReadToEnd();
 35:           Debug.Write(result);
 36:         }
 37:       }
 38:        }
 39:     }
 40: }

simple yii

After unpacking the yii-1.1.13.e9e4a0.zip file into the C:\Program Files\Zend\Apache2\htdocs directory I had notice the hello world sample in the demos directory:
C:\Program Files\Zend\Apache2\htdocs\yii\demos\helloworld
I lunch it using :
http://localhost/yii/demos/helloworld/index.php
And Walla :
Capture24
The code is very trivial:
The index.php file that inits the Yii bootstrap
and create and run the web application

  1: <?php
  2: 
  3: // include Yii bootstrap file
  4: require_once(dirname(__FILE__).'/../../framework/yii.php');
  5: 
  6: // create a Web application instance and run
  7: Yii::createWebApplication()->run();

The controller :SiteController.php that override the default actionIndex method


  1: <?php
  2: 
  3: /**
  4:  * SiteController is the default controller to handle user requests.
  5:  */
  6: class SiteController extends CController
  7: {
  8: 	/**
  9: 	 * Index action is the default action in a controller.
 10: 	 */
 11: 	public function actionIndex()
 12: 	{
 13: 		echo 'Hello World';
 14: 	}
 15: }

UI reenter event deadlock

Dead locks in application are very often  related to the intersection between UI thread and BL layer threads.
Most of the UI technologies require single thread affinity to the UI  calls.so in order to update the UI  the BL threads must invoke a method through mechanism (like the control.invoke in wpf ) in order to change context to the UI context .

The following diagrams demonstrate a locking  problem that may be caused due to this :
 Capture28

1.A BLL activity wants to send update to the UI.
2.The message is send to a common point that invoke events in the UI .The common point is implemented the Front controller pattern . Often this point is protected by locking mechanism in order to void the situation were several updated are entered to the mechanism of the front controller.
3.The UI process the message and call the BL layer to help
4.The helping activity try to access the UI with result  or notification and bang a dead lock .

A lot of poor design is involved here but Its happens to me to meet several systems that implements the front controller badly and the troubles come along.

יום רביעי, 5 ביוני 2013

python properties setter getter and deleter

Property attribute allows to declare read only attribute using the @property attribute.
For an example:

  1: class myCls():
  2:     def __init__(self):
  3:         pass 
  4:         self.myPropValue = 164
  5:     @property
  6:     def ProoValue(self):
  7:         return self.myPropValue
  8: 
  9: theCls = myCls()
 10: 
 11: print (theCls.ProoValue)
 12: 
 13: print ("Done")

Declaring a full property in python is done using properties setter and getter  and deleter attributes.
Note that the property should be declare using the @property attribute in front  of the setter and getter declarations.


  1: class myCls():
  2:     def __init__(self):
  3:         pass 
  4:         self.myPropValue = 164
  5:     @property
  6:     def ProoValue(self):
  7:         return self.myPropValue
  8: 
  9:     @ProoValue.setter
 10:     def ProoValue(self, value):
 11:         self.myPropValue = value
 12: 
 13:     @ProoValue.deleter
 14:     def ProoValue(self):
 15:         del self.myPropValue
 16: 
 17: theCls = myCls()
 18: 
 19: theCls.ProoValue = 6
 20: 
 21: print (theCls.ProoValue)
 22: 
 23: del theCls.ProoValue
 24: 
 25: print ("Done")

Create custom QT Widget

Define the main module that shows the widget

  1: #include <QtGui>
  2: #include "CustomWidget.h"
  3:  
  4: int main( int argc, char **argv )
  5: {
  6:   QApplication app( argc, argv );
  7:  
  8:   CustomWidget theCustomWidget;
  9:   CustomWidget.show();
 10:  
 11:   return app.exec();
 12: }

The custom widget header file  define a slot allows other modules to connect  in order to get mouse clicking events from the widget .


  1: #include <QtGui>
  2: #include <QWidget>
  3:  
  4: class CustomWidget : public QWidget
  5: {
  6:     Q_OBJECT
  7: public:
  8:    CustomWidget();
  9:  
 10: protected:
 11:     void paintEvent(QPaintEvent *event);
 12:   void mouseReleaseEvent ( QMouseEvent * e );
 13:     void mousePressEvent ( QMouseEvent * e );  
 14: private:
 15:   QPoint m_lastPoint;  
 16:     // member variable - flag of click beginning
 17:     bool mWasMouseClicking; 
 18:   
 19: signals:
 20:    void OnMouseClick(QPoint pPos);
 21: public slots:
 22: 
 23: private:
 24:   bool CheckIfPointInWidget (QPoint pPos);
 25:   int DistanceBetweenpoints (QPoint pFirstPt , QPoint pSecondPt);  
 26: };

The custom widget implementation cpp file.


  1: #include "CustomWidget.h"
  2:  
  3: void CustomWidget::paintEvent(QPaintEvent *event)
  4: {
  5:     //create a QPainter and pass a pointer to the device.
  6:     QPainter painter(this);
  7:  
  8:      painter.drawPolygon(polygon);
  9:  
 10:      //draw the widget for an example an ellipse
 11:      //Enables antialiasing, 
 12:    //set QPainter to use different
 13:      //color intensities on the edges to reduce the visual distortion that normally
 14:      //occurs when the edges of a shape are converted into pixels 
 15:      painter.setRenderHint(QPainter::Antialiasing, true);
 16:    
 17:    //Set the inner and the outer colors
 18:      painter.setPen(QPen(Qt::pink, 4, Qt::DashLine, Qt::RoundCap));
 19:      painter.setBrush(QBrush(Qt::blue, Qt::Dense5Pattern));
 20:      
 21:    painter.drawEllipse(200, 50, 250, 100);
 22:    }
 23:    
 24:    void CustomWidget::mousePressEvent ( QMouseEvent * e )
 25:   {
 26:     if ( CheckIfPointInWidget (e->pos()) == false )
 27:     {
 28:       return ;
 29:     }
 30:   
 31:     m_lastPoint = e->pos();
 32:     // set the flag meaning "click begin"
 33:     mWasMouseClicking = true;
 34:   }
 35:   void CustomWidget::mouseReleaseEvent ( QMouseEvent * e )
 36:   {
 37:     if (!mWasMouseClicking) )
 38:     {
 39:       return ;
 40:     }
 41:     
 42:     if ( CheckIfPointInWidget (e->pos()) == false )
 43:     {
 44:       mWasMouseClicking = false ;
 45:       return ;
 46:     }
 47:     
 48:     if ( Abs ( DistanceBetweenpoints (e->pos() , m_lastPoint) > 4 == true )
 49:     {
 50:       mWasMouseClicking = false ;
 51:       return ;
 52:     }  
 53:     emit OnMouseClick (e->pos());    
 54:   }

Sources:
http://www.youtube.com/watch?v=ScZn-cQiVFs&list=SP2D1942A4688E9D63
http://www.developer.nokia.com/Community/Wiki/MouseClick_event_in_Qt's_custom_widget
http://www.qtcentre.org/threads/19493-creating-custom-signa
http://harmattan-dev.nokia.com/docs/library/html/qt4/qbrush.html

OSGI service registration

Declare the operator service:

  1: public class OperatorData {
  2:    String mName; 
  3: 
  4:     public String getmName() {
  5:         return mName;
  6:     }
  7: 
  8:     public void setmName(String mName) {
  9:         this.mName = mName;
 10:     }
 11: }

  1: public interface IOperatorDataService {   
  2:     OperatorData GetOperatorDataByID (int pOperatorID);
  3: }
  4: 

The interface implementer class


  1: public class OperatorDataService implements IOperatorDataService{
  2: 
  3:     public OperatorData GetOperatorDataByID(int pOperatorID) {
  4:         OperatorData theOperatorData = new OperatorData();
  5:         theOperatorData.setmName("Zvika");
  6:         return theOperatorData;    
  7:     }
  8: }

Register the service in the activator start command
UnRegister the service in the activator stop command


  1: package com.mycompany.mybundle;
  2: 
  3: import org.osgi.framework.BundleActivator;
  4: import org.osgi.framework.BundleContext;
  5: import org.osgi.framework.ServiceRegistration;
  6: 
  7: public class Activator implements BundleActivator {
  8: 
  9:     private ServiceRegistration registration;
 10: 
 11:     public void start(BundleContext context) throws Exception {
 12:         System.out.println("Start operator services!" );
 13:         registration = context.registerService(IOperatorDataService.class.getName(),
 14:             new OperatorDataService(), null);
 15:     }
 16: 
 17:     public void stop(BundleContext context) throws Exception {
 18:         System.out.println("Stop operator services!" );
 19:         registration.unregister();
 20:     }
 21: }

uninstall the last version
felix:uninstall 5


Install the new version and start it
g! felix:install file:bundle/myBundle-1.0-SNAPSHOT.jar
Bundle ID: 7
g! start 7


Inspect the bundle interfaces


g! inspect cap service 7
com.mycompany.myBundle [7] provides:
------------------------------------
service; com.mycompany.mybundle.IOperatorDataService with properties:
   service.id = 17
g!