Real-Time Applications with ASP.NET SignalR

My co-worker introduced me to SignalR for ASP .NET. A very cool library to do real-time applications. I’ve done a messaging app and other real time components in the past manually with different tools and technologies but this is cool :). Check it out :)!

Microsoft ASP .NET Official Overview on SignalR

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2014/DEV-B416/player?h=540&w=960&format=flash

Advertisements

SharePoint 2010 Full Text SQL Query – Ranged query results through Search Web Service

If you are wondering how to do a ranged search query through the SP search service pass to the query service something below where the ranged functionality is in the green color. Just specify the start position at the StartAt, notice that the minimum start position is number one not zero. Then you pass in the Count part how many items to retrieve starting from the specified location. To do a paging like functionality you have to keep count in you code what is your paging range, current position and what to retrieve.

<QueryPacket><Query domain=”QDomain”><Context><QueryText type=”MSSQLFT”><![CDATA[ SELECT your fields here FROM Scope() WHERE IsDocument = 1 AND “SCOPE” = ‘your scope name’ AND CONTAINS (some sample field,’field data to contain’) ORDER BY some field name]]></QueryText></Context><Range><StartAt>1</StartAt><Count>3</Count></Range><TrimDuplicates>false</TrimDuplicates></Query></QueryPacket>

 

C# .NET Getting Windows Directory File Permissions programmatically

Hi,

 

I while back I needed to do a security trim on files in a Windows file system based on returned search results by SharePoint search. Since in SharePoint 2010 search indexing does not know how to take into consideration file system rights for searches there was a need to do a security trimming based on what privileges you have in Active Directory and what you are given to the file itself.

Since there where alot of moving parts and hard to find out which classes and function where needed to actually do this specific task I finally found a source that gave a great sample how to do this:

http://www.conarc.com/blog/2010/03/25/programmatically-getting-effective-directoryfile-permissions/

Unfortunately the source above does not exist anymore for whatever reason. So for those who might need similar functionality through code here is the sample code from the link above and what you need class wise.

In the code below what you need is to call the following static function to check for if the user has certain privileges:

FileSystemRights rights = FileSystemEffectiveRights.GetRights(username, filelocation);

Then call the following function to test against the given file system privileges and what you want to user to have in the file system.
bool canReadExecute = rights.HasRights(FileSystemRights.ReadAndExecute);

 

Classes and enumeration needed for this functionality(there are many moving parts here and you might have to work with Active Directory and File System to test this code):

FileSystemRights Enumeration

FileSystemAccessRule Class

AccessControlType Enumeration

AuthorizationRuleCollection Class

SecurityIdentifier Class

FileSystemAccessRule Class

FileInfo Class

PrincipalContext Class

UserPrincipal Class

PrincipalSearcher Class

WindowsIdentity Class

 

Sample code – http://www.conarc.com/blog/2010/03/25/programmatically-getting-effective-directoryfile-permissions/ :

public static class FileSystemRightsEx
{
public static bool HasRights(this FileSystemRights rights, FileSystemRights testRights)
{
return (rights & testRights) == testRights;
}
}

public static class FileSystemEffectiveRights
{

public static FileSystemRights GetRights(string userName, string path)
{
if (string.IsNullOrEmpty(userName))
{
throw new ArgumentException(“UserName not defined!”);
}

//if (!Directory.Exists(path) && !File.Exists(path))
//{
// throw new ArgumentException(string.Format(“path: {0}”, path));
//}

return GetEffectiveRights(userName, path);
}

private static FileSystemRights GetEffectiveRights(string userName, string path)
{
FileSystemAccessRule[] accessRules = GetAccessRulesArray(userName, path);
FileSystemRights denyRights = 0;
FileSystemRights allowRights = 0;

for (int index = 0, total = accessRules.Length; index < total; index++)
{
FileSystemAccessRule rule = accessRules[index];

if (rule.AccessControlType == AccessControlType.Deny)
{
denyRights |= rule.FileSystemRights;
}
else
{
allowRights |= rule.FileSystemRights;
}
}

return (allowRights | denyRights) ^ denyRights;
}

private static FileSystemAccessRule[] GetAccessRulesArray(string userName, string path)
{
// get all access rules for the path – this works for a directory path as well as a file path
AuthorizationRuleCollection authorizationRules = (new FileInfo(path)).GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));

// get the user’s sids
string[] sids = GetSecurityIdentifierArray(userName);

// get the access rules filtered by the user’s sids
return (from rule in authorizationRules.Cast<FileSystemAccessRule>()
where sids.Contains(rule.IdentityReference.Value)
select rule).ToArray();
}

