summaryrefslogblamecommitdiff
path: root/client/Piztor/src/com/macaroon/piztor/SocketClient.java
blob: fe926d91f3a2bbbf1551fc8a1428521e696184d4 (plain) (tree)
1
2
3
4
5
6
7
8
9




                                 
                                   
                        
                                        
                                      
                         

                         
                                        




                              




                                           
 
                                                                                            

                                      

                                                                                
                                                     
                                                   
                                             

                                          
                                             
                                 
                   

          
                                                                                               









                                                                     
                                                                                                                                         




                                                                                 
                                                  




                                                                 
                                                                                                                                                         














                                                                                                                 
                                                                                                                                         












                                                                                                                 
                                                                                                                                         










































                                                                                                                 

                                                                           
                                                                                  
                                                    



                                                                              

                                                                                         







                                                                                      
                                                                           


                                                                     
                                                    
                                                                         
                                                            

                                                     
                                                                  
                                                                   

                                                       
                                                                   
                                                                    


                                                       
                                                                                    




                                                         
                                  
 


                                                         

                                          
                   










                                                      
                                       



                                                                   
                                                     














                                                                                                                
package com.macaroon.piztor;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Locale;
import java.util.Vector;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

public class SocketClient {
	static Socket client;
	
	static final int ByteLength = 1;
	static final int IntLength = 4;
	static final int DoubleLength = 8;
	static final int TokenLength = 32;

	public SocketClient(String site, int port, int retime) throws UnknownHostException,
			IOException {
		try {
			client = new Socket();
			client.connect(new InetSocketAddress(site,port), 5000);
			client.setSoTimeout(retime);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} 
	}

	public int sendMsg(Req req,Handler recall) throws IOException,SocketTimeoutException {
		try {
			DataOutputStream out = new DataOutputStream(
					client.getOutputStream());
			int tmp = req.type;
			int len;
			switch (tmp) {
			case 0:
				ReqLogin rau = (ReqLogin) req;
				String id = rau.user;
				String pa = rau.pass;
				len = IntLength+ByteLength+id.length()+ByteLength+pa.length()+ByteLength;				
				out.writeInt(len);
				out.writeByte(tmp);				
				out.writeBytes(id);
				out.writeByte(0);
				out.writeBytes(pa);
				out.writeByte(0);
				break;
			case 1:
				ReqUpdate rup = (ReqUpdate) req;
				String tk1 = rup.token;
				String name1 = rup.uname;
				len = IntLength+ByteLength+TokenLength+name1.length()+ByteLength+DoubleLength+DoubleLength;				
				out.writeInt(len);				
				out.writeByte(tmp);				
				double slat = rup.lat;
				double slot = rup.lot;
				byte[] b = hexStringToBytes(tk1);						
				out.write(b);
				out.writeBytes(name1);
				out.writeByte(0);
				out.writeDouble(slat);
				out.writeDouble(slot);
				break;
			case 2:
				ReqLocation ras = (ReqLocation) req;
				String tk2 = ras.token;
				String name2 = ras.uname;
				len = IntLength