Monogame 3.0 GamePage.xaml

by sunil ravulapalli /9. November 2013 07:01 /monogame /Comments (0)
<phone:PhoneApplicationPage
    x:Class="Tizzle.GamePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Landscape"
    shell:SystemTray.IsVisible="False">

    <DrawingSurfaceBackgroundGrid x:Name="XnaSurface" Background="Transparent">
        <MediaElement x:Name="XnaMediaElement" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </DrawingSurfaceBackgroundGrid>

</phone:PhoneApplicationPage>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework;
using MonoGame.Framework.WindowsPhone;
using Tizzle.Resources;

namespace Tizzle
{
    public partial class GamePage : PhoneApplicationPage
    {
        private Tizzle _game;

        // Constructor
        public GamePage()
        {
            InitializeComponent();

            _game = XamlGame.Create("", this);

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}

Cisco call studio : Java code for creating custom element which parses JSON

by sunil ravulapalli /5. November 2013 08:07 /cisco-voice-programming /Comments (0)
import com.audium.server.AudiumException;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.xml.ActionElementConfig;
import com.audium.server.session.ActionElementData;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class GetFieldFromJSONElement extends ActionElementBase implements ElementInterface 
{
    public String getElementName()
    {
        return "GetFieldFromJSON";
    }

    public String getDisplayFolderName()
    {
        return "Company Elements";
    }

    public String getDescription() 
    {
        return "This element gets the value of the required field in a JSON string";
    }
    
	public Setting[] getSettings() throws ElementException 
	 {	
		 Setting[] settingArray = new Setting[4];
		 
		 settingArray[0] = new Setting("json", 
				 						"JSON", 
						   				"This is the input JSON",
						   				true,
						   				true,
						   				true,
						   				Setting.STRING);
		 
		 settingArray[1] = new Setting("field", 
					"Field", 
	   				"This is the input JSON",
	   				true,
	   				true,
	   				true,
	   				Setting.STRING);
		 
		 settingArray[2] = new Setting("resultType", 
				 						"Result Type", 
				   						"Choose where to store result \n" +
				   						"into Element or Session data",
				   						true,
				   						true,
				   						false,
				   						new String[]{"Element","Session"});
		settingArray[2].setDefaultValue("Session");		
		
		settingArray[3] = new Setting("resultName", 
										"Result Name", 
										"Name of variable to hold the result.",
										true,
										true,
										true,
										Setting.STRING);	
		settingArray[3].setDefaultValue("resultString");

		return settingArray;
	 }

    public ElementData[] getElementData() throws ElementException 
    {
        return null;
    }
    
	public void	doAction(String name, ActionElementData actionData) throws AudiumException
	{
		ActionElementConfig config = actionData.getActionElementConfig();			
		String json = config.getSettingValue("json",actionData);
		String field = config.getSettingValue("field",actionData);
		String resultType = config.getSettingValue("resultType",actionData);
		String resultName = config.getSettingValue("resultName",actionData);
		
		try {						
			String value = GetValueOfField(json, field);				
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,value);
			} else {
				actionData.setSessionData(resultName,value);
			} 
			actionData.setElementData("status","success");
		} catch (Exception e) {
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,e.toString());
			} else {
				actionData.setSessionData(resultName,e.toString());
			}
			
			actionData.setElementData("status","failure");
		}		
	}

	public String GetValueOfField(String json, String field) throws Exception
	{   
		JSONParser parser = new JSONParser();
		json = json.replaceAll("'", "\"");
		
		JSONObject jsonObject =  (JSONObject) parser.parse(json);
		
		Object val = jsonObject.get(field);
	    
		return val.toString();
	}
}

Cisco call studio : Java code for creating a custom element which can call a REST URL

by sunil ravulapalli /5. November 2013 08:03 /cisco-voice-programming /Comments (0)
import com.audium.server.AudiumException;
import com.audium.server.voiceElement.ActionElementBase;
import com.audium.server.voiceElement.ElementInterface;
import com.audium.server.voiceElement.Setting;
import com.audium.server.voiceElement.ElementData;
import com.audium.server.voiceElement.ElementException;
import com.audium.server.xml.ActionElementConfig;
import com.audium.server.session.ActionElementData;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*;

public class GetUrlElement extends ActionElementBase implements ElementInterface 
{
    public String getElementName()
    {
        return "GetUrl";
    }

    public String getDisplayFolderName()
    {
        return "Company Elements";
    }

    public String getDescription() 
    {
        return "This element does a GET on a given URL and returns a result JSON string";
    }
    
	public Setting[] getSettings() throws ElementException 
	 {	
		 Setting[] settingArray = new Setting[3];
		 
		 settingArray[0] = new Setting("url", 
				 						"URL", 
						   				"This is the URL to call",
						   				true,
						   				true,
						   				true,
						   				Setting.STRING);
		 
		 settingArray[1] = new Setting("resultType", 
				 						"Result Type", 
				   						"Choose where to store result \n" +
				   						"into Element or Session data",
				   						true,
				   						true,
				   						false,
				   						new String[]{"Element","Session"});
		settingArray[1].setDefaultValue("Session");		
		
		settingArray[2] = new Setting("resultName", 
										"Result Name", 
										"Name of variable to hold the result.",
										true,
										true,
										true,
										Setting.STRING);	
		settingArray[2].setDefaultValue("resultString");

		return settingArray;
	 }