private static string[] GetSecurityIdentifierArray(string userName)
{
// connect to the domain
PrincipalContext pc = new PrincipalContext(ContextType.Domain);

// search for the domain user
UserPrincipal user = new UserPrincipal(pc) { SamAccountName = userName };
PrincipalSearcher searcher = new PrincipalSearcher { QueryFilter = user };
user = searcher.FindOne() as UserPrincipal;

if (user == null)
{
throw new ApplicationException(string.Format(“Invalid User Name: {0}”, userName));
}

// use WindowsIdentity to get the user’s groups
WindowsIdentity windowsIdentity = new WindowsIdentity(user.UserPrincipalName);
string[] sids = new string[windowsIdentity.Groups.Count + 1];

sids[0] = windowsIdentity.User.Value;

for (int index = 1, total = windowsIdentity.Groups.Count; index < total; index++)
{
sids[index] = windowsIdentity.Groups[index].Value;
}

return sids;
}
}

Things learned from Objective-C – Cocos2D porting and testing in Swift – Part 2

More things learned from playing around with Swift, Objective-C and Cocos2D

Interoperability between Objective-C and Swift – Initialize patterns

If you have in you Objective-C code an Initialize Pattern to access it in Swift you just use the class name and access the initialize pattern as you would access a static function. Just remember to downcast with the ‘as’ operator returning the class from which you are using the initialization pattern.  The green code below:

var moveToLeftEdge : CCActionMoveTo = CCActionMoveTo(duration: CCTime(birdTime), position: screenLeft);
var turnaround : CCActionFlipX = CCActionFlipX(flipX: true);
var moveBackOffScreen : CCActionMoveTo = CCActionMoveTo(duration: CCTime(birdTime), position: birdStart);
var birdActions = Array<CCAction>();
birdActions.append(moveToLeftEdge);
birdActions.append(turnaround);
birdActions.append(moveBackOffScreen);
birdActions.append(turnaround);

var moveLeftThenBack : CCActionSequence = CCActionSequence.actionWithArray(birdActions) as CCActionSequence;

 

Interoperability between Objective-C and Swift – Objective-C Selectors

To access Objective-C selectors in Swift you define a constant value with the value being the function name to call within your class. See green code below:

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_38

let mySelectorTurnAround: Selector = “turnAround”;
var turnArround : CCActionCallFunc = CCActionCallFunc.actionWithTarget(bird, selector: mySelectorTurnAround) as CCActionCallFunc;

 

Objective-C Struct definition usage and initialization

If you are get the following error message when using struct definitions in Objective-C then the solution is to initialize the struct before usage. Below is an example of than in the green color.

Error Message: Struct ‘struct name’ must be completely initialized before a member is stored to

Original Objective-C Struct definition:

// Bezier configuration structure.
typedef struct _ccBezierConfig {
// End position of the bezier.
CGPoint endPosition;
// Bezier control point 1.
CGPoint controlPoint_1;
// Bezier control point 2.
CGPoint controlPoint_2;
} ccBezierConfig;

In Swift Code Usage

var curve : ccBezierConfig = ccBezierConfig(endPosition: CGPointMake(0, 0), controlPoint_1: CGPointMake(0, 0), controlPoint_2: CGPointMake(0, 0));

Things learned from Objective-C – Cocos2D porting and testing in Swift – Part 1

Subclass & Initializers

This is a Swift language ralated “issue”, well it is a language feature. Personally I find it annoying. If you find yourself with the following error:

fatal error: use of unimplemented initializer ‘some initializer name of your class in which you operate in’ for class ‘your class name

Then you probably are dealing with an initialize problem. This is by default a Swift language feature. In the following link this is explained very well: http://www.codeproject.com/Articles/783584/Subclassing-Objective-C-classes-in-Swift-and-the-p

Quoted from the above link: “Subclassing Objective-C classes in Swift and the perils of Initializers – CodeProject// //

Unlike subclasses in Objective-C, Swift subclasses do not not inherit their superclass initializers by default. Swift’s approach prevents a situation in which a simple initializer from a superclass is automatically inherited by a more specialized subclass and is used to create a new instance of the subclass that is not fully or correctly initialized.

If you want your custom subclass to present one or more of the same initializers as its superclass—perhaps to perform some customization during initialization—you can provide an overriding implementation of the same initializer within your custom subclass.

This explains it. Basically, in Swift, initialization methods work like virtual functions in fact super virtual functions. If a super class initializer is invoked and that in turns calls another initializer (designated or not) then it forwards the call to the derived class. The upshot of this seems to be that for any derived class in Swift it would need to re-implement all of the super classes initializers or at least any which may be invoked from the the other initializers.

The possible solution are that you call each initializer from the super class or use directly the initializer that you definitely are going to use.

To create Formated strings you can use the following piece of code(The green bold text):

var animFrameNameFormat : String;

switch self.birdType
{
case .BirdTypeBig:
animFrameNameFormat = “bird_big_%d.png”;

case .BirdTypeMedium:
animFrameNameFormat = “bird_middle_%d.png”;

case .BirdTypeSmall:
animFrameNameFormat = “bird_small_%d.png”;

default:
animFrameNameFormat = “bird_small_%d.png”;

}

