Monday, April 28, 2008

Weekend with Suse 11 Beta, FreeBSD

Last weekend I was ready to test SUSE 11 Beta ( 55 days were still to go before its release ) and FreeBSD. Installation of SUSE was great I felt like I am installing a Microsoft product, when I ran it for the first time KDE4 was really cool. I have been using Gnome since quite sometime now and recently KDE4 was a lot talked about in the air, so I wanted to give it a try.
I made some changes in the screen resolution and refresh rate. and restarted the xserver, to my surprise the taskbar didn't load, I restarted the machine but same issue, I then created a new user and the new user desktop was fine, but when I logged on to the new user again the same thing happened.
I reinstalled with Gnome, still many bugs, then I finally installed FreeBSD. as expected it was good from the optimal resource utilization angle, but user friendliness I guess Linux (especially Ubuntu) has an upper hand.

Tuesday, April 22, 2008

OpenSUSE 11

This is something I am really excited about opensuse 11, I have used and loved enough of Fedora and recently Ubuntu and was always impressed by something different in OpenSUSE, but the only reason I could not use OpenSUSE was that SUSE was not very friendly to my hardware configuration, maybe this new release bridges the gap so that people like me could use it.
Its beta version is available freely for download.

Thursday, April 17, 2008

Java based multi threaded webserver

Had nothing to do since last few days so decided to make a small webserver, it has a small configuration file also, here the code that I landed up creating
-----------------------------------------------------------------------
import java.io.*;
import java.net.*;
import java.util.Properties;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


public class myServer {

public static void main(String args[]) {

String configFileName="config.properties";
String docRoot="";
int portNumber=80;

// ---- Load configuration file. ------
try {
configFileName = args[0];
}
catch(ArrayIndexOutOfBoundsException aioobe){
//Look for default configuration file.
}


try{
Properties properties = new Properties();
properties.load(new FileInputStream(configFileName));
System.out.println("Using configuration file "+configFileName);
portNumber = Integer.parseInt(properties.getProperty("port"));
docRoot = properties.getProperty("documentRoot");
}
catch(IOException ioex){
System.out.println("\nConfiguration file not found.\nPlease specify config file to use\nExample:\n\tjava myServer "+configFileName);
return;
}

// declare a server socket and a client socket for the server
ServerSocket echoServer = null;
Socket clientSocket = null;

// declare an input and an output stream
String line="";
DataInputStream is;
PrintStream os;

// Try to open a server socket on port 9876
try {
echoServer = new ServerSocket(portNumber);
System.out.println("HTTP Server started at port number "+Integer.toString(portNumber)+", Document root as "+docRoot+"\nTo stop server press ^c");
}
catch (IOException e) {
System.out.println(e);
}

while(true){ // Server runs infinitely.

try {

clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());

for (int i=1;i<5 ;i++ ){
line = line.concat(is.readLine());
}
Runnable runnable = new HandleClient(os, line, docRoot);
Thread thread = new Thread(runnable);
thread.start();

line="";

}

catch (IOException e) {
System.out.println(e);
}

}
}
}

class HandleClient implements Runnable{
PrintStream printStream;
String clientData;
String requestFileName;
String docRoot;

public HandleClient(PrintStream in_printStream, String in_clientData, String in_docRoot){
this.printStream = in_printStream;
this.clientData = in_clientData;
this.docRoot = in_docRoot;
//Set filename
this.requestFileName = this.clientData.substring(3, this.clientData.indexOf("HTTP/1.1")).trim().replaceAll("/", "\\\\");
this.requestFileName = this.docRoot.concat(this.requestFileName);
}

public void run(){
try {
//System.out.println("About to serve "+this.requestFileName);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
File file = new File(this.requestFileName);
if (file.exists()){
//System.out.println("File Exists");
}
else{
System.out.println("File does not Exist "+this.requestFileName);
this.printStream.println("Not found.");
this.printStream.close();
return;
}

fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {
// this statement reads the line from the file and print it to
// the console.
//this.printStream.println(dis.readLine());
this.printStream.write(dis.read());
}
this.printStream.close();
// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();
//System.out.println("served "+this.requestFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
this.printStream.println("Not found.");
this.printStream.close();
return;

} catch (IOException e) {
e.printStackTrace();
this.printStream.println("Not found.");
this.printStream.close();
return;
}
}
}
---------------------------------------------------------------------------

--------- Configuration file --------------
#Configuration file
documentRoot=d:\\amit\\docroot
port=9999
errorPage=404.html

Wednesday, April 16, 2008

MySQL Stored procedure example

DELIMITER $$

DROP PROCEDURE IF EXISTS `mycash`.`addUnit` $$
CREATE PROCEDURE `mycash`.`addUnit` (in_name varchar(45),
in_type enum('Home', 'Organization', 'Community'),
in_country_code tinyint,
in_area_code smallint,
in_pincode integer,
in_street_address text
)
BEGIN
INSERT INTO `unit` (`name`, `type`, `country_code`, `area_code`, `pincode`, `street_address`, `creation_date`)
VALUES (in_name, in_type, in_country_code, in_area_code, in_pincode, in_street_address, CURRENT_DATE);
END $$

DELIMITER ;

MySQL cleaner ways to do things

Create table ..

CREATE TABLE `mycash`.`unit` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Sequence number',
`name` VARCHAR(45) NOT NULL COMMENT 'organization or home name',
`type` ENUM('Home','Organization','Community') NOT NULL DEFAULT 'Home',
`country_code` TINYINT UNSIGNED NOT NULL,
`area_code` SMALLINT UNSIGNED NOT NULL,
`pincode` INTEGER UNSIGNED NOT NULL,
`street_address` TEXT,
`creation_date` DATE NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
COMMENT = 'Top level entity, could be a house, organization or community';

Monday, April 14, 2008

Free Domain names, Free Java PHP MySQL hosting

The following free domain name registrars are in my knowledge

  1. co.cc
  2. freedomain.co.nr
  3. dot.tk

If you are also looking for a hosting support free of cost PHP, Tomcat, MySQL, then you can comment on this article, I have a server at my home and I can arrange some space over that.