    public ElementData[] getElementData() throws ElementException 
    {
        return null;
    }
    
	public void	doAction(String name, ActionElementData actionData) throws AudiumException
	{
		ActionElementConfig config = actionData.getActionElementConfig();			
		String url = config.getSettingValue("url",actionData);
		String resultType = config.getSettingValue("resultType",actionData);
		String resultName = config.getSettingValue("resultName",actionData);
		try {						
			String responseBody = GetUrl(url);	
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,responseBody);
			} else {
				actionData.setSessionData(resultName,responseBody);
			} 
			actionData.setElementData("status","success");
		} catch (Exception e) {
			
			if(resultType.equals("Element")){
				actionData.setElementData(resultName,e.toString());
			} else {
				actionData.setSessionData(resultName,e.toString());
			}
			
			actionData.setElementData("status","failure");
		}		
	}
	
	public String GetUrl(String inputUrl) throws Exception
	{
		URL url = new URL(inputUrl);
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
		connection.setRequestMethod("GET");
		connection.connect();

		InputStream stream = connection.getInputStream();
		Reader reader = new InputStreamReader(stream);		

		StringBuilder sb = new StringBuilder();

		int data = reader.read();		

		while(data != -1){
		    char theChar = (char) data;
			if(theChar == '"')
			    sb.append("'");			
			else
				sb.append(theChar);
		    
			data = reader.read();		    		   
		}
		
		reader.close();

		return sb.toString();
	}
}

ASP.NET MVC 3 site on IIS 8 not rendering CSS and Javascript

by sunil ravulapalli /23. October 2013 06:42 /asp.net-mvc /Comments (0)

Recently, I had this problem where my MVC 3 site on IIS 8 was not rendering correctly. In fact amazingly when I checked fiddler those requests were throwing HTTP 500 errors. After, unsuccessfully searching online for a solution, I resorted to trial and error editing of the web.config file.

Previously, to add MIME types to IIS, I had this section in the web.config.

<staticContent> 
  <mimeMap fileExtension=".mp4" mimeType="video/mp4" /> 
  <mimeMap fileExtension=".ogv" mimeType="video/ogg" />
</staticContent>

However, this doesn't seem to be sitting well with IIS 8. Once, I removed that section all else worked fine.

Cisco unified call studio 9.0 debugger always showing error

by sunil ravulapalli /19. October 2013 01:40 /cisco-voice-programming /Comments (0)

If your old call studio application used to work fine and even the simplest of applications don't seem to run in the Cisco unified call studio 9.0 debugger then you may need a mail.jar file.

SYMPTOMS

This the error you must be getting every time

Connecting...
Prompt: I'm sorry, there was a problem with this application. Please call back later. Thank you. (/CVP/audio/error.wav)
Disconnected.

The actual error which could give you a hit what may be happening is in this file startup_error_logYYYY-MM-DD.txt in this folder C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.debug.runtime_9.0.1-SNAPSHOT\AUDIUM_HOME\logs

SERVER ERROR: There was a problem loading classes from CVP_HOME\VXMLServer/common. The error was: There was a problem loading classes from CVP_HOME\VXMLServer/common. The root cause was: java.lang.NoClassDefFoundError: javax/mail/Authenticator

To fix this go to http://www.oracle.com/technetwork/java/index-138643.html download the latest Javamail.

Extract the contents and in the directory(javamail-1.4.x) you find a file called mail.jar.

Copy that mail.jar file to C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.debug.runtime_9.0.1-SNAPSHOT\CATALINA_HOME\webapps\CVP\WEB-INF\lib

Re-run your application. That should fix it!

Credit goes to http://developer.cisco.com/web/cvp/forums/-/message_boards/message/17876067 

Understanding how SqlConnection opens and closes connections

by sunil ravulapalli /15. October 2013 07:49 /.net /Comments (0)
When you do conn.open() IIS opens a physical connection to DB. 
When u do conn.close() IIS moves to the connection pool. 
If some request is made using the same connection string IIS will use it. 
In the same app if you do:
conn.open()
conn.close()
and then  
conn1.open() 
conn1.close()
IIS will use the same physical connection.
But if you do: 
conn1.open() 
conn2.open()
IIS will open two physical connections. 
You can return the 2 connections to pool by doing
conn1.close() 
conn2.close()
 
If you do not close connections conn1 and conn2 will remain open for a long time and wont return to connection pool. 
So if there are new connection like conn3 and conn4, IIS will have a total 4 physical connections. If your website hits traffic on black friday, there will be hundreds of such connections which will crash your site :).

Sample showing how to use Action<T> in your own code

by sunil ravulapalli /28. September 2013 06:29 /c# /Comments (0)

