Thursday, 5 September 2013

How to handle a Client/Server application for sending message and/or files

How to handle a Client/Server application for sending message and/or files

I have done a lot of searches around here, but I haven't found anything
that matches even a bit of what I need.
I'm not so expert in client/server application, and this is my first
attempt, so, please, be patient if I have made some error or if I made
silly questions.
Now, back to my problem..
I need to build a multi-client / server application.
The server side should be a simple manager for the clients.
So, for example:
the server can change some clients parameters
accepts files from the clients
other boring stuff.. that basically can be translated in sending string or
sending files or getting string and files
The clients, in the other hand, is a complex application (at least, for
the user) that should send to the server:
some user registration data (no need for security stuff here, I'm already
pretty messed up with a simple client/server connection)
a PSD file that should be the result of the user work, so when the user
click "I'm done" the application get this PSD file and send it to the
Server for storing purpose
other client information such us the default configuration data, and so on..
So, my question is basically this: How can I handle the communication from
the server with one specific client? I mean, I have the server up, and I
what to change a configuration just for one client.
I suppose I need to store the clients in some way.. like in an array (a
List), but I don't know if this is the right way to do this. (Basically I
dont't know how the classes Socket and ServerSocket works.. if this can
help you to better understand)
Also, when the server is up and listen.. the GUI need to be updated to
show the new connected clients, so I need some kind of listener to the
server that fires an action back to the interface when a new Client shows
up? (A lot of people uses the while(true) { socket = server.accept(); }
method, but this doesn't sound very smart to me..)
This is the basic Client.java and Server.java files that contains the
client and server basic functions that I wrote based on a lots of Google
searches.
But all of the code below doesn't math all my needs..
Client.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends Socket {
private static Client instance = null;
/**
* The main init() function for this class, to create a Singleton
instance for the Client
*
* @param host
* The host of the Server
* @param port
* The port of the Server
* @return The Client instance that is a new instance if no one exists
previusly,
* otherwise an older instance is returned
* @throws UnknownHostException
* @throws IOException
*/
public static Client init( String host, Integer port ) throws
UnknownHostException, IOException
{
if ( Client.instance == null )
Client.instance = new Client( host, port );
return Client.instance;
}
/**
* Default Constructor made private so this class can only be
instantiated by the
* singleton init() function.
*
* @param host
* The host of the server
* @param port
* The port of the server
* @throws UnknownHostException
* @throws IOException
*/
private Client( String host, Integer port ) throws
UnknownHostException, IOException
{
super( host, port );
}
/**
* Function used to send a file to the server.
* When this function fires, the Client class start sending a file to
the server.
* Internally this function handles the filesize, and some other file
information
* that the server needs to store the file in the correct location
*
* @param filename
* The filename of the file that will be sended to the server
*/
public void sendFile( String filename ) throws FileNotFoundException,
IOException
{
// The file object from the filename
File file = new File( filename );
// A string object to build an half of the message that will be
sent to the exceptions
StringBuilder exception_message = new StringBuilder();
exception_message.append( "The File [" ).append( filename
).append( "] " );
// Check if the file exists
if ( !file.exists() )
throw new FileNotFoundException( exception_message + "does not
exists." );
// Check if the file size is not empty
if ( file.length() <= 0 )
throw new IOException( exception_message + "has zero size." );
// Save the filesize
Long file_size = file.length();
// Check if the filesize is something reasonable
if ( file_size > Integer.MAX_VALUE )
throw new IOException( exception_message + "is too big to be
sent." );
byte[] bytes = new byte[file_size.intValue()];
FileInputStream fis = new FileInputStream( file );
BufferedInputStream bis = new BufferedInputStream( fis );
BufferedOutputStream bos = new BufferedOutputStream(
this.getOutputStream() );
int count;
// Loop used to send the file in bytes group
while ( ( count = bis.read( bytes ) ) > 0 )
{
bos.write( bytes, 0, count );
}
bos.flush();
bos.close();
fis.close();
bis.close();
}
/**
* Function used to send string message from client to the server
*
* @param message
* The string message the server should get
* @throws IOException
*/
public void sendMessage( String message ) throws IOException
{
OutputStream os = this.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter( os );
BufferedWriter bw = new BufferedWriter( osw );
bw.write( message );
bw.flush();
}
/**
* Function used to get a message from the Server
*
* @return The message the server sent back
* @throws IOException
*/
public String getMessage() throws IOException
{
InputStream is = this.getInputStream();
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String message = br.readLine();
return message;
}
}
Server.java
package contestapp.server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends ServerSocket {
private static Server instance = null;
private Socket socket = null;
/**
*
* @param port
* @return
* @throws IOException
*/
public static Server init( Integer port ) throws IOException
{
if ( Server.instance == null )
Server.instance = new Server( port );
return Server.instance;
}
/**
*
* @param port
* @throws IOException
*/
private Server( Integer port ) throws IOException
{
super( port );
// Maybe this is something that needs to be improved
while ( true )
this.socket = this.accept();
}
/**
*
* @param message
* @throws IOException
*/
public void sendMessage( String message ) throws IOException
{
OutputStream os = this.socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter( os );
BufferedWriter bw = new BufferedWriter( osw );
bw.write( message );
bw.flush();
}
/**
*
* @return
* @throws IOException
*/
public String getMessage() throws IOException
{
InputStream is = this.socket.getInputStream();
InputStreamReader isr = new InputStreamReader( is );
BufferedReader br = new BufferedReader( isr );
String message = br.readLine();
return message;
}
}
aehm.. apologize for my english.. please.

Show multiple lists in a container with sencha touch 2.1.0

Show multiple lists in a container with sencha touch 2.1.0

Descriptions:
I have a view with a container named 'topContainer'.The items of the
container consists of a titlebar and a container named 'mainContainer'.
The container 'mainContainer' is vertically scrollable and its items
consists of a panel called 'titlepanel',a button named 'btn1' ,a list
named 'list1',a button named 'btn2' ,a list named 'list2',a button named
'btn3' ,a list named 'list3',a button named 'btn4' ,a list named 'list4',a
fieldset with textareafield named 'input'.
The buttons are used to toggle a show/hide effect of the list next to each
of them.And i want all of the lists show at first and the lists stretch
full height of its data config(That is ,if the data has one item,the list
takes up the height of one item;if the data has three items,the list will
take up the height of three items).
All the list disables scroll(That's why i make the container
'mainContainer' vertically scrollable).
My problems:
The lists will show only when flex config be set,so the the height of the
list will not stretch to the height of the data it has.
The scrollable:false config of list dose not work.The
scrollable:{disabled:true} will work.
The container 'mainContainer' dose not stretch to the actual height but
always to the fullscreen height,but i want a long container enough to
display all the data in the lists.

Wednesday, 4 September 2013

How to index data using elasticSearch in grails without domain class

How to index data using elasticSearch in grails without domain class

I have to index the data in elastic search using grails.
I have only used to index the domain class but i want to do it without
domain class is there any way to do it?
So at the end i just want a code from which i can index data to
elasticsearch and make sure that data is not the domain class object. it's
coming from file or in json.
I have below code by which i can index the domain calss object in
elasticsearch
// Index all searchable instances
elasticSearchService.index()
// Index a specific domain instance
MyDomain md = new MyDomain(value:'that')
md.save()
elasticSearchService.index(md)
// Index a collection of domain instances
def ds = [new MyDomain(value:'that'), new MyOtherDomain(name:'this'), new
MyDomain(value:'thatagain')]
ds*.save()
elasticSearchService.index(ds)
// Index all instances of the specified domain class
elasticSearchService.index(MyDomain)
elasticSearchService.index(class:MyDomain)
elasticSearchService.index(MyDomain, MyOtherDomain)
elasticSearchService.index([MyDomain, MyOtherDomain])

How can i adjust the size of QStandardItemModel and Tableview using style sheet

How can i adjust the size of QStandardItemModel and Tableview using style
sheet

I created a table using table view with the help of QStandardItemModel .
How can i adjust the size of QStandardItemModel and Tableview using style
sheet in QT.

Aptana Studio 3.4 and textwrap

Aptana Studio 3.4 and textwrap

I'm running Aptana Studio 3, build: 3.4.2.201308081805
The whole word wrap thing seems to be inconsistent. Prior to a recent
upgrade, the word wrap was easy... right click in the editor pane and
choose word wrap.
Now that option has disappeared.
But if I am editing a .txt file then voila, it's back again.
Is there a way to perhaps configure Aptana to explicitly do word wrap on
.php files? Or maybe just offer word wrap everywhere. Full stop.
Thanks in advance.

How to config email with custom domain?

How to config email with custom domain?

I will explain it quickly: somebody gave me an email account
XXXXX@customdomain.com, he entered in Gmail and I don't know how he set up
this account, my question is, using the stock LG mail app how can I set up
it?
I have to choose between Imap4 and Pop3 then he ask me imap server, smtp
server and ports.
What should I do?

Update numbers in table with variable and update variable

Update numbers in table with variable and update variable

As you can see from the table below the user with the id of 1 has 10
points altogether
What I'm trying to do is have one number submitted from a form
$edit_points to loop through and subtract from user 1
This number could be anything eg:
$edit_points = 6
would subtract 4 from the first record
then move to the next and only subtract the remainder 2
then break out of loop
UserID | TotalPoints
-------------------
1 | 4
1 | 4
1 | 2
4 | 3
3 | 3
5 | 4
This is what I've got so far
$pointLeft = $edit_points;//sent from form
while($point = mysqli_fetch_array($output)){
if($pointLeft > $point["TotalPoints"]){//if pointsLeft more than this record
$remainder = $pointLeft-$point["TotalPoints"];//get remainder
$query2 = "UPDATE pointstable SET TotalPoints=0 WHERE UserID=".$UserID;
$output2 = $mysqli->query($query2);
}elseif($pointLeft < $point["TotalPoints"]){
$remainder = $point["TotalPoints"] - $pointLeft;
$query2 = "UPDATE pointstable SET TotalPoints=".$remainder." WHERE
UserID=".$UserID;
$output2 = $mysqli->query($query2);
}
$pointLeft = $remainder;
}
It works to a point and seems to populate all the records with the same
last value that remainder holds I've tried break and putting an if
($pointLeft<=0) above the first if
but none works.
any help? or a better way of doing it preferably using php