public static void main(String[] args) { try { if (args.length > 0) Application.DIRECTORY_FILE = args[0]; try { if (args.length > 1) DirectoryServer.PORT = Integer.parseInt(args[1]); } catch (Exception e) { e.printStackTrace(); } DirectoryServer ds = new DirectoryServer(DirectoryServer.PORT); ds.start(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
public void _run() { Socket client = null; try { client = (Socket) ctx; __run(client); } catch (Exception e) { e.printStackTrace(); } try { client.close(); } catch (IOException e) { e.printStackTrace(); } DirectoryServer.leaving(this, client); }
public void __run(Socket client) { try { client.setSoTimeout(DirectoryServer.SOCKET_READ_TIMEOUT); // out.println("DirServ: Accepted... from: "+client.getRemoteSocketAddress()); InetSocketAddress risa = null; try { // InetAddress risa = ((InetSocketAddress) client.getRemoteSocketAddress()).getAddress(); risa = (InetSocketAddress) client.getRemoteSocketAddress(); } catch (Exception e) { e.printStackTrace(); } byte buffer[] = new byte[DirectoryServer.MAX_DR]; int peek = client.getInputStream().read(buffer); if (peek < 0) { out.println("DirServTCP: got no bytes from: " + risa); return; } else { out.println("DirServTCP: __run got bytes from: " + risa); } // out.println("DirServ: Got ASN1 dump: "+Util.byteToHexDump(buffer,peek)); Decoder test = new Decoder(buffer, 0, peek); // out.println("DirServTCP: __run got decoder: "+test); out.println( "DirServTCP: __run: Decoded ASN1: class=" + test.typeClass() + " val=" + test.tagVal() + " blen=" + buffer.length); DirMessage m; // recording msgs if (test.typeClass() == Encoder.CLASS_APPLICATION && test.tagVal() == DirectoryAnnouncement.TAG) { // out.println("DirServ: Detected directory announcement"); InetSocketAddress isa = (InetSocketAddress) client.getRemoteSocketAddress(); DirectoryAnnouncement da = new DirectoryAnnouncement(buffer, peek, client.getInputStream()); out.println("DirServTCP: got announcement: " + da + "\n from: " + isa); // here should first get the old detected address (probably from UDP) and keep it if has // same IP // this stores the message in the announcement_storage for this GID, and the GID - IP // relation in its hashtable DirectoryServer.recordAnnouncementMessage( isa, da, null, DirMessage.TCP, DirMessage.ANNOUNCEMENT); // creates an address object from the socket and reported udp port Address detected_sa = DirectoryServer.detectUDP_Address(isa, da.address.udp_port); out.println("DirServTCP: got announcement: detected = " + detected_sa); // Address detected_sa = new Address(_detected_sa); detected_sa = DirectoryServer.addr_NAT_detection(da, detected_sa); out.println("DirServTCP: got announcement: detected tuned = " + detected_sa); if (da.address.udp_port <= 0) detected_sa = null; boolean storeNAT; boolean TCP; DirectoryAnnouncement_Answer daa = DirectoryServer.handleAnnouncement( da, detected_sa, DirectoryServer.db_dir, storeNAT = false, TCP = true); byte[] answer = new byte[0]; if (daa != null) answer = daa.encode(); // byte[] answer = new D_DAAnswer(detected_sa).encode(); client.getOutputStream().write(answer); DirectoryServer.recordAnnouncementMessage( isa, da, daa, DirMessage.TCP, DirMessage.ANN_ANSWER); } else { // boolean DEBUG = true; if (DEBUG) out.println("DSTCP: Potential directory request"); // handling terms here DirectoryRequest dr = new DirectoryRequest(buffer, peek, client.getInputStream()); if (dr.empty()) { out.println("DirServTCP:__run: potential message detected empty = " + risa + " dr=" + dr); return; } // boolean acceptedTerms = areTermsAccepted(dr); InetSocketAddress isa = (InetSocketAddress) client.getRemoteSocketAddress(); if (DEBUG) out.println("Received directory request: " + dr); DirectoryServer.recordRequestMessage(isa, dr, null, DirMessage.TCP, DirMessage.REQUEST); if (DEBUG) out.println( "DirServ: Looking for: " + D_Peer.getGIDHashFromGID(dr.globalID) + "\n by " + D_Peer.getGIDHashFromGID( dr.initiator_globalID)); // +"\n with source udp="+dr.UDP_port); String globalID = dr.globalID; // looking for peer GID String globalIDhash = dr.globalIDhash; // looking for peer GID hash // de has the look-for-peer and all instances stored in the db D_DirectoryEntry de = DirectoryServerCache.getEntry(globalID, globalIDhash); if (DEBUG) out.println("DirServ: From cache got: " + de); ASNObj da = DirectoryServer.getDA(de, dr, dr.version); if ((da == null)) // || (da.date == null)) { System.out.println( "DirectoryServer:__run: abandon: ?why da=" + da + "\n\tde=" + de + "\n\tdr=" + dr); return; } DirectoryServer.recordRequestMessage( isa, dr, da, DirMessage.TCP, DirMessage.REQUEST_ANSWER); byte msg[] = da.encode(); if (DEBUG) { Decoder dec = new Decoder(msg); DirectoryAnswerMultipleIdentities dami = new DirectoryAnswerMultipleIdentities(dec); System.out.println("DirectoryServer:_run:encode " + da + "\nto " + dami); } // out.println("answer: "+Util.byteToHexDump(msg, " ")+"\n\tI.e.: "+da); /* if(_DEBUG&&(da.addresses.size()>0)){ out.println("DirServ: *******"); out.println("DirServ: Aanswer: "+client.getRemoteSocketAddress()+" <- "+da.toString()); } */ client.getOutputStream().write(msg); } client.close(); } catch (SocketException e) { out.println("server: " + e); return; } catch (Exception e) { e.printStackTrace(); return; } }