In the below sample you can see that Action<T> can used to reuse the function Display multiple times. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FuncTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List<Customer>();

            customers.Add(new Customer { Id = 1, Name = "aaa"});
            customers.Add(new Customer { Id = 2, Name = "bbb" });
            customers.Add(new Customer { Id = 3, Name = "ccc" });

            //Display Ids
            Action<Customer> action1 = c => Console.WriteLine(c.Id);
            Display(customers, action1);

            //Display Names
            Action<Customer> action2 = c => Console.WriteLine(c.Name);
            Display(customers, action2);

            Console.ReadLine();
        }

        static void Display(List<Customer> customers, Action<Customer> action)
        {
            foreach (var c in customers)
            {
                action.Invoke(c);
            }            
        }
    }

    internal class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Example showing how you can use Func<T> in your own functions

by sunil ravulapalli /28. September 2013 06:09 /c# /Comments (0)

In the following example you can see that the Find function can re-used multiple times if we use Func<T>

namespace FuncTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List<Customer>();

            customers.Add(new Customer { Id = 1, Name = "aaa"});
            customers.Add(new Customer { Id = 2, Name = "bbb" });
            customers.Add(new Customer { Id = 3, Name = "ccc" });

            var result1 = Find(customers, c => c.Id == 1);
            Console.WriteLine(result1.Id + " : " + result1.Name);

            var result2 = Find(customers, c => c.Name == "ccc");
            Console.WriteLine(result2.Id + " : " + result2.Name);

            Console.ReadLine();
        }

        private static Customer Find(List<Customer> customers, Func<Customer, bool> predicate)
        {
            return customers.SingleOrDefault(predicate);
        }
    }

    internal class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Example explaining Func<T>

by sunil ravulapalli /28. September 2013 05:41 /c# /Comments (0)

Below is a simple program which shows three different ways we can pass the parameter to SingleOrDefault on a List<T>.

If essentially shows what Func<T> really is. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FuncTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List<Customer>();

            customers.Add(new Customer { Id = 1, Name = "aaa"});
            customers.Add(new Customer { Id = 2, Name = "bbb" });
            customers.Add(new Customer { Id = 3, Name = "ccc" });

            //1
            var result1 = customers.SingleOrDefault(findFunction);
            Console.WriteLine(result1.Name);

            //2
            var result2 = customers.SingleOrDefault(delegate(Customer c)
                {
                    if (c.Id == 2)
                        return true;

                    return false;
                });
            Console.WriteLine(result2.Name);

            //3
            var result3 = customers.SingleOrDefault(c => c.Id == 3);
            Console.WriteLine(result3.Name);

            Console.ReadLine();
        }

        private static bool findFunction(Customer customer)
        {
            if (customer.Id == 1)
                return true;

            return false;
        }

    }

    internal class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Refractoring Code: Design for the future

by sunil ravulapalli /4. September 2013 07:17 /software-design /Comments (0)
AccountInformation accountInformation = getInfo();
string allNames = accountInformation.SignerName  + " " +  accountInformation.CoSignerName  + " " +  accountInformation.CoSignerName2;

Lets say you have a class like this which you are using in multiple places in your program:

public class AccountInformation
{
	public string SignerName { get; set; }
	public string CoSignerName { get; set; }	
}

One of the instances where it is being used is as follows:

AccountInformation accountInformation = getInfo();
string allNames = accountInformation.SignerName + " " + accountInformation.CoSignerName;

One fine day you are informed of a business rule which states that AccountInformation can have second Co Signer.
How would you redesign this class?
One option would be to do this:

public class AccountInformation
{
	public string SignerName { get; set; }
	public string CoSignerName { get; set; }
	public string CoSignerName2 { get; set; }
}

Now, next thing you have to do is find all places where AccountInformation is used and add CoSignerName2 for the additional information like follows:

public class AccountInformation
{
	public string SignerName { get; set; }
	public List<string> CoSignerNames { get; set; }	
}

This seems easy. But what if you need to add another Co Signer? Will you add CoSignerName3? This can go on endlessly.
A good programmer would recognize this possibility and he/she would have made a provision for this scenario. AccountInformation could have been changed as follows:

public class AccountInformation
{
	public string SignerName { get; set; }
	public List<string> CoSignerNames { get; set; }	
}

All the usages of AccountInformation would have to be changed to something like this:

AccountInformation accountInformation = getInfo();
string allNames = accountInformation.SignerName + getAllCoSignerNames(accountInformation.CoSignerNames);

where

public string getAllCoSignerNames(List<string> coSignerNames)
{
	string allCoSignerNames = String.Empty;
	foreach(string coSignerName in coSignerNames)
	{
		allCoSignerNames += coSignerName + " "
	}
	return allCoSignerNames;
}

Yes, this definitely more work than just adding CoSignerName2. But we have to realize that this is future proof! No matter how many Co Signers are allowable in future, you don't have to touch a line of code. You are done! This saves both the programmer and the business so much time, just because you though ahead and designed your system.