var animFrames = [CCSpriteFrame]();

for (var i = 0; i < 7 ; i++)
{
var currentFrameName = String(format: animFrameNameFormat, i);

var animationFrame = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName(currentFrameName);
animFrames.append(animationFrame);
}

In this example basically you call the String class to create a string formatted with an integer number that indicates which image to load. Pay also attention to the text notations where the integer is inserted at( the red bold text). You can use Objective-C formaters in swift: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/Strings/Articles/formatSpecifiers.html

Cocos2D Macros as Extension in Swift:

For some reason(I am not fluent in Objective-C, Swift or XCode) in swift the macros for Cocos2D game framework do not work, even when adding the Objective-C macros and extension header file. I decided to take another approach to tie struct, class and type operations as extension that can be performed on a specific chosen element. In the code below I added to the CGPoint the Cocos2d equivalent ccpSub, ccpMult, ccpDot etc operations. Notice that in order for these function to be available through the CGPoint struct the function must be set to static.

extension CGPoint
{
static func Sub(v1 : CGPoint, v2 : CGPoint) -> CGPoint
{
return(CGPointMake(v1.x – v2.x, v1.y – v2.y));
}

static func Dot(let v1 : CGPoint, let v2 : CGPoint) -> CGFloat
{
return v1.x * v2.x + v1.y * v2.y;
}

static func LengthSQ(let v: CGPoint) -> CGFloat
{
return CGPoint.Dot(v, v2: v);
}

static func Normalize(let v: CGPoint) -> CGPoint
{
return CGPoint.Mult(v, s: 1.0 / CGPoint.Length(v));
}

static func Mult(let v: CGPoint , let s: CGFloat) -> CGPoint
{
return CGPointMake(v.x * s, v.y * s);
}

static func Length(let v: CGPoint) -> CGFloat
{
return CGFloat(sqrtf(Float(CGPoint.LengthSQ(v))));
}

static func AngleSigned(a: CGPoint, b: CGPoint) -> Float
{
var a2 : CGPoint = CGPoint.Normalize(a);
var b2 : CGPoint = CGPoint.Normalize(b);
var angle : Float = atan2f(Float(a2.x * b2.y – a2.y * b2.x), Float(CGPoint.Dot(a2, v2: b2)));
if( fabs(angle) < FLT_EPSILON ) { return 0; }
return angle;
}

static func RotateByAngle(v : CGPoint, pivot: CGPoint, angle : Float) -> CGPoint
{
var r : CGPoint = CGPoint.Sub(v, v2: pivot);
var cosa : Float = cosf(angle), sina = sinf(angle);
var t : Float = Float(r.x);
r.x = CGFloat(t*cosa – Float(r.y)*sina + Float(pivot.x));
r.y = CGFloat(t*sina + Float(r.y)*cosa + Float(pivot.y));
return r;
}
}

Properties not initialized before super class initialization call

The problem is simple in swift basically you have to initialize properties before the call of super initialization.

If you do not you will get an error message similar to what you find below:

Property ‘your property name‘ not initialized at super.init call

Apple describes the situation as follows:

“Swift’s compiler performs four helpful safety-checks to make sure that two-phase initialization is completed without error:”

Safety check 1 “A designated initializer must ensure that all of the “properties introduced by its class are initialized before it delegates up to a superclass initializer.”

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/

 Solution: Use Swift functionality related to optionals to get around the problem, that is: the exclamation mark ! and the question mark ?:

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html

 

Some sample code below, the solution is marked with the green color:

class GameScene : CCScene

{
var hunter : Hunter!;
var bird : Bird!;
var birdsCount = 0;
var batchNode : CCSpriteBatchNode!;

override init()
{

super.init()
userInteractionEnabled = true
self.createBatchNode();

self.addHunter();

}
func addHunter()
{
self.hunter = Hunter();
var viewSize = CCDirector.sharedDirector().viewSize();
var hunterPositionX = viewSize.width * 0.5 – 250;
var hunterPositionY = viewSize.height * 0.3;
self.hunter.position = CGPointMake(hunterPositionX, hunterPositionY);
self.batchNode.addChild(self.hunter);
}

}

 

public class Hunter : CCSprite
{
var torso : CCSprite?;

override init() {

super.init(imageNamed: “hunter_bottom.png”);
self.torso = CCSprite(imageNamed: “hunter_top_0.png”);
self.torso!.anchorPoint = CGPointMake( 0.5,10/44);
self.torso!.position = CGPointMake(self.boundingBox().size.width/2, self.boundingBox().size.height);
self.addChild(torso, z: -1);
}

}

Swift Objective-C and Cocos2d

I got interested in making something for my iPad and play around with Swift. I decided to use the Cocos2d Open Source Framework but because it is written in Objective-C I was puzzled how to use it properly in Swift code. Thanks to Apple documentation there is a solution.

To use Cocos2d code within a Swift project follow these instructions in the Apple documentation.

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html