2012-07-13

Compile OpenSSL with Visual Studio 2010


Download Perl, nasm, source OpenSSL -> chỉnh lại path trong file bat (copy trong link trên)

@rem @echo off
@cls

@rem 1. Download and unzip Netwide Assembler (http://nasm.us/) to some folder
@rem 2. Download and uzip Strawberry Perl (http://strawberryperl.com/)
@rem    or ActivePerl (http://www.activestate.com/activeperl/downloads)
@rem 3. Download and uzip (xxx.tar.gz) latest version of OpenSSL (http://www.openssl.org/)

@rem NOTE: For more info, please read INSTALL.W32 file in OpenSSL folder

@rem ***************************************************************
@rem Setup paths
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@set strPathNetwideAssembler=D:\workspace\nasm-2.09.10
@set strPathStrawberryPerl=D:\workspace\strawberry
@set strPathActivePerl=D:\workspace\Perl64\bin
@rem set strPathActivePerl=D:\workspace\Perl64\bin
@set strPathOpenSSLDir=D:
@set strPathOpenSSL=%strPathOpenSSLDir%\download\openssl-1.0.1c
@set strPathTargetOpenSSLDir=D:\workspace\openssl-1.0.1c
@set bEnableStaticEngine=True
@rem True -> the shared library build will compile the engines into libeay32.dll
@rem ***************************************************************

@IF NOT "%strPathNetwideAssembler%" == "" set PATH=%strPathNetwideAssembler%;%PATH%
@IF NOT "%strPathStrawberryPerl%" == "" set PATH=%strPathStrawberryPerl%\c\bin;

%strPathStrawberryPerl%\perl\site\bin;%strPathStrawberryPerl%\perl\bin;%PATH%
@IF NOT "%strPathActivePerl%" == "" set PATH=%strPathActivePerl%;%PATH%
@set strEnableStaticEngine=
@IF "%bEnableStaticEngine%"=="True" set strEnableStaticEngine=enable-static-engine

@rem START

@rem Create OpenSSL installation dir
@rmdir /S /Q %strPathTargetOpenSSLDir%
@mkdir %strPathTargetOpenSSLDir%
@echo OpenSSL will be installed into %strPathTargetOpenSSLDir%

@%strPathOpenSSLDir%
@cd %strPathOpenSSL%

@IF NOT "%strPathNetwideAssembler%" == "" call :ConfigureMakefileWithASM
@IF "%strPathNetwideAssembler%" == ""     call :ConfigureMakefileWithoutASM

@if "%VS100COMNTOOLS%" == "" goto ErrorNoVS2010
@call "%VS100COMNTOOLS%vsvars32.bat"

@cls
@echo ***************************************************************
@echo Compile OpenSSL
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak
@echo ***************************************************************
@echo Compile OpenSSL DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Test OpenSSL
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak test
@echo ***************************************************************
@echo Test OpenSSL DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo install OpenSSL to the specified location (%strPathTargetOpenSSLDir%)
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak install
@echo ***************************************************************
@echo install OpenSSL to %strPathTargetOpenSSLDir% DONE
@echo ***************************************************************
@pause

goto EOF



:ConfigureMakefileWithoutASM
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@rem Configure with platform VC-WIN32 but without Assembler and Build Makefiles
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo ***************************************************************
@echo Configure with platform VC-WIN32 but without Assembler
@echo ***************************************************************
@perl Configure VC-WIN32 no-asm %strEnableStaticEngine% --prefix=%strPathTargetOpenSSLDir%
@echo ***************************************************************
@echo Configure with platform VC-WIN32 but without Assembler. DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Build the Makefiles
@echo ***************************************************************
@call ms\do_ms.bat
@echo ***************************************************************
@echo Build the Makefiles DONE
@echo ***************************************************************
@pause
@cls
goto EOF

:ConfigureMakefileWithASM
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@rem Configure with platform VC-WIN32 and with Assembler, and Build Makefiles
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo ***************************************************************
@echo Configure with platform VC-WIN32 and Assembler
@echo ***************************************************************
perl Configure VC-WIN32 %strEnableStaticEngine% --prefix=%strPathTargetOpenSSLDir%
@echo ***************************************************************
@echo Configure with platform VC-WIN32 and Assembler. DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Build the Makefiles and optionally the assembly language files:
@echo ***************************************************************
@call ms\do_nasm.bat
@echo ***************************************************************
@echo Build the Makefiles DONE
@echo ***************************************************************
@pause
@cls
goto EOF

rem Errors
rem ***********************************************************
:ErrorNoVS2010
echo Visual Studio 2010 is not installed on your computer. Please install it and run this file again.
goto EOF

:EOF

LNK1181: cannot open input file ‘rpcndr.lib’

Mở đám code cũ COM ra, convert xong build bị cái lỗi này.

Note lại
Vào Linker bỏ rpcndr.lib tất nhiên vẫn giữ lại rpcrt4.lib
http://henbo.wordpress.com/2006/11/22/lnk1181-cannot-open-input-file-rpcndr-lib/

Compiling gcc on Mac OSX


Link hướng dẫn ở đây

Tạo link cho g++, gcc, cpp
cd /usr/bin
sudo ln -s /usr/local/g++ g++-4.2

Thêm -m32 hay -m64 vào export, không sẽ bị lỗi
checking for __gmpz_init in -lgmp... no
configure: error: libgmp not found or uses a different ABI.
Please read the INSTALL file -- see "In case of problem".

Thay export CXX như sau:
export CXX=/usr/bin/g++-4.2
chuyển thành
export CXX="/usr/bin/g++-4.2 -m32"

Nhanh hơn thì dùng macports
sudo port install gmp
sudo port install mpfr
sudo port install mpc

macports install vào /opt/local

2012-04-28

Một bản "cực kỳ" nháp syncing file (file storage)


Theo cách liệt kê thì tất cả các trường hợp có thể conflict:

Đối với file
Local
Remote
Notes
Created
Created
Type 1
Created
Modified
Hạn chế *
Created
Renamed
Hạn chế *
Created
Deleted
Hạn chế *
Created
Deleted container folder
Có thể không coi là conflict **
Modified
Created
logically
Modified
Modified
Type 2
Modified
Renamed
Type 2
Modified
Deleted
Type 3
Modified
Deleted container folder
Type 4
Renamed
Created
logically
Renamed
Modified
Type 2
Renamed
Renamed
Type 2
Renamed
Deleted
Type 3
Renamed
Deleted container folder
Type 4
Deleted
Created
logically
Deleted
Modified
Type 3
Deleted
Renamed
Type 3
Deleted
Deleted
Type 5
Deleted
Deleted container folder
Type 6

* hạn chế là trường hợp ít khả năng xảy ra, ví dụ Created – Modified là ít khả năng xảy ra, khi local tạo file mà không biết rằng remote đã tạo file, và không biết cả việc thay đổi nội dung file sau đó.
** trường hợp Created file vs Deleted parent folderCreated folder vs Deleted parent folder có thể không coi là conflict vì chúng ta sẽ phải luôn giữ các file mới tạo tại remote dù đã bị xóa tại local vì vấn đề an toàn (do local không biết nội dung file/folder mới tạo).
Notes: trường hợp loại bỏ là liệt kê theo logic và không tính là conflict à dạng Deleted vs CreatedModified (Renamed) vs Created à sẽ phải rơi vào trường hợp Created vs Created và phải giải quyết trước khi có thể xảy ra, implementation sẽ không để xảy ra những trường hợp này.

Đối với folder tương tự nhưng không có modified, chỉ còn trường hợp renamed
Local
Remote
Notes
Created
Created
Type 7
Created
Renamed
Hạn chế *
Created
Deleted
Hạn chế *
Created
Deleted container folder
Có thể không coi là conflict
Renamed
Created
logically
Renamed
Renamed
Type 8
Renamed
Deleted
Type 9
Renamed
Deleted container folder
Type 10
Deleted
Created
logically
Deleted
Renamed
Type 9
Deleted
Deleted
Type 11
Deleted
Deleted container folder
Type 12


Gom lại có tất cả 7 trường hợp conflict (cả case conflict)


1.       Created  file – Created file conflict

Tạo file khi không biết thông tin về file đã được tạo

2.       Modified file – Modified file conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết file đã bị thay đổi

3.       Modified file – Deleted file conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết chính file đó bị xóa

4.       Modified file – Deleted parent folder conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết folder chứa file (parent hoặc ancestor đã bị xóa)

5.       Deleted file – Deleted file conflict

Xóa file khi không biết chính file đã bị xóa

6.       Deleted file – Deleted parent folder conflict

Xóa file khi không biết folder chứa file (parent hoặc ancestor đã bị xóa)

7.       Case conflict

Trên Mac và Windows không phân biệt hoa thường trong khi trên Linux có phân biệt à case conflict giải quyết à force rename?
Dựa trên các trường hợp có thể detect conflict phân thành theo file và theo folder
DetectFileConflict(…) kiểm tra conflict từ 1 đến 6
DetectFolderConflict(…) kiểm tra conflict đối với folder

Phát hiện conflict

ü  Khi download notice changes từ server, pause sync, kiểm tra giữa 2 file list để xác định conflict về file với Type từ 1 đến 6.
ü  Khi thực hiện sync up lên server à phát hiện conflict do thực hiện thất bại và theo error code trả về.
Xét với từng loại
ü  Type 1: xác định cùng file name nhưng khác file size, modified date à conflict
ü  Type 2 & Type 3: Modified file vs Modified file và Modified file vs Deleted file
ü  Type 4: là conflict

ü  Type 5 & 6: có thể không coi là conflict

Startup syncing

Download server snapshot

Thực hiện download snapshot file và folder list trên server insert vào table snapshot tại client. Table snapshot giữ thông tin mới nhất về file và folder, chỉ download 1 lần khi start app. Table snapshot cấu trúc giống table signstore.
Table signstore dung để keep track thay đổi khi app running, mọi thay đổi nếu đã sync với server sẽ được update vào signstore.

Có thể dùng thuật toán Eugene Myers?

Tìm danh sách file chỉ có tại local và danh sách file chỉ có tại server?

Coi thông tin trong stored là chuẩn à tính diff của local files và server files để có thông tin thay đổi của local và server đối với stored.

Tính diff giữa local và stored à local deleted files, no changes và local added files.
Tính diff giữa server và stored à server deleted files, no changes và server added files.

Xử lý các trường hợp xảy ra như sau:



Giải thích:
1 + 4: local added
2 + 5: local both
3 + 6: local deleted
4 + 7: server added
5 + 6: server both
2 + 3: server deleted

1.      Local added (1 + 4)  và server added (4 + 7)

Local add bắt buộc sync up, server added bắt buộc sync down. Local va server cùng added à conflict.
1: created sync up
7: created sync down
4: conflict

2.      Local added (1 + 4) và server both (5 + 6) à không intersect

3.      Local added (1 + 4)  và server deleted (2 + 3) à không intersect

4.      Local both (2 + 5)  và server added (4 + 7) à không intersect

5.      Local both (2 + 5)  và server both (5 + 6) à không chọn vì cả 2 list cùng có nhiều items

Nếu có cả server và local kiểm tra modified cho phần 5: có cả tại local, stored và server
5: check modified

6.      Local both (2 + 5)  và server deleted (2 + 3)

2: deleted sync down
3: delete khỏi stored (có trong stored nhưng không có tại local và server)
5: check modified à làm cho case 5

7.      Local deleted (3 + 6)  và server added (4 + 7) à không intersect

8.      Local deleted (3 + 6)  và server both (5 + 6) à server both nhiều items

6: deleted sync up

9.      Local deleted (3 + 6)  và server deleted (2 + 3)

2: deleted sync down
3: không cần sync deleted à xóa khỏi stored
6: deleted sync up à làm cho case 8




2012-04-24

Multiplayer HTML5 game with Node

Tối nay lang thang 1 vòng coi có game engine gì hay không, nói chung vẫn chưa có gì mới nhiều.
Mình vẫn muốn và đang tìm để cùng vài người chế 1 game gì đó dạng 2D isometric MMORPG. Hy vọng sắp tới WebSocket sẽ OK hơn cũng như có nhiều engine phát triển hơn.

Thiệt ra bao giờ một mình mà đủ thời gian và resource làm được 1 cái như BrowserQuest http://browserquest.mozilla.org đã là kỳ công lắm rồi nhưng chẳng lẽ chỉ show off như Mozilla.

Muốn có thể làm được cái gì đó nhưng tự hiểu một mình ko đủ khả năng và quan trọng là không để làm gì. Hiện tại thì vẫn là "giấc mơ" ... :D.

2012-03-18

EventAggregator without Prism

Nếu trước đây vài năm, ai đã từng dùng EventBroker của CAB/SCSF và sau đó là EventAggregator của Prism thì không khó khăn gì nhận ra sự linh hoạt của nó so với cách viết thông thường. Tuy nhiên CAB/SCSF EventBroker khó tách ra thành 1 project, hơn nữa cách tiếp cận WorkItem và attribute có vẻ không hợp với các IoC container library sau này.

Dạo gần đây khi review 1 số dự án junior viết, mình nhận thấy code nhất là phần UI rất lộn xộn và không thể nào đọc được. Hầu như không junior nào có khái niệm phải thay đổi cách tổ chức. Nếu làm lại hết tất cả từ đầu thì không khả thi vì vậy mình tìm các apply 1 phần nhỏ để chỉnh lại đám UI code.

Tất nhiên EventAggregator của Prism theo mình nghĩ sẽ là source code hay nhất có thể dễ dàng modified và đưa vào project đã có. Nếu ai đã làm quen với Prism hay thậm chí chỉ cần biết qua về IoC contaienr library nào đó dễ thấy rằng việc đưa EventAggregator vào có thể dễ dàng dọn dẹp được rất nhiều code garbage. Đây là mô tả của Martin Flower http://martinfowler.com/eaaDev/EventAggregator.html.

OK xem thử code EventAggregator của Prism. Thực sự sau khi xem thì cũng không có nhiều việc phải chỉnh. Hơn nữa license của Prism cũng không cấm modified http://compositewpf.codeplex.com/license. Chỉ cần copy các files trong v4/PrismLibrary/Desktop/Prism/Events. Có 1 phần nhỏ để chỉnh lại là DefaultDispatcher.Desktop.cs. Class DefaultDispatcher dùng để marshal code trong trường hợp thread cần marshal đến là UI thread. Trước khi modify PL cũng search qua 1 số code trên Internet nhưng 1 số code lại comment luôn phần dispatcher trong trường hợp scope là UI. Tuy nhiên cũng không khó khăn gì nếu dùng SynchronizationContext như cách BackgroundWorker hay EventBroker của CAB.

Trong với WPF đơn giản code như sau:

if (Application.Current != null)
{
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
}

Nếu dùng code với console app hay WinForms thì cần thay bằng SynchronizationContext. Nếu muốn tìm hiểu SynchronizationContext là gì thì trên CodeProject có bài 2 phần giải thích về class này http://www.codeproject.com/Articles/31971/Understanding-SynchronizationContext-Part-I.

Code sau khi chỉnh như sau:

namespace Microsoft.Practices.Prism.Events
{
    /// Wraps the Application Dispatcher.

    public class DefaultDispatcher : IDispatcherFacade    {
        /// Forwards the BeginInvoke to the current application's .

        /// method Method to be invoked.
        /// arg Arguments to pass to the invoked method.
        public void BeginInvoke(Delegate method, object arg)
        {
#if WPF
            if (Application.Current != null)
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, method, arg);
            }
#else

            if (SynchronizationContext.Current != null)
            {
                SynchronizationContext.Current.Send(data =>
                {
                    try
                    {
                        ((Delegate)data).DynamicInvoke(arg);
                    }
                    catch (TargetInvocationException ex)
                    {
                        Debug.WriteLine("There is an exception when marshal to UI thread: " + ex.Message);
                    }
                }, method);
            }
            else
            {
                try
                {
                    method.DynamicInvoke(arg);
                }
                catch (TargetInvocationException ex)
                {
                    Debug.WriteLine("There is an exception when marshal to UI thread: " + ex.Message);
                }
            }
#endif
        }
    }
}

2012-03-05

Flex/Flash - Security sandbox violation

Error #2010: Local-with-filesystem SWF files are not permitted to use sockets.
Add this configuration setting -use-network=false
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html