|
import
java.net.*;
import java.io.*;
import java.util.*;
import
java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import
javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
class
baza {
public int racun;
public int kljuc;
public String KID;
public
baza () {
KID=""; racun=kljuc=0;
}
}
public class Server_BANKA {
private
final int serverPort = 9811; // PORT
public static String putanja="d:\\baza\\";
public
static void main(String args[]) {
if (args.length!=1)
{
System.out.println("? Upotreba: java Server_BANKA putanja_do_baze");
System.exit(1);
}
putanja = args[0];
// Kreira objekt server i pokrece ga
Server_BANKA server = new Server_BANKA();
server.run();
}
public
Server_BANKA() {
super();
}
public
void run() {
try {
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket
sslserversocket = (SSLServerSocket)sslserversocketfactory.createServerSocket(serverPort);
// Kreira serverov socket na datom portu
// ServerSocket server = new ServerSocket(serverPort);
do {
// Vrti se zauvijek i prihvaca dolazece zahtjeve (konekcije)
//Socket client = server.accept();
SSLSocket client = (SSLSocket)sslserversocket.accept();
// Kreira novu dretvu za obradu svakoga zahtjeva (konekcije)
(new ServerThread(client)).start();
} while
( true );
} catch
( IOException ex ) {
System.exit(0);
}
}
}
class
ServerThread extends Thread {
SSLSocket
client;
// Store
a reference to the socket to which the client is connected
public
ServerThread(SSLSocket client) {
this.client
= client;
}
// Pokretanje
dretve (entry point)
public
void run() {
try {
// Kreira
tokove za komunikaciju sa klijentom
ServiceOutputStream outStream = new ServiceOutputStream(new
BufferedOutputStream(client.getOutputStream()));
ServiceInputStream inStream = new ServiceInputStream(client.getInputStream());
// Cita
klijentov zahtjev iz ulaznog toka
ServiceRequest request = inStream.getRequest();
while
( processRequest(outStream,inStream.strbla) ) {
};
outStream.close();
} catch ( IOException ex ) {
System.exit(0);
}
try {
client.close();
} catch
( IOException ex ) {
System.exit(0);
}
}
/** Provjerava DIGITALNI POTPIS na NOVCANICI
Kontrolne znamenke su jednake zbroju SVIH prethodnih znamenaka
ukljucujuci i kontrolne prije*/
private boolean provjera (String strNovac) {
int intChk1=0,
intChk2=0, intChk3=0, Sum=0;
boolean boolUspio = true,boolUp=true;
// Kontrolne
znamenke
intChk1=strNovac.charAt(7)-48;
intChk2=strNovac.charAt(14)-48;
intChk3=strNovac.charAt(18)-48;
// Petlja
provjere - racuna kontrolne i usporedjuje
for ( short i=1;i<strNovac.length();i++ ) {
if (
(i==7)&&(Sum%8!=intChk1) ) boolUspio=false;
if ( (i==14)&&(Sum%8!=intChk2) ) boolUspio=false;
if ( (i==18)&&(Sum%8!=intChk3) ) boolUspio=false;
Sum+=strNovac.charAt(i)-48;
}
if ( boolUspio==true ) {
try {
// Provjera
u BAZI uporabljenih
String tmpStr1 = new String();
tmpStr1 = strNovac.substring(8,14);
int trBroj=Integer.parseInt(tmpStr1);
String
ime = new String();
int ukupno;
ime = Server_BANKA.putanja+"postojece.txt";
File inputFile = new File(ime);
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream inSt = new DataInputStream(fis);
ukupno = inSt.readInt();
int serija[]= new int[ukupno];
for ( int i=0;i<ukupno;i++ ) {
serija[i]=inSt.readInt();
}
for ( int i=0;i<ukupno;i++ ) {
if ( serija[i]==trBroj ) {
boolUp = false;break;
}
}
inSt.close();
fis.close();
System.out.println("ukupno"+ukupno);
if ( boolUp==true ) {
FileOutputStream
outFile = new FileOutputStream(inputFile);
DataOutputStream outSt = new DataOutputStream(outFile);
ukupno++;
outSt.writeInt(ukupno);
for (
int i=0;i<ukupno-1;i++ ) outSt.writeInt(serija[i]);
outSt.writeInt(ukupno);
outSt.close();
outFile.close();
System.out.println("ukupno"+ukupno);
} // boolUp==true
} catch ( IOException IOGR ) {
System.out.println(IOGR+"\n");
}
} // boolUspio==true
return(boolUspio&&boolUp);
}
// Obrada
zahtjeva je SINKRONIZIRANA ZBOG PISANJA U DATOTEKE
public synchronized boolean processRequest(ServiceOutputStream
outStream, String podatak) {
int kljuc=0,racun=0;
String Stanje="$";
if ( podatak.charAt(0)!='t' ) {
try {
// obrada
kupca
String KID = new String();
KID= podatak.substring(0,6);
String
ime = new String();
ime = Server_BANKA.putanja+"k"+KID+".txt";
File inputFile = new File(ime);
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream inSt = new DataInputStream(fis);
kljuc = inSt.readInt();
racun = inSt.readInt();
inSt.close();
fis.close();
int koliko=0,intChkSum=0,kontrola=0;
Integer
prt = new Integer (koliko);
Integer chk = new Integer (intChkSum);
// treba OPTIMIZACIJA TJ. TO NA POCETAK DA NE OTVARA BEZ VEZE
for ( int i=0;i<podatak.length()-2;i++ ) intChkSum+=podatak.charAt(i);
intChkSum%=100;
kontrola = chk.parseInt(podatak.substring(12,podatak.length()));
if (
kontrola==intChkSum ) {
koliko
= prt.parseInt(podatak.substring(6,12));
racun-=koliko;
Stanje+=Integer.toString(racun);
System.out.println("*******************************************************************");
System.out.println("Kupac "+ime+" zahtjeva
$"+koliko+", na racunu jos $"+racun);
System.out.println("*******************************************************************");
System.out.flush();
}
FileOutputStream
outFile = new FileOutputStream(inputFile);
DataOutputStream outSt = new DataOutputStream(outFile);
outSt.writeInt(kljuc);
outSt.writeInt(racun);
outSt.close();
outFile.close();
/////////////////////////////////////////////////////////////////
String Novcanica = new String();
String TempNovcanica = new String();
Obradi_zahtjev para = new Obradi_zahtjev(podatak);
TempNovcanica
= para.Kreiraj_Novcanicu(koliko);
Novcanica="!"+TempNovcanica;
outStream.println(Novcanica);
outStream.println(Stanje);
outStream.flush();
//////////////////////////////////////////////////////////////////
} catch ( IOException IOGR ) {
System.out.println(IOGR+"\n");
}
} else if ( podatak.charAt(0)=='t' ) {
String
dignov = new String();
String imeTrg = new String();
int stari=0;
try {
// Provjera ispravnosti digitalne novcanice
boolean ok=provjera(podatak.substring(1,podatak.indexOf('|')));
System.out.print("-------------------------------------------------------------------\nProvjera:");
if (
ok==true ) {
System.out.print("OK,
");
imeTrg=Server_BANKA.putanja+"t";
dignov = podatak.substring(2,podatak.indexOf('|'));
imeTrg+= podatak.substring(podatak.indexOf('|')+1,podatak.length());
imeTrg+=".txt";
// Ucitaj
podatke iz datoteke - Stari RACUN
File inputFile = new File(imeTrg);
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream inSt = new DataInputStream(fis);
kljuc = inSt.readInt();
racun = inSt.readInt();
inSt.close();
fis.close();
stari=racun;
// Povecaj
racun TRGOVCU
racun+=Integer.parseInt(dignov.substring(0,6));
// Upisi
podatke nazad
FileOutputStream outFile = new FileOutputStream(inputFile);
DataOutputStream outSt = new DataOutputStream(outFile);
outSt.writeInt(kljuc);
outSt.writeInt(racun);
outSt.close();
outFile.close();
/// Posalji
odgovor TRGOVCU
outStream.println("BOK1");
}
// Posalji odgovor TRGOVCU da nesto nije u redu
else outStream.println("BNO0");
System.out.println("
Lokacija "+imeTrg+"\nSTANJE: STARO $"+stari+"
- NOVO $"+racun);
System.out.println("-------------------------------------------------------------------\n");
} catch ( IOException IOGR ) {
System.out.println(IOGR+"\n");
}
}
return
false;
}
}
///////////////////////////////////////////////////////////////////////////////////
class Obradi_zahtjev {
private
String strPaket;
private String strNovac;
final private String ID_BANKA="321";
public
Obradi_zahtjev(String strPaket1) {
strPaket = new String (strPaket1);
strNovac = new String ();
}
/** Ovo
bi trebalo genreirati serijski broj novcanice
tako da procita zadnji iz baze i uveca za jedan i jedna kontrolna
nakon toga azurira bazu, ovo pripise novcanici i gotovo */
public String GenSerBroj() {
String ime = Server_BANKA.putanja+"serbr.txt";
int serBr=0;
try {
// Ucita
zadnji izdani SERIJSKI BROJ
File inputFile = new File(ime);
FileInputStream fis = new FileInputStream(inputFile);
DataInputStream inSt = new DataInputStream(fis);
serBr = inSt.readInt();
inSt.close();
fis.close();
serBr++; // povecava serijski broj Novcanice za 1 !!!
// Upise
nazad
FileOutputStream outFile = new FileOutputStream(inputFile);
DataOutputStream outSt = new DataOutputStream(outFile);
outSt.writeInt(serBr);
outSt.close();
outFile.close();
} catch ( IOException IOGR ) {
System.out.println(IOGR+"\n");
}
String
temp1 = new String();
String moram = Integer.toString(serBr);
if ( moram.length()<6 ) {
for ( int h=1;h<=6-moram.length();h++ ) {
temp1+="0";
}
}
temp1+=moram;
return temp1; // sada ima 6 znamenaka
}
// generira
ctrl znamenka Sum%8
public String GenKontrol(int intTr) {
int intSum=0;
for ( short i=0;i<intTr-1;i++ ) {
intSum += (int)(strNovac.charAt(i));
}
intSum%=8;
return(Integer.toString(intSum));
}
// provjera
ispravnosti pristiglog paketa
public boolean Provjera() {
int intChkSum = 0, intDuz=0;
String strTmp="";
if (
(intDuz=strPaket.length())!=14 ) {
return false;
}
for (
int i=0;i<intDuz-2;i++ ) {
intChkSum+=(int)strPaket.charAt(i);
}
strTmp+=(strPaket.charAt(13)+strPaket.charAt(14));
if ( intChkSum != Integer.parseInt(strTmp) ) return false;
return
true; // nije pao ni na jednoj provjeri
}
// oblikovanje
novcanice: Iznos(6)+Ctrl1+Ser_br+Ctrl2+ID_Banke+Ctrl3
public String Kreiraj_Novcanicu(int intKolicina) {
String
temp1 = new String();
String moram = Integer.toString(intKolicina);
if ( moram.length()<6 ) {
for ( int h=1;h<=6-moram.length();h++ ) {
temp1+="0";
}
}
temp1+=moram;
strNovac=temp1;
//strNovac+=
arrTmp;
strNovac+=(GenKontrol(7)+GenSerBroj());
strNovac+=(GenKontrol(14)+ID_BANKA);
strNovac+=GenKontrol(18);
strNovac+="\r\n";
return strNovac;
}
}
// Input
stream filter
class
ServiceInputStream extends FilterInputStream {
public String strbla;
public ServiceInputStream(InputStream in) {
super(in);
}
public
String readLine() throws IOException {
StringBuffer
result=new StringBuffer();
boolean finished = false;
boolean cr = false;
do {
int ch = -1;
ch = read();
if ( ch==-1 ) return result.toString();
result.append((char) ch);
if (
cr && ch==10 ) {
result.setLength(result.length()-2);
return result.toString();
}
if (
ch==13 ) cr = true;
else cr=false;
} while
( !finished );
return result.toString();
}
// Metoda
za citanje Klijentovog zahtjeva sa ulaznog toka
public ServiceRequest getRequest() throws IOException {
ServiceRequest request = new ServiceRequest();
strbla=readLine();
return request;
}
}
// naslijedjeno
iz FilterOutputStreama - klasa sluzi za ispis u izlazni tok
class ServiceOutputStream extends FilterOutputStream {
public
ServiceOutputStream(OutputStream out) {
super(out);
}
public void println() throws IOException {
write(13);
write(10);
}
public
void println(String s) throws IOException {
for ( int i=0;i<s.length();++i ) write(s.charAt(i));
println();
}
}
// Klasa
implementacije korisnikovih zahtjeva
class ServiceRequest {
Vector lines = new Vector();
public String lnm;
public
ServiceRequest() {
}
public
void addLine(String line) {
lines.addElement(line);
}
public
boolean WhichReq() {
lnm=(String) lines.elementAt(0);
return true;
}
}
|